Mysql 使用JPA在Spring中使用removeAll更新对象集
我有一个提案对象,它有一组投票。一个用户拥有一定数量的投票权(比如说,100票将分配给不同的提案)。提案有一个atribute类型,允许用户投票支持它。在研究中,提案可以修改,但不能表决。获得批准后,可以对其进行投票,但当该属性更改为“开发”时,必须从数据库中清除所有投票,并且已将其100票用于已批准提案的用户可以将其取回并投票给另一个提案 我可以创建一个提案,我的控制器可以向提案中添加一个投票,这些投票将显示在数据库中。问题来了,当我改变阿曲布他类型的发展。此时,我可以从DB获取提案,将其发送到proposal.deleteVote()方法,移除所有投票,以(null)投票返回提案,但当我将提案保存在数据库中时,它不会更新。从集合中删除的投票记录仍然存在Mysql 使用JPA在Spring中使用removeAll更新对象集,mysql,spring,jpa,spring-boot,Mysql,Spring,Jpa,Spring Boot,我有一个提案对象,它有一组投票。一个用户拥有一定数量的投票权(比如说,100票将分配给不同的提案)。提案有一个atribute类型,允许用户投票支持它。在研究中,提案可以修改,但不能表决。获得批准后,可以对其进行投票,但当该属性更改为“开发”时,必须从数据库中清除所有投票,并且已将其100票用于已批准提案的用户可以将其取回并投票给另一个提案 我可以创建一个提案,我的控制器可以向提案中添加一个投票,这些投票将显示在数据库中。问题来了,当我改变阿曲布他类型的发展。此时,我可以从DB获取提案,将其发送
@Entity
@Table(name="proposal")
public class Proposal {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private Long id;
@Column(name="functionality")
private String functionality;
@ManyToOne
@JoinColumn(name="siteuserid")
private SiteUser siteUser;
@ManyToOne
@JoinColumn(name = "statusupdateid")
private StatusUpdate statusUpdate;
@OneToMany(mappedBy="proposal", cascade=CascadeType.ALL)
private Set<Vote> votes;
@Column(name="releasedate")
@DateTimeFormat(pattern="yyyy/MM/dd")
private Date release;
@Column(name="proposal_type")
@Enumerated(EnumType.STRING)
private ProposalType proposalType;
@Transient
private int proposalVotes;
public Proposal() {
}
// CONSTRUCTORS GETTER AND SETTERS
public Set<Vote> getVotes() {
return votes;
}
public void setVotes(Set<Vote> votes) {
this.votes = votes;
}
public void addVote(Vote vote){
votes.add(vote);
}
public Proposal deleteVote(Proposal proposal){
HashSet <Vote> thevotes = new HashSet<Vote>();
thevotes.removeAll(proposal.getVotes());
return proposal;
}
public int getProposalVotes() {
return proposalVotes;
}
public void setProposalVotes(int proposalVotes) {
this.proposalVotes = proposalVotes;
}
proposalService在这里。提案未经表决即通过(空)
但它不会在数据库中更新(没有给出错误)。我不知道为什么会这样
public void createProposal(Proposal proposal){
System.out.println("!!!! ProposalService + createProposal .... " +proposal);
proposalDao.save(proposal);
}
投票实体在这里:
package com.caveofprogramming.model.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="votes")
public class Vote {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private Long id;
@ManyToOne
@JoinColumn(name="siteuserid")
private SiteUser siteUser;
@ManyToOne
private Proposal proposal;
@Column(name="points")
private int numberOfPoints;
public Vote() {
}
public Vote(Long id, SiteUser siteUser, Proposal proposal, int numberOfPoints) {
this.id = id;
this.siteUser = siteUser;
this.proposal = proposal;
this.numberOfPoints = numberOfPoints;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public SiteUser getSiteUser() {
return siteUser;
}
public void setSiteUser(SiteUser siteUser) {
this.siteUser = siteUser;
}
public Proposal getProposal() {
return proposal;
}
public void setProposal(Proposal proposal) {
this.proposal = proposal;
}
public int getNumberOfPoints() {
return numberOfPoints;
}
public void setNumberOfPoints(int numberOfPoints) {
this.numberOfPoints = numberOfPoints;
}
@Override
public String toString() {
return "Vote [id=" + id + ", numberOfPoints=" + numberOfPoints + "]";
}
}你能分享你的完全控制者方法和投票实体吗?@Amer Qarabsa完成,但另一个编辑被拒绝。你现在能看到吗?看看你的公共删除投票(提案)方法。这没有任何意义。它创建一个新的空哈希集,然后从这个空哈希集中删除内容。更不用说它是一种提案方法,它将另一个提案作为参数,并返回该提案。。。调用方已经知道了,因为它只是将其作为参数。即使它确实从提案的投票中删除了投票,它也不会修改协会的所有者方(即将投票提案设置为空),因此JPA不会在意。
package com.caveofprogramming.model.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="votes")
public class Vote {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private Long id;
@ManyToOne
@JoinColumn(name="siteuserid")
private SiteUser siteUser;
@ManyToOne
private Proposal proposal;
@Column(name="points")
private int numberOfPoints;
public Vote() {
}
public Vote(Long id, SiteUser siteUser, Proposal proposal, int numberOfPoints) {
this.id = id;
this.siteUser = siteUser;
this.proposal = proposal;
this.numberOfPoints = numberOfPoints;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public SiteUser getSiteUser() {
return siteUser;
}
public void setSiteUser(SiteUser siteUser) {
this.siteUser = siteUser;
}
public Proposal getProposal() {
return proposal;
}
public void setProposal(Proposal proposal) {
this.proposal = proposal;
}
public int getNumberOfPoints() {
return numberOfPoints;
}
public void setNumberOfPoints(int numberOfPoints) {
this.numberOfPoints = numberOfPoints;
}
@Override
public String toString() {
return "Vote [id=" + id + ", numberOfPoints=" + numberOfPoints + "]";
}