[英]many to many relationship : org.hibernate.MappingException: Could not determine type for: java.util.Set
I'm trying to set up a many to many bidirectional relationship with JPA based on this example : http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/ 我正在尝试根据此示例与JPA建立多对多双向关系: http : //www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-注解/
I m getting a 我得到一个
org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: T_ITEM, for columns: [org.hibernate.mapping.Column(itemHikes)]
org.hibernate.MappingException:无法确定类型:java.util.Set,在表:T_ITEM,用于列:[org.hibernate.mapping.Column(itemHikes)]
If anyone can help me on that, here is the source code below: 如果有人可以帮助我,请参见以下源代码:
Class Item 类项目
@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;
}
}
Class Hike 班级远足
@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;
}
}
Here is a similiar question ! 这是一个类似的问题!
You're setting @Anottations on Fields and too on Getters, that's wrong, You must put annotations on Fields OR on Getters Methods. 您要在Fields上以及Getters上设置@Anottations,这是错误的,您必须在Fields或Getters方法上放置注释。
Wrong way: 错误的方法:
@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;
}
}
Correct: 正确:
@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;
}
}
Try moving the @OneToMany annotation from getItemHikes()
to the itemHikes
field. 尝试将@OneToMany注释从
getItemHikes()
移至itemHikes
字段。 If you want your annotations to be sometimes on the fields and sometimes on the getters, you need to use the @Access annotation. 如果您希望注释有时出现在字段中,有时出现在getter上,则需要使用@Access注释。 If you don't use it, all annotations need to be either on the fields or on the getters, but not a mix of both.
如果您不使用它,则所有注释都必须在字段上或在吸气剂上,但不能同时使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.