I have two classes, Terminal
(which is the parent) and Portaria
(child).
I'm using JPA and it's working, but it has a bug that I can not resolve.
I load a list of Terminal
and when testing t.getPortarias()
it is instantiated and showing the children to those who have and showing size 0 to those who do not have children, so far everything is right.
Terminal
, this new object does not instantiate t.getPortarias()
gets as null
, the other objects are there with the children, but the new object does not. If I close the system and open it again the new object appears with the child instantiated.
The problem is that it gives NullPointerException
when testo t.getPortarias().isEmpty()
because it is not loading the child object when I give persist
to the object.
I have tried to use different manager in every DAO, I have tried to use the same one and also did not give. I already put lazy and eager fetch and did not work either.
Terminal Class
@Entity
public class Terminal implements Serializable {
@Id
@GeneratedValue
private int terminalId;
@Column(nullable = false, unique = true)
private String nome;
@Column(nullable = false, unique = true)
private String cnpj;
@Column(nullable = false)
private String endereco;
@Lob
private byte[] logo;
@OneToMany(mappedBy = "terminal")
private List<Portaria> portarias;
Ordinance Class
@Entity
public class Portaria implements Serializable {
@Id
@GeneratedValue
private int portariaId;
@Column(nullable = false)
private String descricao;
@ManyToOne
@JoinColumn(name = "terminalId", nullable = false)
private Terminal terminal;
In this section I test whether or not I can exclude, but it sucks because List
of getPortarias
nor was instantiated:
public boolean canDelete(Terminal t) throws BusinessException {
if (!t.getPortarias().isEmpty()) {
throw new BusinessException("Não é possível excluir porque existe portarias associadas a este terminal");
}
return true;
}
In this section I get the object by id
to delete:
public T getById(int id) {
return getDbManager().find(entityClass, id);
}
How do I make fetch of children work?