Java 无法通过反射设置字段值
我的多对多关系有问题。 各表如下: 成分和坚果价值。 我创建了两个实体之间的关系表,其中有成分和营养价值的外部键(构成复合键)和一些属性 类别Java 无法通过反射设置字段值,java,spring,hibernate,spring-boot,jpa,Java,Spring,Hibernate,Spring Boot,Jpa,我的多对多关系有问题。 各表如下: 成分和坚果价值。 我创建了两个实体之间的关系表,其中有成分和营养价值的外部键(构成复合键)和一些属性 类别联合营养价值成分: import lombok.Data; import javax.persistence.*; import javax.validation.constraints.NotNull; import java.io.Serializable; @Entity @Data public class JoinedNutrionalValu
联合营养价值成分:
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;
}
类别NutrionalValueingCreditID
:
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
,但我有相同的错误
类别营养价值
:
@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>();
}
更改变量名,如下所示
长成分id到长成分id
营养价值id至营养价值id
范例
@Column(name = "ingredient_id")
Long ingredient_id;
到
然后为所有字段生成getter setter。Hibernate无法设置字段,因为没有公共getter/setter。为id实体“NutrionalValueingRedentid”设置值,然后尝试将其作为“JoinedNutrionalValueComponent”的嵌入id插入。
请参阅以下示例:
package com.example;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class EmployeeId implements Serializable
{
private static final long serialVersionUID = 1L;
@Column(name = "EMP_ID")
private int empId;
@Column(name = "DEPARTMENT")
private String department;
public EmployeeId()
{
super();
}
public EmployeeId(int empId, String department)
{
super();
this.empId = empId;
this.department = department;
}
public int getEmpId()
{
return empId;
}
public void setEmpId(int empId)
{
this.empId = empId;
}
public String getDepartment()
{
return department;
}
public void setDepartment(String department)
{
this.department = department;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((department == null) ? 0 : department.hashCode());
result = prime * result + empId;
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
EmployeeId other = (EmployeeId) obj;
if (department == null)
{
if (other.department != null)
return false;
} else if (!department.equals(other.department))
return false;
if (empId != other.empId)
return false;
return true;
}
}
请参阅上面的嵌入式Id类(EmployeeId)。这是Employee类的主键。
因此,我们需要在Id类(EmployeeId)中设置值,然后将该Id类作为主键注入Employee。
那就行了。如果没有主键,则该值为空
package com.example;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Employee implements Serializable
{
private static final long serialVersionUID = 1L;
@EmbeddedId
EmployeeId id;
@Column(name="EMP_NAME")
private String empName;
public Employee()
{
super();
}
public Employee(EmployeeId id, String empName)
{
super();
this.id = id;
this.empName = empName;
}
public EmployeeId getId()
{
return id;
}
public void setId(EmployeeId id)
{
this.id = id;
}
public String getEmpName()
{
return empName;
}
public void setEmpName(String empName)
{
this.empName = empName;
}
}
设置Id类和其他字段的值,如下所示
//Create a new Employee object
Employee employee = new Employee();
EmployeeId employeeId = new EmployeeId(1,"DailyNews");
employee.setEmpName("CompositeKey");
employee.setId(employeeId);
session.save(employee);
检查您是否正在为嵌入式id设置实例。我没有设置,因此出现此错误
我改变这个
@EmbeddedId
ProductUserId id;
ProductUser(Integer productId, Integer UserId, Double rating){
this.rating = rating
}
到
您的NutrionalValueingRedentid字段受包保护,您需要将它们设置为私有,并添加getter/setter(手动(例如使用ide生成器))或使用lombok。我尝试过,但也遇到了相同的错误。还有一件事,您忘记添加所需的默认构造函数。你能分享营养价值吗?谢谢你的回复。我更改了变量的名称,现在我收到了另一个错误。使用spring数据rest后,crud操作自动生成。然而,向我们提出的解决方案将迫使我重写post方法。我想做的是直接从rest调用插入这个表@洛希妮
//Create a new Employee object
Employee employee = new Employee();
EmployeeId employeeId = new EmployeeId(1,"DailyNews");
employee.setEmpName("CompositeKey");
employee.setId(employeeId);
session.save(employee);
@EmbeddedId
ProductUserId id;
ProductUser(Integer productId, Integer UserId, Double rating){
this.rating = rating
}
@EmbeddedId
ProductUserId id;
ProductUser(Integer productId, Integer userId, Double rating){
this.id = new ProductUserId(productId, userId);
this.rating = rating
}