Bom dia, Estou tentando persistir dados usando relacionamento no hibernate e estou obtendo o seguinte erro: alguem sabe como me resolver?
09:29:20,265 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.sigga.test.CommonTest.testJoin(CommonTest.java:42)
package com.sigga.test;
import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
@Entity
@Table
public class Universidade {
@Id
@Column(name = "id_universidade")
private int id;
private String nome;
@OneToMany(mappedBy = "universidade", fetch = FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@Cascade(CascadeType.ALL)
private Collection<Centro> centros;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Collection<Centro> getCentros() {
return centros;
}
public void setCentros(Collection<Centro> centros) {
this.centros = centros;
}
}
package com.sigga.test;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
@Entity
@Table
public class Centro {
@Id
@Column(name = "id_centro")
private int id;
private String nome;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "id_universidade")
@Fetch(FetchMode.JOIN)
private Universidade universidade;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Universidade getUniversidade() {
return universidade;
}
public void setUniversidade(Universidade universidade) {
this.universidade = universidade;
}
}
public void testJoin() {
Object owner = null;
Session session = null;
Transaction tx = null;
Universidade univ = null;
try {
owner = HibernateSession.createSession();
session = HibernateSession.getSession();
tx = session.getTransaction();
tx.begin();
univ = new Universidade();
univ.setNome("Universidade Federal do Rio Grande do Norte");
session.save(univ);
tx.commit();
session.flush();
Centro centro1 = new Centro();
centro1.setNome("Centro de Tecnologia");
centro1.setUniversidade(univ);
if(!tx.isActive())
tx.begin();
session.save(centro1);
tx.commit();
session.flush();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
finally {
try {
session.flush();
HibernateSession.closeSession(owner);
} catch (Exception e) {
e.printStackTrace();
}
}
}