我正在尝试根据此示例与JPA建立多对多的双向关系:http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/
我得到了
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;
}
}
Class 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;
}
}
Class 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;
}
}
答案 0 :(得分:5)
您在字段上设置@Anottations,在Getters上也是如此,这是错误的,您必须在字段上或在Getters方法上添加注释。
错误的方式:
@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_ITEM")
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.item", cascade=CascadeType.ALL)
private Set<ItemHike> itemHikes = new HashSet<ItemHike>(0);
public Set<ItemHike> getItemHikes() {
return this.itemHikes;
}
public void setItemHikes(Set<ItemHike> itemHikes) {
this.itemHikes = itemHikes;
}
}
答案 1 :(得分:2)
尝试将@OneToMany注释从getItemHikes()
移至itemHikes
字段。如果您希望注释有时在字段上,有时在getter上,则需要使用@Access注释。如果你不使用它,所有注释都需要在字段上或在getter上,而不是两者的混合。