I am trying to relate an entity to a collection in my system where this collection is associated to another entity object as well. We originally had the following entities:
Code:
// ItemId entity
public class ItemId implements Serializable {
private Long itemId;
private Long plantId;
@Column(name="ITEM_ID",unique=false,nullable=false,insertable=true,updatable=true,scale=0)
public Long getItemId() { return itemId; }
public void setItemId(Long itemId) { this.itemId = itemId; }
@Column(name="PLANT_ID",unique=false,nullable=false,insertable=true,updatable=true,scale=0)
public Long getPlantId() { return plantId; }
public void setPlantId(Long plantId) { this.plantId = plantId; }
// hashCode/equals
}
// Item entity
public class Item implements Serializable {
private ItemId id;
@EmbeddedId
public ItemId getId() { return id; }
public void setId(ItemId id) { this.id = id; }
@OneToMany(fetch=FetchType.LAZY,mappedby="item")
public List<ItemText> getTexts() { return texts; }
public void setTexts(List<ItemText> texts) { this.texts = texts; }
};
// Item Text
public class ItemText implements Serializable
{
private ItemTextId id;
// more stuff
@EmbeddedId
public ItemTextId getId() { return id; }
public void setId(ItemTextId id) { this.id = id; }
@ManyToOne
@JoinColumns({
@JoinColumn(name="ITEM_ID",insertable=false,updatable=false),
@JoinColumn(name="PLANT_ID",insertable=false,updatable=false)
})
public Item getItem() { return item; }
public void setItem(Item item) { this.item = item; }
}
We now need to create another entity based on a database view, called ItemSearchView and what I would like to do is be able to associate the ItemText collection in this view just like I did with the Item entity above. The problem is if I do that, I get a collection foreign key exception:
Caused by: org.hibernate.AnnotationException: A Foreign key refering com.setech.seek.hibernate.model.ItemSearchV from com.setech.seek.hibernate.model.ItemText has the wrong number of column. should be 1
at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:429)
at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1443)
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1262)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:693)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:628)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:65)
Is it not possible for me to do a @ManyToMany associate in my View object to ItemText but also have a @OneToMany relationship between Item and ItemText simultaneously ??? If so, what do I need to change to accomplish this?