拥有 2 个实体:
带有属性列表的模块实体
@Entity
@Table(name = "module")
public class ModuleEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "module_id", nullable = false)
private List<PropertyEntity> properties;
//getters/setters etc
}
具有复合 ID 的属性实体
@Entity
@IdClass(PropertyEntityId.class)
@Table(name = "module_property")
public class PropertyEntity {
@Id
@Column(name = "module_id")
private Integer moduleId;
@Id
@Column(name = "[key]")
private String key;
@Column(name = "value")
private String value;
//getters/setters etc
}
有对应ID
public class PropertyEntityId implements Serializable {
private Integer moduleId;
private String key;
//getters/setters etc
}
问题是,当我尝试插入带有属性列表的模块时,JPA 尝试插入 module_id 两次,在这种情况下第二个值为空
GenericJDBCException: 无法执行语句 [未设置参数号 4 的值。] [插入 module_property (module_id,value,[key],module_id) 值 (?,?,?,?)]
我的映射出了什么问题?
您需要将
moduleId
中的 PropertyEntity
属性替换为 module
属性
@Id
@ManyToOne
@JoinColumn(name = "module_id")
private ModuleEntity module;
请参阅 Hibernate ORM 用户指南。