我試圖建立一個多基於這個例子與JPA許多雙向關係:http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/多對多的關係:org.hibernate.MappingException:無法確定類型:java.util.Set中
我米得到一個
org.hibernate.MappingException:無法確定類型:java.util.Set中,在表中:T_ITEM,對於列:[org.hibernate.mapping.Column(itemHikes)]
如果有人能幫助我,那就是下面的源代碼:
類物品
@Entity
@Table(name="T_ITEM")
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
private Set<ItemHike> itemHikes = new HashSet<ItemHike>(0);
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.item", cascade=CascadeType.ALL)
public Set<ItemHike> getItemHikes() {
return this.itemHikes;
}
public void setItemHikes(Set<ItemHike> itemHikes) {
this.itemHikes = itemHikes;
}
}
類遠足
@Entity
@Table(name="T_HIKE")
public class Hike implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
private List<ItemHike> itemHikes = new ArrayList<ItemHike>(0);
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.hike", cascade=CascadeType.ALL)
public List<ItemHike> getItemHikes() {
return this.itemHikes;
}
public void setItemHikes(List<ItemHike> itemHikes) {
this.itemHikes = itemHikes;
}
}
類ItemHike
@Entity
@Table(name="T_ITEM_HIKE")
@AssociationOverrides({
@AssociationOverride(name = "pk.item",
joinColumns = @JoinColumn(name = "iditem")),
@AssociationOverride(name = "pk.hike",
joinColumns = @JoinColumn(name = "idlist")) })
public class ItemHike implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private ItemHikePK pk;
private int quantity;
//bi-directional many-to-one association to THike
@Transient
public Item getItem() {
return getPk().getItem();
}
public void setItem(Item item) {
getPk().setItem(item);
}
@Transient
public Hike getHike() {
return getPk().getHike();
}
public void setHike(Hike hike) {
getPk().setHike(hike);
}
public ItemHike() {
}
public ItemHikePK getPk() {
return this.pk;
}
public void setPk(ItemHikePK pk) {
this.pk = pk;
}
public int getQuantity() {
return this.quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
類ItemHikePk
@Embeddable
public class ItemHikePK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
private Item item;
private Hike hike;
@ManyToOne
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
@ManyToOne
public Hike getHike() {
return hike;
}
public void setHike(Hike hike) {
this.hike = hike;
}
}
謝謝您的回答,我沒有找到stakoverflow了類似的回答。它正在工作。但在運行時,我得到一個com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'字段列表'中的未知列'itemhikes1_.item'。其實,這個領域提到的是成語,而不是'項目'。它由pk.item引用。 – user1260928 2014-10-31 21:36:07
你說你的MySql表中的字段是'成語',那麼當你在'@AssociationOverride'中聲明'@JoinColumn'時,你應該輸入正確的字段名,現在你有'@JoinColumn(name =「iditem 「)',你應該有:'@JoinColumn(name =」idiom「)'。 – 2014-11-01 07:57:03
對不起,我的平板電腦將iditem這個詞變成了成語,我沒有看到它。該領域是iditem。這就是爲什麼我不明白爲什麼它不能看到這個領域。 – user1260928 2014-11-01 09:22:36