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

标签 java spring hibernate spring-boot jpa

我有一个多对多关系的问题。
这些表是:
成分和营养值(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/

相关文章:

java对象转json字符串构建jqmobile嵌套列表

java - 出站Http端点的Spring集成测试

java - 如何以毫秒为单位设置带有数据 JPA 事务性的 Spring boot

java - JPA级联删除: Setting child FK to NULL on a NOT NULL column

spring - 在spring + hibernate中保存creationTimestamp和updatedTime

java - 将电源集初始化为匿名接口(interface)

java - 如何使用 XPath 通过另一个属性的值过滤元素?

java - org.thymeleaf.exceptions.TemplateInputException : Exception parsing document: template ="login", 第 36 行 - 第 3 列

java - @Value 注解不从属性文件中注入(inject)值

java - 将 Oracle 10g 数据库迁移到 Microsoft SQL 2008 R2 的最佳实践是什么?应用程序正在使用 Hibernate