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

时间:2015-10-18 10:27:41

标签: java entity

这是我的第一篇文章,如果我确实犯了错误,请耐心等待。

我收到错误" java.lang.IllegalStateException:在同步过程中,通过未标记为级联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 个答案:

答案 0 :(得分:0)

字段

@ManyToOne(optional = false)
private Sponsors sponsorid;
Donations实体中的

未配置为级联。尝试将其更改为:

@ManuToOne(optional = false, cascade = CascadeType.ALL)
private Sponsors sponsorid;