gpt4 book ai didi

java - Spring JPA 多对多,额外列未更新

转载 作者:行者123 更新时间:2023-12-02 14:06:16 25 4
gpt4 key购买 nike

提案和公司之间存在多对多关系。该关系连同与提案相关的公司职能 ID 一起存储在表 proposal_companies 中。

以下是(我认为的)实体的相关部分:

提案

@Entity
@Table(name="sales.proposals")
public class Proposal implements Serializable {
private static final long serialVersionUID = 1L;

private int proposalID;
private List<ProposalCompany> companies = new ArrayList<>();

public Proposal() {}

@Id
@Column(name="pk_proposalid")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonView({View.ProposalView.class,View.ProposalRevisionView.class})
public int getProposalID() {
return proposalID;
}

public void setProposalID(int proposalID) {
this.proposalID = proposalID;
}

@OneToMany(mappedBy="proposal")
@JsonView(View.ProposalView.class)
public List<ProposalCompany> getCompanies() {
return companies;
}

public void setCompanies(List<ProposalCompany> companies) {
this.companies = companies;
}

public void addCompany(Company company) {
ProposalCompany proposalCompany = new ProposalCompany(this, company);
this.companies.add(proposalCompany);
company.getProposals().add(proposalCompany);
}

public void removeCompany(Company company) {
for (Iterator<ProposalCompany> iterator = companies.iterator(); iterator.hasNext(); ) {
ProposalCompany proposalCompany = iterator.next();

if (proposalCompany.getProposal().equals(this) &&
proposalCompany.getCompany().equals(company)) {
iterator.remove();
proposalCompany.getCompany().getProposals().remove(proposalCompany);
proposalCompany.setProposal(null);
proposalCompany.setCompany(null);
}
}
}
}

公司

@Entity
@Table(name="companies.companies")
public class Company implements Serializable {
private static final long serialVersionUID = 1L;

private int companyID;
private List<ProposalCompany> proposals = new ArrayList<>();

public Company() {}

@Id
@Column(name="pk_companyid")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonView(View.AllCompaniesView.class)
public int getCompanyID() {
return companyID;
}

public void setCompanyID(int companyID) {
this.companyID = companyID;
}

@OneToMany(mappedBy="company")
@JsonView(View.SingleCompanyView.class)
public List<ProposalCompany> getProposals() {
return proposals;
}

public void setProposals(List<ProposalCompany> proposals) {
this.proposals = proposals;
}
}

提案公司

@Entity
@Table(name="sales.proposal_companies")
public class ProposalCompany implements Serializable {
private static final long serialVersionUID = 1L;

private Proposal proposal;
private Company company;
private JobFunction jobFunction;

public ProposalCompany() {}

public ProposalCompany(Proposal proposal, Company company) {
this.proposal = proposal;
this.company = company;
}

@Id
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="fk_proposalid")
@JsonView(View.SingleCompanyView.class)
public Proposal getProposal() {
return proposal;
}

public void setProposal(Proposal proposal) {
this.proposal = proposal;
}

@Id
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="fk_companyid")
@JsonView(View.ProposalView.class)
public Company getCompany() {
return company;
}

public void setCompany(Company company) {
this.company = company;
}

@ManyToOne
@JoinColumn(name="fk_jobfunctionid")
@JsonView(View.ProposalView.class)
public JobFunction getJobFunction() {
return jobFunction;
}

public void setJobFunction(JobFunction jobFunction) {
this.jobFunction = jobFunction;
}

@Override
public String toString() {
return "Proposal: "+(proposal==null ? "NULL" : proposal.toString())+" / Company: "+(company==null ? "NULL" : company.toString())+" / Function: "+(jobFunction==null ? "NULL": jobFunction.toString());
}
}

这对于数据库中的现有条目效果很好。当我从 UI 添加公司,或者甚至尝试更改附加到提案的现有公司的功能时,就会出现问题。 (我使用的是 Angular 5,它将 JSON 格式的 Proposal 对象传递给 Spring。)

