无法通过反射设置字段值 [英] Could not set field value by reflection
问题描述
我与多对多关系有问题.这些表是:成分和营养价值.我创建了两个实体之间的关系表,其中有成分和营养价值的外键(形成复合键)以及一些属性.
I have a problem with a many to many relationship. The tables are: ingredient and nutional value. I created a table of relations between the 2 entities, in which there are the external keys of ingredient and nutrional value (which form the compound key) and some attributes.
Class JoinedNutrionalValueIngredient
:
import lombok.Data;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
@Entity @Data
public class JoinedNutrionalValueIngredient implements Serializable {
@EmbeddedId
private NutrionalValueIngredientId id;
@ManyToOne(fetch= FetchType.LAZY)
@MapsId("ingredient_id")
private Ingredient ingredient;
@ManyToOne(fetch=FetchType.LAZY)
@MapsId("nutrional_value_id")
private NutrionalValue nutrionalValue;
@NotNull
String matrixUnit;
@NotNull
int value;
@NotNull
String valueType;
}
类 NutrionalValueIngredientId
:
import javax.persistence.*;
import java.io.Serializable;
import java.util.Objects;
@Embeddable
@Getter
@Setter
public class NutrionalValueIngredientId implements Serializable{
@Column(name = "ingredient_id")
private Long ingredient_id;
@Column(name = "nutrional_value_id")
private Long nutrional_value_id;
public NutrionalValueIngredientId() {
}
public NutrionalValueIngredientId(Long ingredient, Long nutrionalValue){
this.ingredient_id=ingredient;
this.nutrional_value_id=nutrionalValue;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass())
return false;
NutrionalValueIngredientId that = (NutrionalValueIngredientId) o;
return Objects.equals(ingredient_id, that.ingredient_id) &&
Objects.equals(nutrional_value_id, that.nutrional_value_id);
}
@Override
public int hashCode() {
return Objects.hash(ingredient_id, nutrional_value_id);
}
}
当我尝试在关系表中插入新字段时,出现此错误:
When I try to insert a new field inside the relationship table I get this error:
{
"timestamp": 1542653896247,
"status": 500,
"error": "Internal Server Error",
"message": "Could not set field value [1] value by reflection : [class com.whateat.reciper.model.NutrionalValueIngredientId.ingredient_id] setter of com.whateat.reciper.model.NutrionalValueIngredientId.ingredient_id; nested exception is org.hibernate.PropertyAccessException: Could not set field value [1] value by reflection : [class com.whateat.reciper.model.NutrionalValueIngredientId.ingredient_id] setter of com.whateat.reciper.model.NutrionalValueIngredientId.ingredient_id",
"path": "/v1/joinedNutrionalValueIngredients"
}
我添加了构造函数和注释 @Getter
和 @Setter
,但是我有相同的错误.
I added constructor and annotation @Getter
and @Setter
, but I have the same error.
类 NutritionalValue
:
@Data
@Entity
public class NutrionalValue implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private String name;
@NotNull
private String unit;
@NotNull
private String source;
@ManyToOne
@NotNull
@JoinColumn(name = "category_id")
private NutrionalValueCategory category;
@OneToMany(mappedBy = "nutrionalValue")
private Set<JoinedNutrionalValueIngredient> joined = new HashSet<JoinedNutrionalValueIngredient>();
}
在Debopam回答后,出现了此错误.
edit: after Debopam's answer, this error came out.
{
"timestamp": 1542657216244,
"status": 500,
"error": "Internal Server Error",
"message": "null id generated for:class com.whateat.reciper.model.JoinedNutrionalValueIngredient; nested exception is org.hibernate.id.IdentifierGenerationException: null id generated for:class com.whateat.reciper.model.JoinedNutrionalValueIngredient",
"path": "/v1/joinedNutrionalValueIngredients"
}
推荐答案
按如下所示更改变量名称
Change the variable Names as below
长Ingredient_id到Long Ingredientidnutrional_value_id到nutrionalvalueid
Long ingredient_id to Long ingredientid nutrional_value_id to nutrionalvalueid
示例
@Column(name = "ingredient_id")
Long ingredient_id;
到
@Column(name = "ingredient_id")
Long ingredientid;
然后为所有字段生成getter设置程序.Hibernate无法设置字段,因为没有公共获取者/设置者.
这篇关于无法通过反射设置字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!