Hibernate单向OneToMany级联插入失败

问题描述 投票:0回答:1

拥有 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) 值 (?,?,?,?)]

我的映射出了什么问题?

hibernate jpa spring-data-jpa
1个回答
0
投票

您需要将

moduleId
中的
PropertyEntity
属性替换为
module
属性

@Id
@ManyToOne
@JoinColumn(name = "module_id")
private ModuleEntity module;

请参阅 Hibernate ORM 用户指南

© www.soinside.com 2019 - 2024. All rights reserved.