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

many to many relationship : org.hibernate.MappingException: Could not determine type for: java.util.Set

我正试图基于以下示例与JPA建立多对多双向关系:http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/

我得到一个

org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: T_ITEM, for columns: [org.hibernate.mapping.Column(itemHikes)]

如果有人能帮我的话,下面是源代码:

类项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@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;
    }
}

班级远足

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@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;
    }
}

课程项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@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;
    }
}


这是一个类似的问题!

您在字段和getter上设置了@anotations,这是错误的,您必须在字段或getter方法上放置注释。

错误的方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@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;
    }
}

对的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@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;
    }
}


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