gpt4 book ai didi

java - hibernate ORM : Saving Parent Entity Saves the Children too?

转载 作者:行者123 更新时间:2023-11-30 10:46:01 24 4
gpt4 key购买 nike

我将以下 JSON 作为输入:

{
"type": "Student",
"numOfPeople": "1",
"tenantMembers": [
{
"firstName": "Chris",
"lastName": "C"
}
],
"tenantDetails": {
"firstName": "John",
"lastName": "J",
"email" "xyz@gmail.com"
}
}

我想用它来做一个保存:

tenantRepo.save(tenant);

这应该保存父级“Tenant”和子级“TenantMembers”和“TenantDetails”。

但是当我这样做时, children 的 'tenant_id' 为 NULL。 (如果我在数据库中有外键,则给出 'tenant_id' can't be null constraint exception)

我的问题是:这在 Hibernate 中可能吗?

我的模型:

父类:

@Entity
@Table(name = "tenant")
public class Tenant {

@GeneratedValue
@Id
private Long id;

private String type;

@Column(name = "num_of_people")
private String numOfPeople;

@OneToMany(mappedBy = "tenant", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<TenantMember> tenantMembers;

@OneToOne(mappedBy = "tenant", cascade = CascadeType.ALL)
private TenantDetails tenantDetails;

TenantMember 子类:

@Entity
@Table(name = "tenant_member")
public class TenantMember {
@GeneratedValue
@Id
private Long id;

@ManyToOne
@JoinColumn(name = "tenant_id")
private Tenant tenant;

@Column(name = "first_name")
private String firstName;

@Column(name = "last_name")
private String lastName;

TenanatDetails 子类:

@Entity
@Table(name="tenant_details")
public class TenantDetails {
@GeneratedValue
@Id
private Long id;

@OneToOne
@JoinColumn(name = "tenant_id")
private Tenant tenant;

@Column(name = "first_name")
private String firstName;

@Column(name = "last_name")
private String lastName;

private String email;

编辑:

按照 Dragan Bozanovic 的建议,尝试使用@JsonIdentityInfo对于三个表:

@Entity
@Table(name = "tenant")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Tenant {


@Entity
@Table(name="tenant_details")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class TenantDetails {


@Entity
@Table(name = "tenant_member")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class TenantMember {

并做了以下保存:

@RequestMapping(value = "/set", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Tenant test(@RequestBody Tenant tenant) {
Tenant t = new Tenant();
t.setType(tenant.getType());
t.setNumOfPeople(tenant.getNumOfPeople());
tenantRepo.save(t);
tenant.setId(t.getId());
tenant.getTenantDetails().setTenant(tenant);
for(TenantMember member: tenant.getTenantMembers()) {
member.setTenant(tenant);
}
return tenantRepo.save(tenant);
}

这是可能的最佳方法吗?

最佳答案

由于您指定的级联选项,Hibernate 确实会保存子项(因此会违反约束),但它不会保存您的情况下的关系信息(连接列值)。

TenantMemberTenantDetails 是与 Tenant 关联的所有者(关联注释中的 mappedBy 属性租户)。

这意味着您必须正确更新 TenantMemberTenantDetails 实例中的 tenant 字段,因为 Hibernate 会忽略关联的反面在维持关系时。

关于java - hibernate ORM : Saving Parent Entity Saves the Children too?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36776939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com