-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Problems with duplicate entries
PostPosted: Thu May 12, 2005 9:38 am 
Beginner
Beginner

Joined: Mon Mar 21, 2005 1:39 pm
Posts: 28
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 17, 2005 8:01 am 
Beginner
Beginner

Joined: Mon Mar 21, 2005 1:39 pm
Posts: 28
Hi again,
Has really nobody any idea to solve this strange problem?

Regards,
Martin


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 17, 2005 1:37 pm 
Beginner
Beginner

Joined: Mon Mar 21, 2005 1:39 pm
Posts: 28
A little more information about my problem ( which i recognized today):

Looking at the specific tables of both classes you can see, that both entries have always the same primary key (if that happens by chance i don't know).
Can it be, that the annotations are interpreted in a wrong way? that there is a relationship between both objects by primary key and not with a foreign key, as specified?
Or can there be a problem, because the annotations for the attriubtes are defined by the field attributes and not by the accessors?



Regards,
Martin


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.