-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problems with duplicate entries
PostPosted: Fri May 20, 2005 5:55 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 ---------------------
Code:
@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 -----------------------
Code:
@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-----------------------
Code:
// opens the session
session = HibernateUtil.currentSession();

// creates data in database which are used for the test case
dataCreation(session);

// fetching necessary data
.
.
.
.


// 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
.
.
.
.


// 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();



---------------------------------
Code:
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



A little more information about my problem:

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?


Hope you can help me
Regards,
Martin


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 8:56 am 
Beginner
Beginner

Joined: Mon Mar 21, 2005 1:39 pm
Posts: 28
Hallo,
has really nobody an idea for this problem?

regards,
Martin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 9:29 am 
Beginner
Beginner

Joined: Mon Mar 21, 2005 1:39 pm
Posts: 28
Hallo,
i just updated to the newest versions of hibernate (version 3.0.5) and hibernate annotations.
I hoped, that that could eventually eliminate my problem, but it didn't....

Regards,
Martin


Top
 Profile  
 
 Post subject: could you have the wrong cascade option?
PostPosted: Sat Jun 25, 2005 12:08 am 
Beginner
Beginner

Joined: Thu Dec 09, 2004 7:04 pm
Posts: 26
Location: Denver, CO
My understanding of your unit test is that you create the following relationship:
Code:
  Person  <---> class1

and then you edit the relationships so that it is
Code:
         <---  class1
  Person <---> class11

which would probably cause a foreign key issue depending on what your primary key is. I would think you are missing a
Code:
  klasse_1.setPerson(null);

  or

  public void setKlasse_1(Klasse_1 k) {
    if(klasse_1 != null) {
      klasse_1.setPerson(null);
    }
    this.klasse_1 = k;
    k.setPerson(this);
  }


John


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.