org.hibernate.PropertyValueException:not-null属性引用null或transient值:mdl.Complaint.jobDone

时间:2016-02-29 17:02:32

标签: java hibernate

我尝试使用hibanate并在SQL DB中保存值。它有一个数据库和3个tbales表,这些表称为Complaint,JobDone和JobCard。

Complaint和JobDone之间存在一对一的关系 Complaint和JobCard之间存在多对一的关系。

当我运行我的项目时出现以下错误:

Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value: mdl.Complaint.jobDone
    at org.hibernate.engine.Nullability.checkNullability(Nullability.java:100)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:312)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)

以下代码是否可行?
编写这些代码的更好方法是什么?

JobCard.java:

@Entity
@Table(name = "JobCard", catalog = "jobcard", uniqueConstraints = {
        @UniqueConstraint(columnNames = "vehicleNumber"),
                @UniqueConstraint(columnNames = "date"),
                @UniqueConstraint(columnNames = "model"),
                @UniqueConstraint(columnNames = "groupLeader"),
                @UniqueConstraint(columnNames = "testBy"),
                @UniqueConstraint(columnNames = "jobDirector"),
        @UniqueConstraint(columnNames = "finishDate") })
public class JobCard implements Serializable{
    private Integer jobNumber;
    private String vehicleNumber;
    private Integer date;
    private String model;
    private String groupLeader;
    private String testBy;
    private String jobDirector;
    private Integer finishDate;
    private Set<Complaint> complaints = new HashSet<Complaint>(0);
    /**
     * @return the jobNumber
     */
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "jobNumber", unique = true, nullable = false)
    public Integer getJobNumber() {
        return jobNumber;
    }

    /**
     * @param jobNumber the jobNumber to set
     */
    public void setJobNumber(Integer jobNumber) {
        this.jobNumber = jobNumber;
    }

    /**
     * @return the vehicleNumber
     */
    public String getVehicleNumber() {
        return vehicleNumber;
    }

    /**
     * @param vehicleNumber the vehicleNumber to set
     */
    public void setVehicleNumber(String vehicleNumber) {
        this.vehicleNumber = vehicleNumber;
    }

    /**
     * @return the date
     */
    public Integer getDate() {
        return date;
    }

    /**
     * @param date the date to set
     */
    public void setDate(Integer date) {
        this.date = date;
    }

    /**
     * @return the model
     */
    public String getModel() {
        return model;
    }

    /**
     * @param model the model to set
     */
    public void setModel(String model) {
        this.model = model;
    }

    /**
     * @return the groupLeader
     */
    public String getGroupLeader() {
        return groupLeader;
    }

    /**
     * @param groupLeader the groupLeader to set
     */
    public void setGroupLeader(String groupLeader) {
        this.groupLeader = groupLeader;
    }

    /**
     * @return the testBy
     */
    public String getTestBy() {
        return testBy;
    }

    /**
     * @param testBy the testBy to set
     */
    public void setTestBy(String testBy) {
        this.testBy = testBy;
    }

    /**
     * @return the jobDirector
     */
    public String getJobDirector() {
        return jobDirector;
    }

    /**
     * @param jobDirector the jobDirector to set
     */
    public void setJobDirector(String jobDirector) {
        this.jobDirector = jobDirector;
    }

    /**
     * @return the finishDate
     */
    public Integer getFinishDate() {
        return finishDate;
    }

    /**
     * @param finishDate the finishDate to set
     */
    public void setFinishDate(Integer finishDate) {
        this.finishDate = finishDate;
    }

    /**
     * @return the complaints
     */
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "jobCard")
    public Set<Complaint> getComplaints() {
        return complaints;
    }

    /**
     * @param complaints the complaints to set
     */
    public void setComplaints(Set<Complaint> complaints) {
        this.complaints = complaints;
    }


}

JobDone.java:

@Entity
@Table(name = "JobDone", catalog = "jobcard", uniqueConstraints = {
        @UniqueConstraint(columnNames = "nature"),
        @UniqueConstraint(columnNames = "amount") })
public class JobDone implements Serializable{
    private Integer jobDone_id;
    private String nature;
    private Integer amount;
    private Complaint complaint;

