gpt4 book ai didi

java - Spring Boot JPA 保留具有空列的父级的子级

转载 作者:行者123 更新时间:2023-12-01 18:20:56 25 4
gpt4 key购买 nike

我有一个奇怪的问题。我有实体公司、分支机构和地址。公司有分支机构列表,每个分支机构都有地址。我试图保留地址实体之前不存在的分支,但地址保留为空列。

@Data
@Entity
@Indexed
@Table(name = "company")
public class Company {

@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(
name = "UUID",
strategy = "org.hibernate.id.UUIDGenerator"
)
private String id;

@Field
@Column(name = "full_name", nullable = false, unique = true)
private String fullName;

@OneToMany(mappedBy = "company")
private Set<Branch> branches;

}
@Data
@Entity
@Table(name = "branch")
public class Branch {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@Column
private String phone;

@Column
private String email;

@OneToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "address_id", referencedColumnName = "id")
private Address address;

@ManyToOne
@JoinColumn(name = "company_id", referencedColumnName = "id")
private Company company;
}
@Entity
@Table(name = "address")
public class Address {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@Column
private String street;


@OneToOne(mappedBy = "address")
private Company company;

@OneToOne(mappedBy = "address")
private Branch branch;
}

服务...

    public Integer addBranch(BranchDto branchDto) {
Branch branch = modelMapper.map(branchDto, Branch.class);

Company company = companyRepository.getCompanyById(branchDto.getCompanyId());
branch.setCompany(company);

return branchRepository.save(branch).getId();
}

D 到...

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BranchDto {

private Integer id;

private String phone;

private String email;

private AddressDto address;

private String companyId;
}

这就是效果...

enter image description here

调试器...

enter image description here

有什么问题吗?你能帮我吗?

最佳答案

您正在使用具有相同 ID 的“mappedBy”,它仅适用于他找到的第一个。

尝试更换其他id。

关于java - Spring Boot JPA 保留具有空列的父级的子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60291520/

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