JPA的问题很多,连接表中有额外的列

时间:2014-04-03 17:21:50

标签: hibernate jpa many-to-many

我正在尝试为连接表中的额外列设置多对多关系的JPA映射。我的模型看起来像这样: Hikelist(id),Item(id),ItemList(idList,itItem,quantity)

我的映射:

项目:

@Entity
@Table(name = "T_ITEM")
public class Item implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    private Collection<Itemlist> setOfItemHikelist = new ArrayList<Itemlist>();

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.item", cascade=CascadeType.ALL)
    public Collection<Itemlist> getSetOfItemHikelist() {
        return setOfItemHikelist;
    }

    public void setSetOfItemHikelist(Collection<Itemlist> setOfItemHikelist) {
        this.setOfItemHikelist = setOfItemHikelist;
    }
}

ITEMLIST:

@Entity
@Table(name = "T_ITEM_LIST")
@AssociationOverrides({
        @AssociationOverride(name = "pk.item", 
            joinColumns = @JoinColumn(name = "iditem")),
        @AssociationOverride(name = "pk.hikelist", 
            joinColumns = @JoinColumn(name = "idlist")) })
public class Itemlist implements Serializable {

    private ItemListId pk = new ItemListId();

    @EmbeddedId
    public ItemListId getPk() {
        return pk;
    }

    public void setPk(ItemListId pk) {
        this.pk = pk;
    }   

    @Transient
    public Item getItem() {
        return getPk().getItem();
    }

    public void setStock(Item item) {
        getPk().setItem(item);
    }

    @Transient
    public Hikelist getHikelist() {
        return getPk().getHikelist();
    }

    public void setCategory(Hikelist hikelist) {
        getPk().setHikelist(hikelist);
    }

    private String quantity;

    public String getQuantity() {
        return quantity;
    }

    public void setQuantity(String quantity) {
        this.quantity = quantity;
    } 
}

ItemListId:

@Embeddable
public class ItemListId implements java.io.Serializable{

    private Item item;
    private Hikelist hikelist;

    @ManyToOne
    public Item getItem() {
        return item;
    }
    public void setItem(Item item) {
        this.item = item;
    }

    @ManyToOne
    public Hikelist getHikelist() {
        return hikelist;
    }
    public void setHikelist(Hikelist hikelist) {
        this.hikelist = hikelist;
    }
}

我收到此错误:

  

引起:org.hibernate.MappingException:无法确定类型   for:java.util.Collection,at table:T_HIKELIST,for columns:   [org.hibernate.mapping.Column(setOfItemHikelist)] at   org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:336)at at   org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:310)at at   org.hibernate.mapping.Property.isValid(Property.java:241)at   org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:496)     在org.hibernate.mapping.RootClass.validate(RootClass.java:270)at   org.hibernate.cfg.Configuration.validate(Configuration.java:1358)at   org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1849)     在   org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl $ 4.perform(EntityManagerFactoryBuilderImpl.java:850)

如果有人可以帮助我...谢谢!

1 个答案:

答案 0 :(得分:0)

我认为你正在混合AccesType,在你使用FIELDS和PROPERTIES中的注释的类Item中,考虑这样做。

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.item", cascade=CascadeType.ALL)
private Collection<Itemlist> setOfItemHikelist = new ArrayList<Itemlist>();

public Collection<Itemlist> getSetOfItemHikelist() {
    return setOfItemHikelist;
}

将注释放在字段上,就像在同一类中的Id中所做的那样