当前位置: 首页 > 知识库问答 >
问题:

错误“java.lang.IllegalStateException:在同步过程中,通过未标记为cascade PERSIST的关系找到了一个新对象”

阎星河
2023-03-14

这是我的第一篇帖子,如果我有任何错误,请原谅我。

当我想查询(添加,编辑,删除)数据库时,我得到一个错误java.lang.IllegalStateExctive:在同步期间,通过未标记为级联PERSIST的关系找到了一个新对象。

相关表格包括赞助商和捐款。它们之间存在一对多的关系。以下课程:赞助商

    @Entity
    @Table(name = "SPONSORS")
    @NamedQueries({
    @NamedQuery(name = "Sponsors.findAll", query = "SELECT s FROM Sponsors s")})

    public class Sponsors implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "SPONSORID")
    private Short sponsorid;
    @Basic(optional = false)
    @Column(name = "NAME")
    private String name;
    @Basic(optional = false)
    @Column(name = "SURNAME")
    private String surname;
    @Basic(optional = false)
    @Column(name = "ADDRESS")
    private String address;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "sponsorid")
    private List<Donations> donationsList;

    public Sponsors() {
    }

    public Sponsors(Short sponsorid) {
        this.sponsorid = sponsorid;
    }

    public Sponsors(Short sponsorid, String name, String surname, String address) {
        this.sponsorid = sponsorid;
        this.name = name;
        this.surname = surname;
        this.address = address;
    }

    public Short getSponsorid() {
        return sponsorid;
    }

    public void setSponsorid(Short sponsorid) {
        this.sponsorid = sponsorid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public List<Donations> getDonationsList() {
        return donationsList;
    }

    public void setDonationsList(List<Donations> donationsList) {
        this.donationsList = donationsList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (sponsorid != null ? sponsorid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Sponsors)) {
            return false;
        }
        Sponsors other = (Sponsors) object;
        if ((this.sponsorid == null && other.sponsorid != null) || (this.sponsorid != null && !this.sponsorid.equals(other.sponsorid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Pat.Sponsors[ sponsorid=" + sponsorid + " ]";
    }

}

捐款:

@Entity
@Table(name = "DONATIONS")
@NamedQueries({
    @NamedQuery(name = "Donations.findAll", query = "SELECT d FROM Donations d")})
public class Donations implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "DONATIONID")
    private Short donationid;
    @Basic(optional = false)
    @Column(name = "DONATIONDATE")
    @Temporal(TemporalType.DATE)
    private Date donationdate;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Column(name = "DONATIONAMOUNT")
    private Double donationamount;
    @JoinColumn(name = "SPONSORID", referencedColumnName = "SPONSORID")
    @ManyToOne(optional = false)
    private Sponsors sponsorid;

    public Donations() {
    }

    public Donations(Short donationid) {
        this.donationid = donationid;
    }

    public Donations(Short donationid, Date donationdate) {
        this.donationid = donationid;
        this.donationdate = donationdate;
    }

    public Short getDonationid() {
        return donationid;
    }

    public void setDonationid(Short donationid) {
        this.donationid = donationid;
    }

    public Date getDonationdate() {
        return donationdate;
    }

    public void setDonationdate(Date donationdate) {
        this.donationdate = donationdate;
    }

    public Double getDonationamount() {
        return donationamount;
    }

    public void setDonationamount(Double donationamount) {
        this.donationamount = donationamount;
    }

    public Sponsors getSponsorid() {
        return sponsorid;
    }

    public void setSponsorid(Sponsors sponsorid) {
        this.sponsorid = sponsorid;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (donationid != null ? donationid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Donations)) {
            return false;
        }
        Donations other = (Donations) object;
        if ((this.donationid == null && other.donationid != null) || (this.donationid != null && !this.donationid.equals(other.donationid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Pat.Donations[ donationid=" + donationid + " ]";
    }

}

共有1个答案

隆飞宇
2023-03-14

领域

@ManyToOne(optional = false)
private Sponsors sponsorid;

捐赠实体中,没有为级联配置。请尝试将其更改为:

@ManuToOne(optional = false, cascade = CascadeType.ALL)
private Sponsors sponsorid;
 类似资料: