繁体   English   中英

Spring 引导 - “一对多”属性值类型不应为错误

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

我在 Spring Boot 中有我的 api。

我有 2 节课:

这是我的供应商 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

}

这是我收到的错误消息:

“一对多”属性值类型不应为“成分”

这是我的配料 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 是产生此错误的原因之一。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM