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