gpt4 book ai didi

java - not-null 属性在 hibernate 中引用 null 或 transient 值

转载 作者:行者123 更新时间:2023-11-30 06:56:45 25 4
gpt4 key购买 nike

我在执行代码时遇到异常。我不是 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)

学生.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;
}


}

地址.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");


}
}

最佳答案

当你初始化Student对象的值时,officeAddress不能为空

关于java - not-null 属性在 hibernate 中引用 null 或 transient 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41647950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com