当我尝试使用复合键时保存实体时出现异常。这是我尝试保存的例外,模型和存储库。有人可以帮忙吗?
org.springframework.beans.TypeMismatchException:无法转换 'org.CheckType _ $$ _ jvst96c_4'类型的属性值为必需类型 'int'表示属性'checkType';嵌套异常是 java.lang.IllegalArgumentException:无法转换类型的值 'org.CheckType _ $$ _ jvst96c_4'到属性所需的类型'int' 'checkType':PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] 返回类型'org.CheckType _ $$ _ jvst96c_4'
的不适当值
@Entity
public class CheckType {
@Id
private int id;
@NotNull
private String name;
@OneToMany(mappedBy = "checkType")
private List<ClientCheck> checks;
//getters and setters
}
@Entity
public class Offer {
@Id
@Column(name = "offerid")
private Integer offerId;
//...
@OneToMany(mappedBy = "offer")
private List<ClientCheck> clientCheckList;
//getters and setters
}
@Entity
@IdClass(ClientCheckId.class)
public class ClientCheck {
@Id
@ManyToOne
@JoinColumn(name = "offer_id", referencedColumnName = "offerid")
private Offer offer;
@Id
@ManyToOne
@JoinColumn(name = "check_type_id", referencedColumnName = "id")
private CheckType checkType;
@Id
private Date timestamp;
private String result;
//getters and setters
}
public class ClientCheckId implements Serializable {
private int offer;
private int checkType;
private Date timestamp;
//getters and setters
//equals and hashCode
}
public interface ClientCheckRepo extends JpaRepository<ClientCheck, ClientCheckId> {
}
答案 0 :(得分:0)
如果另一个实体是复合键的一部分,则必须在ID类中声明该实体,而不是其ID:
public class ClientCheckId implements Serializable {
private int offer;
private CheckType checkType;
private Date timestamp;
//getters and setters
//equals and hashCode
}
如果您想了解Hibernate的工作原理,则错误消息非常易读:org.CheckType_$$_jvst96c_4
是您的类的丰富版本,Hibernate试图通过反射将checkType
字段分配给ClientCheckId
字段,由于类型不兼容而失败。