3
I have the following problem: I can save/persist the two objects below, but JPA is not linking the ID of the DeclaracaoImportacao
in the column ID class/entity .
Follows code
@Entity
@Table (name = "declaracaoimportacao")
public class DeclaracaoImportacao implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String importadorNome;
private double freteTotalMoeda;
private String importadorEnderecoMunicipio;
private String importadorEnderecoLogradouro;
private int importadorEnderecoCep;
private String importadorEnderecoBairro;
private int importadorEnderecoNumero;
private String importadorEnderecoUf;
private String conhecimentoCargaEmbarqueData;
private String dataRegistro;
private int freteMoedaNegociadaCodigo;
private int numeroDI;
private double freteValorMoedaNegociada;
private double freteCollect;
private double fretePrepaid;
private double freteTotalReais;
@OneToMany(cascade={CascadeType.ALL},mappedBy = "declaracaoImportacao")
@Fetch(FetchMode.JOIN)
private List<Adicao> adicao = new ArrayList<Adicao>();
public DeclaracaoImportacao(){
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Adicao> getAdicoes() {
return adicao;
}
public void setAdicao(ArrayList<Adicao> adicao) {
this.adicao = adicao;
}
public void addAdicao(Adicao adicao) {
addAdicao(adicao, true);
}
void addAdicao(Adicao adicao, boolean set) {
if (adicao != null) {
if(getAdicoes().contains(adicao)) {
getAdicoes().set(getAdicoes().indexOf(adicao), adicao);
}
else {
getAdicoes().add(adicao);
}
if (set) {
adicao.setDi(this, false);
}
}
}
// outros getters e setters
@Override
public boolean equals(Object object) {
if (object == this)
return true;
if ((object == null) || !(object instanceof DeclaracaoImportacao))
return false;
final DeclaracaoImportacao di = (DeclaracaoImportacao)object;
if (id != null && di.getId() != null) {
return id.equals(di.getId());
}
return false;
}
}
Following the class Adicao
, that should receive the ID that comes from the class DeclaracaoImportacao
:
@Entity
@Table(name = "adicao")
public class Adicao implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id_adicao;
@Column
private String condicaoVendaIncoterm;
private int paisOrigemMercadoriaCodigo;
private String paisOrigemMercadoriaNome;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="id",referencedColumnName="id")
private DeclaracaoImportacao declaracaoImportacao;
public Adicao(){
}
public Long getId() {
return id_adicao;
}
public void setId(Long id) {
this.id_adicao = id;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public DeclaracaoImportacao getDi() {
return declaracaoImportacao;
}
public void setDi(DeclaracaoImportacao di) {
setDi(di, true);
}
void setDi(DeclaracaoImportacao di, boolean add) {
this.declaracaoImportacao = di;
if (di != null && add) {
di.addAdicao(this, false);
}
}
// outros getters and setter
@Override
public boolean equals(Object object) {
if (object == this)
return true;
if ((object == null) || !(object instanceof Adicao))
return false;
final Adicao adicao = (Adicao)object;
if (id_adicao != null && adicao.getId() != null) {
return id_adicao.equals(adicao.getId());
}
return false;
}
}
As you can see I’ve been quite concerned with maintaining the integrity of objects in the database.
Follow the DAO class to DeclaracaoImportacao
:
public class DeclaracaoImportacaoFacade implements Serializable {
private static final long serialVersionUID = 1L;
DeclaracaoImportacaoDAO diDAO = new DeclaracaoImportacaoDAO();
public void createDeclaracaoImportacao(DeclaracaoImportacao di){
diDAO.beginTransaction();
// Esse método save está dentro de uma superclasse DAO que faz a criação do EntityManager
diDAO.save(di);
diDAO.commitAndCloseTransaction();
}
And that’s the class Dad GenericDAO
:
abstract class GenericDAO<T> implements Serializable {
private static final long serialVersionUID = 1L;
private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("JSFCrudPU");
private EntityManager em;
private Class<T> entityClass;
public void beginTransaction() {
em = emf.createEntityManager();
em.getTransaction().begin();
}
public void commit() {
em.getTransaction().commit();
}
public void rollback() {
em.getTransaction().rollback();
}
public void closeTransaction() {
em.close();
}
public void commitAndCloseTransaction() {
commit();
closeTransaction();
}
public void flush() {
em.flush();
}
public void joinTransaction() {
em = emf.createEntityManager();
em.joinTransaction();
}
public GenericDAO(Class<T> entityClass) {
this.entityClass = entityClass;
}
public void save(T entity) {
em.persist(entity);
}
public void delete(Object id, Class<T> classe) {
T entityToBeRemoved = em.getReference(classe, id);
em.remove(entityToBeRemoved);
}
public T update(T entity) {
return em.merge(entity);
}
public T find(int entityID) {
return em.find(entityClass, entityID);
}
public T findL(Long entityID) {
return em.find(entityClass, entityID);
}
}
When I will persist the entity DeclaracaoImportacao
automatically it persists the class Adicao
, or that you don’t need to call a save method(Adicao
) for the addition class.
But, the JPA as I said earlier is failing to link the ID of the DeclaracaoImportacao
on the table Adicao
this causes the relation N->1 not to be created.
Follow prints showing the already persisted objects in Mysql.
I really looked for everything personal, could give a strength ?
Welcome to Stack Overflow! Your question is very clear and very well described, good preparation!
– Sérgio Mucciaccia