外键问题,显示非法尝试将非集合映射为@OneToMany,@ ManyToMany或@CollectionOfElements错误

时间:2017-08-17 16:34:19

标签: java spring eclipse foreign-keys

@Entity
 @Table(name="ITEMS")
 public class ItemsEntity {

        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
    @OneToMany(targetEntity=AddOnEntity.class,mappedBy="primaryItem",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
    @Column(name="ITEM_ID")
    private Integer itemId;

    @Column(name="ITEM_NAME")
    private String itemName;

    @Column(name="ITEM_PRICE")
    private Double itemPrice;

    public Integer getItemId() {
        return itemId;
    }

    public void setItemId(Integer itemId) {
        this.itemId = itemId;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public Double getPrice() {
        return itemPrice;
    }

    public void setPrice(Double price) {
        this.itemPrice = price;
    }   

}

这里我试图将itemId作为AddOn表的外键,但我得非法尝试将非集合映射为@OneToMany,@ ManyToMany或@CollectionOfElements。请任何人都能告诉我我做错了什么。

@Entity
@Table(name="ADD_ON_ITEMS")
public class AddOnEntity {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="ADD_ON_ID")
    private Integer addOnId;

    @Column(name="ADD_ON_NAME")
    private String addOnName;

    @Column(name="ADD_ON_PRICE")
    private Double addOnPrice;

    @ManyToOne
    @JoinColumn(name="PRIMARY_ITEM")
    private ItemsEntity primaryItem;

    public Integer getAddOnId() {
        return addOnId;
    }

    public void setAddOnId(Integer addOnId) {
        this.addOnId = addOnId;
    }

    public String getAddOnName() {
        return addOnName;
    }

    public void setAddOnName(String addOnName) {
        this.addOnName = addOnName;
    }

    public Double getAddOnprice() {
        return addOnPrice;
    }

    public void setAddOnprice(Double addOnprice) {
        this.addOnPrice = addOnprice;
    }

    public ItemsEntity getPrimaryItem() {
        return primaryItem;
    }

    public void setPrimaryItem(ItemsEntity primaryItem) {
        this.primaryItem = primaryItem;
    }


}

如果我完全错了,请提供正确的解决方案 我参考了https://www.youtube.com/watch?v=PIg6F9TayOk

1 个答案:

答案 0 :(得分:1)

ItemsEntity:

@Entity
@Table(name="ITEMS")
public class ItemsEntity {

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   @Column(name="ITEM_ID")
   private Integer itemId;

   @Column(name="ITEM_NAME")
   private String itemName;

   @Column(name="ITEM_PRICE")
   private Double itemPrice;


   @OneToMany(mappedBy="primaryItem",cascade=CascadeType.ALL)   
   private List<AddOnEntity> entity;

   public Integer getItemId() {
       return itemId;
   }

   public void setItemId(Integer itemId) {
       this.itemId = itemId;
   }

   public String getItemName() {
       return itemName;
   }

   public void setItemName(String itemName) {
       this.itemName = itemName;
   }

   public Double getPrice() {
       return itemPrice;
   }

   public void setPrice(Double price) {
       this.itemPrice = price;
   }   

}

AddOnEntity:

@Entity
@Table(name="ADD_ON_ITEMS")
public class AddOnEntity {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="ADD_ON_ID")
    private Integer addOnId;

    @Column(name="ADD_ON_NAME")
    private String addOnName;

    @Column(name="ADD_ON_PRICE")
    private Double addOnPrice;

    @ManyToOne
    @JoinColumn(name="ITEM_ID")
    private ItemsEntity primaryItem;

    public Integer getAddOnId() {
        return addOnId;
    }

    public void setAddOnId(Integer addOnId) {
        this.addOnId = addOnId;
    }

    public String getAddOnName() {
        return addOnName;
    }

    public void setAddOnName(String addOnName) {
        this.addOnName = addOnName;
    }

    public Double getAddOnprice() {
        return addOnPrice;
    }

    public void setAddOnprice(Double addOnprice) {
        this.addOnPrice = addOnprice;
    }

    public ItemsEntity getPrimaryItem() {
        return primaryItem;
    }

    public void setPrimaryItem(ItemsEntity primaryItem) {
        this.primaryItem = primaryItem;
    }


}

One Items实体有很多AddOnEntites:

许多AddOnEntity属于一个ItemsEntity。