    /**
     * @return the jobDone_id
     */
    @GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "complaint"))
    @Id
    @GeneratedValue(generator = "generator")
    @Column(name = "jobDone_id", unique = true, nullable = false)
    public Integer getJobDone_id() {
        return jobDone_id;
    }

    /**
     * @param jobDone_id the jobDone_id to set
     */
    public void setJobDone_id(Integer jobDone_id) {
        this.jobDone_id = jobDone_id;
    }

    /**
     * @return the nature
     */
    public String getNature() {
        return nature;
    }

    /**
     * @param nature the nature to set
     */
    public void setNature(String nature) {
        this.nature = nature;
    }

    /**
     * @return the amount
     */
    public Integer getAmount() {
        return amount;
    }

    /**
     * @param amount the amount to set
     */
    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    /**
     * @return the complaint
     */

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "jobDone", cascade = CascadeType.ALL)
    public Complaint getComplaint() {
        return complaint;
    }

    /**
     * @param complaint the complaint to set
     */
    public void setComplaint(Complaint complaint) {
        this.complaint = complaint;
    }
}

Complaint.java

@Entity
@Table(name = "Complaint", catalog = "jobcard")
public class Complaint implements Serializable{
    private Integer complaint_id;
    private String nature;
    private String instruction;
    private JobDone jobDone;
    private JobCard jobCard;

    /**
     * @return the complaint_id
     */
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "complaint_id", unique = true, nullable = false)
    public Integer getComplaint_id() {
        return complaint_id;
    }

    /**
     * @param complaint_id the complaint_id to set
     */
    public void setComplaint_id(Integer complaint_id) {
        this.complaint_id = complaint_id;
    }

    /**
     * @return the nature
     */
    public String getNature() {
        return nature;
    }

    /**
     * @param nature the nature to set
     */
    public void setNature(String nature) {
        this.nature = nature;
    }

    /**
     * @return the instruction
     */
    public String getInstruction() {
        return instruction;
    }

    /**
     * @param instruction the instruction to set
     */
    public void setInstruction(String instruction) {
        this.instruction = instruction;
    }

    /**
     * @return the jobDone
     */
    @OneToOne(fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumn
    public JobDone getJobDone() {
        return jobDone;
    }

    /**
     * @param jobDone the jobDone to set
     */
    public void setJobDone(JobDone jobDone) {
        this.jobDone = jobDone;
    }

    /**
     * @return the jobCard
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "jobNumber", nullable = false)
    public JobCard getJobCard() {
        return jobCard;
    }

    /**
     * @param jobCard the jobCard to set
     */
    public void setJobCard(JobCard jobCard) {
        this.jobCard = jobCard;
    }

}

SQl查询:

CREATE DATABASE jobcard;
USE jobcard;
CREATE TABLE JobCard(
    customer_id varchar(10),
    grade_id varchar(10),
    jobNumber int(10) primary key NOT NULL AUTO_INCREMENT,
    vehicleNumber varchar(10),
    date int,
    model varchar(100),
    groupLeader varchar(100),
    testBy varchar(100),
    jobDirector varchar(100),
    finishDate int,
    constraint foreign key(customer_id) references Customer(customer_id) 
    on delete cascade on update cascade    ,
    constraint foreign key(grade_id) references Grade(grade_id) 
    on delete cascade on update cascade    
);
CREATE TABLE Complaint(
    jobNumber int(100),
    complaint_id int(10) primary key NOT NULL AUTO_INCREMENT,
    nature varchar(200),
    instruction varchar(200),
    constraint foreign key(jobNumber) references JobCard(jobNumber) 
    on delete cascade on update cascade  
);

CREATE TABLE JobDone(
    complaint_id int(10),
    jobDone_id int(10) primary key NOT NULL AUTO_INCREMENT,
    nature varchar(200),
    amount int,
    constraint foreign key(complaint_id) references Complaint(complaint_id) 
    on delete cascade on update cascade     
);

我创建了hibernate.cfg.xml文件,并为每个类映射了完全限定的类名。

1 个答案:

答案 0 :(得分:0)

您正在尝试使用瞬态嵌入对象保存实例 - 对于休眠是未知的(通常意味着ID为null)。您必须先保存此嵌入对象。

因此,如果您想将Complaint保存到数据库,则必须首先保存嵌入式JobCard和JobDone,然后再保存投诉。