2014-10-31 64 views
0

我試圖建立一個多基於這個例子與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; 
    } 
} 

回答

2

嘗試將@OneToMany註釋從getItemHikes()移動到itemHikes字段。如果您希望註釋有時位於字段上,有時位於getter上,則需要使用@Access註釋。如果您不使用它,則所有註釋都需要在字段或獲取者上,但不能混合使用。

5

Here is a similiar question !

你設置的字段@Anottations太上吸氣劑,這是錯誤的,你必須穿上字段或上吸氣劑方法的註釋。

錯誤方式:

@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; 
    } 
} 
+0

謝謝您的回答,我沒有找到stakoverflow了類似的回答。它正在工作。但在運行時,我得到一個com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'字段列表'中的未知列'itemhikes1_.item'。其實,這個領域提到的是成語,而不是'項目'。它由pk.item引用。 – user1260928 2014-10-31 21:36:07

+0

你說你的MySql表中的字段是'成語',那麼當你在'@AssociationOverride'中聲明'@JoinColumn'時,你應該輸入正確的字段名,現在你有'@JoinColumn(name =「iditem 「)',你應該有:'@JoinColumn(name =」idiom「)'。 – 2014-11-01 07:57:03

+0

對不起,我的平板電腦將iditem這個詞變成了成語,我沒有看到它。該領域是iditem。這就是爲什麼我不明白爲什麼它不能看到這個領域。 – user1260928 2014-11-01 09:22:36

相關問題