Spring 引导 - 'One To Many' 属性值类型不应为错误

Spring boot - 'One To Many' attribute value type should not be Error

我在 Spring 引导中有我的 api。

我有 2 个 classes:

这是我的供应商 Class:

 public class Supplier{
     @OneToMany(cascade= {CascadeType.ALL})
     private List<Product> products;      <---  This one is working perfectly fine.

     @OneToMany(cascade= {CascadeType.ALL})
     private List<Ingredient> components;      <---- this is the line where I am getting an error

}

这是我收到的错误消息:

'One To Many' attribute value type should not be 'Ingredient'

这是我的配料class

public class Ingredient {

     @Id
     @GeneratedValue
     private Long id;
     private String name;
     private String unit;
     private Double quantity = 0.0;
     private Double cost = 0.0;
     private Double price = 0.0;
}

我的问题: 上述错误的可能修复方法是:

private List<Ingredient> components;

即使上面的行有效?

如果你有单向关联@OneToMany你需要替换

@OneToMany(cascade= {CascadeType.ALL})
private List<Ingredient> components; 

@OneToMany(cascade= {CascadeType.ALL})  
@JoinColumn(name="components")
private List<Ingredient> components; 

如果你想要一个双向关联 @OneToMany 你必须添加你的 Ingredient class

...
@ManyToOne
@JoinColumn
private Supplier supplier;

和供应商class没有变化 帮助您理解的链接

https://javabydeveloper.com/one-many-unidirectional-association/

https://javabydeveloper.com/one-to-many-bidirectional-association/

如果有帮助请告诉我!

确保您正在注释您的类型 class,在您的情况下是成分。它应该用@Entity 注释。缺少注释你的 class 是产生此错误的原因之一。