gpt4 book ai didi

java - 无法通过反射设置字段值

转载 作者:行者123 更新时间:2023-12-03 23:53:40 29 4
gpt4 key购买 nike

我有一个多对多关系的问题。
这些表是:
成分和营养值(value)。
我创建了两个实体之间的关系表,其中有成分和营养值(value)的外部键(形成复合键)和一些属性。
类(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;
}
类(class) 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);
}
}
当我尝试在关系表中插入一个新字段时,我收到此错误:
{
"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 ,但我有同样的错误。
类(class) 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的回答之后,这个错误就出来了。
{
"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"
}

最佳答案

 @Entity @Data
public class JoinedNutrionalValueIngredient implements Serializable {

@EmbeddedId
private NutrionalValueIngredientId id = new NutrionalValueIngredientId();

// ... rest of class
}
JoinedNutrionalValueIngredient 类化合物是 NutrionalValueIngredientId 应实例化。

关于java - 无法通过反射设置字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53381067/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com