1
私のコードを実行中に例外以下になります。私は冬眠の専門家ではなく、ちょうどそれを学び始めた。助けてください。以下のコードを正常に実行するためには、どのような変更を行う必要がありますか。私は以下のすべてのコードを追加しました。ハイパーネートでNULL以外のプロパティがnullまたは一時的な値を参照しています
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", [email protected](name="Home_PIN_Code", nullable=false)),
@AttributeOverride(name="street", [email protected](name="Home_Street", nullable=false)),
@AttributeOverride(name="city", [email protected](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");
}
}
例外は明らかです:あなたのe1にはオフィスアドレスがありませんが、このアドレスは必須です(すべての列はnullではありません)。また、クラスのすべてのマッピングアノテーションは、フィールドまたはゲッターのいずれかである必要がありますが、混合されていない必要があります。そして姓に '@ Lob'を使うことはあまり意味がありません。あなたの生徒は4000文字以上の姓を持っていますか? –
Thanks mate、@Lob私はテストのために置いて間違ったフィールドに置いています。例外が解決されました。ありがとう – sajid