-->
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: Wrong PK for @ElementCollection
PostPosted: Mon Jan 24, 2011 4:24 am 
Regular
Regular

Joined: Wed Sep 21, 2005 1:37 pm
Posts: 53
Location: Montpellier - France
Hi all,

I have a wrong definition of the primary key for the table used to store the embedded components of an entity.

Hibernate: 3.5.1-Final

Code is:

Code:
@Entity(name = "A")
@org.hibernate.annotations.Entity(dynamicUpdate = true, optimisticLock = OptimisticLockType.VERSION)
@Table(name = "A")
public class ABean {
...
@ElementCollection(fetch = FetchType.EAGER, targetClass = AComponent.class)
@JoinTable(name = "B", joinColumns = @JoinColumn(name = "aid", nullable = false),
   @UniqueConstraint(columnNames = {
      ID, AComponent.OFFSET
   })
)
@Sort(type = SortType.COMPARATOR, comparator = BComparator.class)
@ForeignKey(name = "FK_A_B")
public SortedSet<AComponent> getTemplates() {
   return templates;
...
}

@Embeddable
public class AComponent {
...
@JoinColumn(name = "cid", referencedColumnName = "cid", nullable = false)
@ManyToOne(optional = false, targetEntity = CBean.class, fetch = FetchType.LAZY)
@ForeignKey(name = "FK_B_C")
public C getZone() {
   return zone;
}
...
@Column(name = OFFSET, nullable = false)
@Index(name = "IDX_B_OFFSET")
public long getOffset() {
   return offset;
}
...
}


The primary key created by hibernate is: 'aid', 'offset' and 'cid'.
I don't understand why 'cid' is a part of the pk. I suppose it is due to the @JoinColumn in the component but, in my opinion, it's a mistake or a bug.

Thanks.


Top
 Profile  
 
 Post subject: Re: Wrong PK for @ElementCollection
PostPosted: Mon Jan 24, 2011 9:40 am 
Regular
Regular

Joined: Wed Sep 21, 2005 1:37 pm
Posts: 53
Location: Montpellier - France
I have found a 'bad' workaround for this issue. Now I use a List instead of a SortedSet for the 'getTemplate' property and collection.


Top
 Profile  
 
 Post subject: Re: Wrong PK for @ElementCollection
PostPosted: Thu Jan 27, 2011 11:01 pm 
Newbie

Joined: Thu Jan 27, 2011 10:17 pm
Posts: 1
I've recently run into this as well with Hibernate 3.5.5-Final using Sets of Embeddables. It appears to be a bug because the behavior breaks Set semantics.

A different work around than the one mentioned above is to make each field nullable in the embeddable component. If all fields are nullable, then Hibernate does not create a primary key for the element collection.

So this bug appears to be triggered by the combination of using an @ElementCollection of type Set and having non-nullable fields on the Embeddable.


Top
 Profile  
 
 Post subject: Re: Wrong PK for @ElementCollection
PostPosted: Fri Jan 28, 2011 4:07 am 
Regular
Regular

Joined: Wed Sep 21, 2005 1:37 pm
Posts: 53
Location: Montpellier - France
Good explanation.

Thanks


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.