多对多关系:org.hibernate.MappingException:无法确定类型:java.util.Set

Posted

技术标签:

【中文标题】多对多关系:org.hibernate.MappingException:无法确定类型:java.util.Set【英文标题】:many to many relationship : org.hibernate.MappingException: Could not determine type for: java.util.Set 【发布时间】:2014-10-31 14:47:07 【问题描述】:

我正在尝试基于此示例与 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;
    

类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;
    

【问题讨论】:

【参考方案1】:

Here is a similiar question !

您在 Fields 和 Getters 上设置了 @Anottations,这是错误的,您必须在 Fields 或 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;
    

【讨论】:

感谢您的回答,我在 stakoverflow 上没有找到类似的答案。它正在工作。但在运行时我得到一个 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'itemhikes1_.item' in 'field list'。实际上,提到的领域是成语,而不是“项目”。它被 pk.item 引用。 你说你的MySql表中的字段是'idiom',那么当你在'@AssociationOverride'中声明'@JoinColumn'时你应该输入正确的字段名,现在你有:' @JoinColumn(name = "iditem")',你应该有:'@JoinColumn(name = "idiom")'。 抱歉,我的平板电脑将单词 idiem 转换为 idiom,我没看到。该字段是同上的。这就是为什么我不明白为什么它看不到这个字段。 哦,我明白了,您的访问策略又遇到了问题,在您的 ItemHike 类中,您混合了字段访问和 getter 属性访问;您在私有 ItemHikePK pk 上有“@EmbeddedId”,然后将“@Transient”放在 getter 上。【参考方案2】:

尝试将@OneToMany 注释从getItemHikes() 移动到itemHikes 字段。如果您希望注释有时在字段上,有时在 getter 上,则需要使用 @Access 注释。如果您不使用它,则所有注释都需要在字段上或在 getter 上,但不能同时在两者上。

【讨论】:

以上是关于多对多关系:org.hibernate.MappingException:无法确定类型:java.util.Set的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate多对多关系映射(建表)

(转)Hibernate框架基础——多对多关联关系映射

Laravel5.1 模型 --多对多关系

多对多关系

雄辩的多对多对多 - 如何轻松加载远距离关系

Hibernate的多对多关联关系