-->
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.  [ 1 post ] 
Author Message
 Post subject: @CollectionId and accessing the PK of embedded objects
PostPosted: Mon Mar 31, 2008 9:55 am 
Newbie

Joined: Sun Jan 02, 2005 10:17 pm
Posts: 13
Hi all,

I have an Item entity with a set of Images as a collection of embedded objects (similar to section 6.3.3 in the book Java Persistence with Hibernate):

Code:
@Entity
@Table(name="item")
public class Item {

   private Long id;
   private List<Image> images;

   .....

   @CollectionOfElements
   @JoinTable(name="item_image", joinColumns=@JoinColumn(name="item_id"))
   @CollectionId(
      columns=@Column(name="id"),
      type=@Type(type="long"),
      generator="sequence")
   public List<Image> getImages() {
      return images;
   }

   public void setImages(List<Image> images) {
      this.images = images;
   }

}

@Embeddable
public class Image {

   private String name;
        private Item item;
   .....

   @Parent
   public Item getItem() {
      return this.item;
   }

}


Using the above mapping, my application cannot access the primary key for the Image objects. I tried to rewrite the Image class as follows so my app can access the primary key of the Image class:

Code:
public class Image {
   private Long id;
   ....


   @Id
   @GeneratedValue(...)
   public Long getId() {
      return this.id;
   }

   private void setId(Long id) {
      this.id = id;
   }
}


Doing so results in an error (specifically, Hibernate complains that the primary key for Image is mapped twice). My questions is: using annotations, how can I achieve the benefits of <idbag> mapping (specifically automatic cascade deletes) and still be able to retrieve/get the primary key of the embedded object (in this case, the primary key of the Image entities)?

Thanks in advance :)


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

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.