我无法设法使这个简单的外键约束起作用。
@Entity
@Table(name = "wheel")
@Data
public class WheelEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
...
@Entity
@Table(name = "bike")
@Data
public class BikeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne(targetEntity = WheelEntity.class, fetch=FetchType.EAGER, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
@JoinColumn(name = "wheel_1_id", referencedColumnName = "id")
@NotNull
private Integer wheel_1_id;
@ManyToOne(targetEntity = WheelEntity.class, fetch=FetchType.EAGER, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
@JoinColumn(name = "wheel_2_id", referencedColumnName = "id")
@NotNull
private Integer wheel_2_id;
...
当我想插入一辆新自行车时,这是我在持续存在BikeEntity时遇到的错误:
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Integer field com.xxx.xxx.xxx.storage.WheelEntity.id to java.lang.Integer
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
at java.lang.reflect.Field.get(Field.java:393)
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:39)
无法将java.lang.Integer设置为java.lang.Integer。 MMMMMM?奇怪!
非常感谢您的帮助