-->
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.  [ 6 posts ] 
Author Message
 Post subject: inheritance, composite keys and duplicate entry
PostPosted: Tue Sep 11, 2007 4:01 am 
Newbie

Joined: Mon Sep 10, 2007 1:09 pm
Posts: 3
Hello,

Let A be a container class with a Set of objects of class B, with bidirectional mapping.
B holds a reference to an object of class C.
B has a composite primary key composed of its two references to the A and C objects.

Here are my corresponding simplified annotated classes :

Code:
@Entity
public class A implements Serializable {
   @Id @GeneratedValue(strategy=GenerationType.AUTO) private long id;

   @OneToMany(mappedBy="A", cascade = CascadeType.ALL)
   @LazyCollection(LazyCollectionOption.FALSE)
   @Cascade({org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
   private Set<B> myBs = new HashSet<B>();
}


@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@IdClass(BPrimaryKey.class)
public class B implements Serializable {
   @Id @ManyToOne private A myA;   
   @Id @ManyToOne private C myC;
}


@Embeddable
public class BPrimaryKey implements Serializable {
   @ManyToOne private A myA;
   @ManyToOne private C myC;
}


@Entity
public class C implements Serializable {
   @Id @GeneratedValue(strategy=GenerationType.AUTO) private long id;
}


I create object 'a' and 'c' of classes A and C, and an object b of class B which references a and c. a also has b in its myBs set. I saveOrUpdate a and c and everything gets correctly created and linked in the database (thanks to cascade).

In an other session, the next step loads 'a' back from the database and the saveOrUpdate it. Ok.



Now I create a subB subclass like this :
Code:
@Entity
public class subB extends B { /*data omitted*/ }


I run the same test using a subB instance instead of a B : creation and association to 'a' and 'c', then saving. Fine. But the next step (load 'a' and simply save it) fails with a "Duplicate entry" error message while the SQL trace shows that Hibernate tries to insert the subB instance again, with the same composite key ('a' and 'c' ids).

Using a TABLE_PER_CLASS or a SINGLE_TABLE inheritance strategy does not solve the problem.

The database creation script are very similar for B and subB, both declaring a "primary key (A_id, C_id)". But it works well with the super class (B), but fails with inherited classes (subB).

Using Hibernate 3.2.3, annotations V3.3.0 and MySQL 4.1.12.

Could you tell me what is wrong in this example ? I'm new to hibernate, do not hesitate to ask if you need more information.

Thanks in advance,

Gilles


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 11, 2007 4:45 am 
Beginner
Beginner

Joined: Mon Aug 27, 2007 8:10 am
Posts: 37
There are some bugs with composite key. Please reffer to this topic
http://forum.hibernate.org/viewtopic.ph ... highlight=


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 11, 2007 10:13 am 
Newbie

Joined: Mon Sep 10, 2007 1:09 pm
Posts: 3
hutorny wrote:
There are some bugs with composite key. Please refer to this topic
http://forum.hibernate.org/viewtopic.ph ... highlight=


Thanks for the advice,

I tried to compare the hasIdentifierMapper value for the B and subB classes, but unfortunately this property is private, and I don't see how to get or set it.

If composite key id are fragile, I'd be happy to switch if there is an alternative. Maybe removing the @IdClass annotation ?

Has someone encountered a similar problem ? Is there hope for a patch or for an alternative solution or do I need to consider getting rid of hibernate in that case ?


Last edited by gilles on Thu Sep 13, 2007 3:44 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 12, 2007 2:42 pm 
Beginner
Beginner

Joined: Mon Aug 27, 2007 8:10 am
Posts: 37
gilles wrote:
I tried to compare the
Code:
hasIdentifierMapper
value for the B and subB classes, but unfortunately this property is private, and I don't see how to get or set it.

If composite key id are fragile, I'd be happy to switch if there is an alternative. Maybe removing the @IdClass annotation ?

Has someone encountered a similar problem ? Is there hope for a patch or for an alternative solution or do I need to consider getting rid of hibernate in that case ?


We discovered the difference in debugger's evaluate window and then patched the issue with reflection API.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 13, 2007 3:43 pm 
Newbie

Joined: Mon Sep 10, 2007 1:09 pm
Posts: 3
hutorny wrote:
We discovered the difference in debugger's evaluate window and then patched the issue with reflection API.


Thanks,

I didn't know the reflection API allowed access to private methods. I may consider using it. However, the workaround I implemented is simply to delete the object from the database before I update it (so that its insertion does not create a duplicate entry).

I hope a future release will fix the problem, since I consider this as a bug. Should I report it somewhere ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 14, 2007 5:05 am 
Beginner
Beginner

Joined: Mon Aug 27, 2007 8:10 am
Posts: 37
gilles wrote:
I hope a future release will fix the problem, since I consider this as a bug. Should I report it somewhere ?


For an issue to be accepted they require a test that isolates the problem.
I would report this issue, but unfortunatelly, we are behind the schedule with our project and I have no time to do it right.

If you may do it, please do: http://opensource.atlassian.com/project ... board.jspa


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