Hallo together,
i have a problem with the error message "Duplicate entries", which i can't comprehend....
I show you the two classes and the testcase, with the necessary explanations (looks more then it really is.... The problem is also explained in the testcase):
---------------- class Person ---------------------
@Entity(access = AccessType.FIELD)
@Table(name = "Person")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Person {
@OneToOne(cascade = CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn (name = "Klasse_1_ID")
private Klasse_1 klasse_1 ;
.
.
.
.
.
}
--------------------- class Klasse_1 -----------------------
@Entity(access = AccessType.FIELD)
@Table(name = "Klasse_1")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Klasse_1 {
@OneToOne(cascade = CascadeType.ALL, mappedBy = "klasse_1", fetch=FetchType.EAGER)
private Person person ;
.
.
.
.
}
------------------------- junit testCase-----------------------
// opens the session
session = HibernateUtil.currentSession();
// creates data in database which are used for the test case
dataCreation(session);
// fetching necessary data
Query q = session
.createQuery("select c from Person c where c.attribut_1 = :attribut_1");
q.setString("attribut_1", "0");
List<Person> person_list = q.list();
Person person = person_list.get(0);
Query p = session
.createQuery("select c from Klasse_1 c where c.attribut_1 = :attribut_1");
p.setString("attribut_1", "0");
List<Klasse_1> klasse_1_list = p.list();
Klasse_1 klasse_1 = klasse_1_list.get(0);
Klasse_1 klasse_11 = klasse_1_list.get(1);
// this set method is bidirectional. It sets a reference from class
// person to class klasse_1 and vice versa
person.setKlasse_1(klasse_1);
session.saveOrUpdate(person);
session.flush();
session.connection().commit();
// fetching necessary data
q = session
.createQuery("select c from Person c where c.attribut_1 = :attribut_1");
q.setString("attribut_1", "0");
person_list = q.list();
person = person_list.get(0);
Person person1 = person_list.get(1);
p = session
.createQuery("select c from Klasse_1 c where c.attribut_1 = :attribut_1");
p.setString("attribut_1", "0");
klasse_1_list = p.list();
klasse_1 = klasse_1_list.get(0);
klasse_11 = klasse_1_list.get(1);
// reference is set to klasse_11 and is deleted from klasse_1
//////////////////////////////////////////////////////////////////////
// HERE IS THE PROBLEM :
// Exceptionmessage: Caused by: java.sql.BatchUpdateException:
// Invalid argument value, message from server: "Duplicate
// entry '252' for key 2"
// This set method deletes the bidiricetional reference from class
// person and class klasse_1 and creates a new one between
// class klasse_11 (this works... i've tested it several times).
// It only works, when i first delete the bidirectional reference,
// actualize the database and the creates the new bidirectional
// database; but why? the new object have all the right values,
// why can't they be set in the database?
//////////////////////////////////////////////////////////////////////
person.setKlasse_1(klasse_11);
session.saveOrUpdate(klasse_1);
session.saveOrUpdate(klasse_11);
session.saveOrUpdate(person);
session.flush();
session.connection().commit();
---------------------------------
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:179)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:137)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:675)
at testcases.AssocTestCase.testKlasse1PersonDataBase(AssocTestCase.java:1339)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194)
Caused by: java.sql.BatchUpdateException: Invalid argument value, message from server: "Duplicate entry '252' for key 2"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1404)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:172)
... 21 more
Hope you can help me
Regards,
Martin
|