basic”属性类型不应为“persistence entity”

sc4hvdpw  于 2021-07-16  发布在  Java
关注(0)|答案(1)|浏览(403)

我正在使用springboot创建restapi。我有一个“组件”类,如下所示:

@Entity
public class Component {
   @Id @GeneratedValue
   private Long id;
   private String name;
   private String unit;
   private double quantity;

   public Component(){

   }

   public Component(Long id, String name, String unit, double quantity){
       this.id = id;
       this.name = name;
       this.unit = unit;
       this.quantity = quantity;
   }

  //getters
  //setters

我有一个由“组件”组成的产品类:

@Entity
public class Product{
    @Id @GeneratedValue
    private Long id;
    private String name;
    private Component productComponent;

}

下面是我得到的错误:它突出显示了product类中的“private component productcomponent”行,并说了以下内容:
“basic”属性类型不应为“persistence entity”
这种错误背后的原因是什么?

iyr7buue

iyr7buue1#

我通过添加@manytoone和@joincolumn修复了上述问题:

@Entity
public class Product{
    @Id @GeneratedValue
    private Long id;
    private String name;
    @ManyToOne
    @JoinColumn
    private Component productComponent;
}

相关问题