Hallo,
Ich versuche mit Hibernate/EJB3 2 neu erzeugten Objekte von der selben Klasse(Gene) zu persistieren. Diese 2 neue Objekte haben eine und dieselbe Liste von schon-persistierten Objekten einer anderen Klasse(CancerType) als Instanzvariablen. Die Beziehung ist 1-n, Gene hat mehrere CancerTpye. Wenn ich diese 2 neue Objekte persisteren will, kriege ich ein "Duplicate entry" Fehler.
Kann jemand aus den folgenden Codes herausfinden, was falsch ist? Danke im voraus.
Hibernate erzeugt für diese 2 Klassen mit der 1-n Beziehung 3 Tabellen. Gene, CancertType und Gene_CancerType.
Code
@Entity
public class Gene implements java.io.Serializable {
@Id @GeneratedValue private Integer id;
private String name;
@OneToMany()
private List<CancerType> cancerTypes;
public Gene() { }
// Setters and Getters
}
Alles anzeigen
Die Tabelle CancerType wird schon am Anfang mit Daten gefüllt.
Code
@Entity
public class CancerType implements java.io.Serializable {
@Id @GeneratedValue private Integer id;
private String name;
public CancerType(){ }
// Setters and Getters
}
Alles anzeigen
Und die Main-Methode
Code
Session session = new AnnotationConfiguration().configure().buildSessionFactory().getCurrentSession(
);
session.beginTransaction();
Gene g1 = new Gene();
g1.setName("name");
Gene g2 = new Gene();
g2.setName("name");
List<CancerType> cts = BasicDAO.session.createQuery("from CancerType").list();
g1.setCancerType(cts);
g2.setCancerType(cts);
session.save(g1);
session.save(g2);
session.getTransaction().commit();
Alles anzeigen
StackTrace:
Code
Hibernate: select cancertype0_.id as id0_, cancertype0_.name as name0_ from CancerType cancertype0_
Hibernate: insert into Gene (name) values (?)
Hibernate: insert into Gene (name) values (?)
Hibernate: insert into Gene_CancerType (Gene_id, cancerTypes_id) values (?, ?)
Hibernate: insert into Gene_CancerType (Gene_id, cancerTypes_id) values (?, ?)
00:01:39,109 WARN JDBCExceptionReporter:77 - SQL Error: 1062, SQLState: 23000
00:01:39,109 ERROR JDBCExceptionReporter:78 - Duplicate entry '1' for key 1
00:01:39,109 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with ses
sion
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
Exception in thread "main" 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:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingE
ventListener.java:298)
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:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingE
ventListener.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 tvsas1.Main.a2(Main.java:58)
at tvsas1.Main.main(Main.java:64)
Caused by: java.sql.BatchUpdateException: Duplicate entry '1' for key 1
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1237)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:936)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 9 more
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 tvsas1.Main.a2(Main.java:58)
at tvsas1.Main.main(Main.java:64)
Caused by: java.sql.BatchUpdateException: Duplicate entry '1' for key 1
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1237)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:936)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 9 more
Alles anzeigen