当我添加新公司时,出现以下错误:

2018-01-30 09:02:34,187 https-jsse-nio-8493-exec-8 ERROR [/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; 
nested exception is org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.couplingcorp.sales.domain.proposals.ProposalCompany with id Proposal: Proposal ID: 2923 / Company: 10 - Company Name / Function: NULL;
nested exception is javax.persistence.EntityNotFoundException: Unable to find com.couplingcorp.sales.domain.proposals.ProposalCompany with id Proposal: Proposal ID: 2923 / Company: 10 - Company Name / Function: NULL] with root cause

即使这是传入的 JSON:

companies   […]
0 {…}
company {…}
companyID: 10
companyName: Company Name
companyAbbr:
jobFunction {…}
jobFunctionID: 4
jobFunctionName: Sole Handler
jobFunctionDefinition: Company who purchases and uses the product

以下是 Spring 在服务层处理 JSON 的方式:

@Override
public Proposal update(Proposal proposal) throws EntityNotFoundException {
Optional<Proposal> prop = repository.findById(proposal.getProposalID());
if (!prop.isPresent()) {
throw new EntityNotFoundException();
}

// in cases where a ProposalCompany has been added or updated, we need to set up the objects appropriately
proposal.getCompanies().stream()
.filter((pc) -> pc.getProposal()==null)
.forEachOrdered((pc) -> {
LOGGER.debug("The proposal is not set for "+pc.toString());
pc.setProposal(proposal);
LOGGER.debug("The proposal is now set for "+pc.toString());
});

return repository.save(proposal);
}

我可以在日志中看到,在保存提案时,ProposalCompany 对象看起来没问题:

2018-01-30 09:02:34,112 https-jsse-nio-8493-exec-8 DEBUG proposals.ProposalServiceImpl - The proposal is not set for Proposal: NULL / Company: 10 - Company Name / Function: 4: Sole Handler
2018-01-30 09:02:34,112 https-jsse-nio-8493-exec-8 DEBUG proposals.ProposalServiceImpl - The proposal is now set for Proposal: Proposal ID: 2923 / Company: 10 - Company Name / Function: 4: Sole Handler

我错过了什么会导致它找不到该实体,即使它显然拥有所需的所有信息?

我正在使用 Spring Boot 2.0.0.M7 和 Hibernate 5.2.12。

这里是从调用 save 处开始的完整日志的链接(我不能把它放在这里,因为它太长了): https://pastebin.com/2AjVA2zp

编辑:我还尝试了其他两种方法。

第二个如所述here 。这看起来很有希望,但我收到错误“字段列表”中的未知列“companies0_.proposal_pk_proposalid”。不知何故,使用嵌入的 ID 时,它无法正确形成 SQL。如果我忽略 JSON 的公司部分,那么一切都会正常。但我需要了解这些公司,所以这不是一个有效的选择。

第三种方法与第二种类似,只是我在 @Embeddable PropoasalCompanyID 类中使用了实际的 Proposal 和 Company 对象,但我无法弄清楚如何映射 ManyToMany 方面.

最佳答案

这很容易。您忘记了正确级联。

首先,您不应该从子实体级联到父实体。所以代替:

@ManyToOne(cascade=CascadeType.ALL)

你应该有:

@ManyToOne

其次,应该在父端使用级联:

@OneToMany(mappedBy="company", cascade = CascadeType.ALL, orphanRemoval = true)

@OneToMany(mappedBy="proposal", cascade = CascadeType.ALL, orphanRemoval = true)

第三,您应该对 @ManyToOne 关联使用 FetchType.LAZY,因为默认情况下它们是 EAGER,这对性能不利。

所以,用这个代替:

@ManyToOne(fetch = FetchType.LAZY)

关于java - Spring JPA 多对多,额外列未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48526156/

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