not-null属性在hibernate

时间:2017-01-14 08:01:33

标签: java hibernate

我在执行代码时遇到异常。我不是hibernate专家,只是开始学习它。请帮忙。成功执行以下代码需要做哪些更改。我在下面添加了所有代码。

Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value: hibernate.Student.officeAddress
    at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:284)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
    at hibernate.StoreData.main(StoreData.java:44)

Student.java

package hibernate;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table (name="student")
public class Student {

@Id
private int id;
private String firstName;
private String lastName;
private java.util.Date date;
@Embedded
@AttributeOverrides({
    @AttributeOverride(name="pincode", column=@Column(name="Home_PIN_Code", nullable=false)),
    @AttributeOverride(name="street", column=@Column(name="Home_Street", nullable=false)),
    @AttributeOverride(name="city", column=@Column(name="Home_City", nullable=false))
    })
private Address homeAddress;
@Embedded
private Address officeAddress;

public Address getHomeAddress() {
    return homeAddress;
}

public void setHomeAddress(Address homeAddress) {
    this.homeAddress = homeAddress;
}

public Address getOfficeAddress() {
    return officeAddress;
}

public void setOfficeAddress(Address officeAddress) {
    this.officeAddress = officeAddress;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

@Column (name="FirstNAME")
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}

@Lob
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}

@Temporal (TemporalType.TIMESTAMP)
public java.util.Date getDate() {
    return date;
}

public void setDate(java.util.Date date2) {
    this.date = date2;
}


}

Address.java

package hibernate;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Address {
@Column(name="Pincode",nullable=false)
private int pincode;
@Column(name="Street",nullable=false)
private String street;
@Column(name="City",nullable=false)
private String city;

public int getPincode() {
    return pincode;
}
public void setPincode(int pincode) {
    this.pincode = pincode;
}
public String getStreet() {
    return street;
}
public void setStreet(String street) {
    this.street = street;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}


}

StoreData.java

package hibernate;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

public class StoreData {
public static void main(String[] args) {

    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction t=session.beginTransaction();  

    Student e1=new Student();
    e1.setId(1);
    e1.setFirstName("Majid");  
    e1.setLastName("Khan");  
    e1.setDate(new Date());

    Address ad1 = new Address();
    ad1.setCity("Mumbai");
    ad1.setPincode(400059);
    ad1.setStreet("Marol Mahrishi Road");

    e1.setHomeAddress(ad1);

    Student e2=new Student();  
    e2.setId(2);
    e2.setFirstName("Jayada");  
    e2.setLastName("Bano");
    e2.setDate(new Date());

    Address ad2 = new Address();
    ad2.setCity("Hindaun");
    ad2.setPincode(322230);
    ad2.setStreet("Islam Colony");

    e2.setOfficeAddress(ad2);

    session.save(e1);  
    session.save(e2);  

    t.commit();  
    session.close();  
    System.out.println("successfully saved");


   }
   }

2 个答案:

答案 0 :(得分:1)

将值初始化为Student对象时,

officeAddress不能为空

答案 1 :(得分:0)

按照Hibernate Embedded/Embeddable not null exception

的说明将private int pincode更改为private Integer pincode