Java not null属性引用Hibernate中的null或瞬时值错误
我正在构建一个Hibernate应用程序,在MySql数据库中存储国家、州和城市信息。但我在线程“main”org.hibernate.PropertyValueException中不断遇到Java not null属性引用Hibernate中的null或瞬时值错误,java,hibernate,jpa,Java,Hibernate,Jpa,我正在构建一个Hibernate应用程序,在MySql数据库中存储国家、州和城市信息。但我在线程“main”org.hibernate.PropertyValueException中不断遇到异常:notnull属性引用空值或瞬时值,即使我在相关实体中有空检查 这里是Country.java package com.models; import java.util.ArrayList; import java.util.List; import javax.persistence.Cascad
异常:notnull属性引用空值或瞬时值
,即使我在相关实体中有空检查
这里是Country.java
package com.models;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.sun.istack.NotNull;
@Entity
@Table(name = "COUNTRY")
public class Country {
public Country() {
super();
// TODO Auto-generated constructor stub
}
public Country(String country, String countryCode) {
super();
this.country = country;
this.countryCode = countryCode;
}
private String country;
@Id
@NotNull
private String countryCode;
@OneToMany(mappedBy = "state", cascade = CascadeType.ALL,fetch = FetchType.LAZY)
private List<State> state = new ArrayList<State>();
public List<State> getState() {
return state;
}
public void setState(List<State> state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
}
package com.models;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "STATE")
public class State {
public State() {
super();
}
public State(String state, String statecode) {
super();
this.state = state;
this.statecode = statecode;
}
private String state;
@Id
private String statecode;
@OneToMany
List<City> city = new ArrayList<City>();
@ManyToOne
@JoinColumn(name="countryCode", nullable=false)
private Country country;
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public List<City> getCity() {
return city;
}
public void setCity(List<City> city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getStatecode() {
return statecode;
}
public void setStatecode(String statecode) {
this.statecode = statecode;
}
}
package com.models;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="CITY")
public class City {
public City() {
super();
// TODO Auto-generated constructor stub
}
public City(String city, boolean isStateCapital, boolean isCountryCapital,
String statecode, String countrycode) {
super();
this.city = city;
this.isStateCapital = isStateCapital;
this.isCountryCapital = isCountryCapital;
this.statecode = statecode;
this.countrycode = countrycode;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int cityid;
private String city;
private boolean isStateCapital;
private boolean isCountryCapital;
private String statecode;
private String countrycode;
public String getStatecode() {
return statecode;
}
public void setStatecode(String statecode) {
this.statecode = statecode;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public boolean isStateCapital() {
return isStateCapital;
}
public void setStateCapital(boolean isStateCapital) {
this.isStateCapital = isStateCapital;
}
public boolean isCountryCapital() {
return isCountryCapital;
}
public void setCountryCapital(boolean isCountryCapital) {
this.isCountryCapital = isCountryCapital;
}
public String getCountrycode() {
return countrycode;
}
public void setCountrycode(String countrycode) {
this.countrycode = countrycode;
}
}
这里是City.java
package com.models;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.sun.istack.NotNull;
@Entity
@Table(name = "COUNTRY")
public class Country {
public Country() {
super();
// TODO Auto-generated constructor stub
}
public Country(String country, String countryCode) {
super();
this.country = country;
this.countryCode = countryCode;
}
private String country;
@Id
@NotNull
private String countryCode;
@OneToMany(mappedBy = "state", cascade = CascadeType.ALL,fetch = FetchType.LAZY)
private List<State> state = new ArrayList<State>();
public List<State> getState() {
return state;
}
public void setState(List<State> state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
}
package com.models;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "STATE")
public class State {
public State() {
super();
}
public State(String state, String statecode) {
super();
this.state = state;
this.statecode = statecode;
}
private String state;
@Id
private String statecode;
@OneToMany
List<City> city = new ArrayList<City>();
@ManyToOne
@JoinColumn(name="countryCode", nullable=false)
private Country country;
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public List<City> getCity() {
return city;
}
public void setCity(List<City> city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getStatecode() {
return statecode;
}
public void setStatecode(String statecode) {
this.statecode = statecode;
}
}
package com.models;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="CITY")
public class City {
public City() {
super();
// TODO Auto-generated constructor stub
}
public City(String city, boolean isStateCapital, boolean isCountryCapital,
String statecode, String countrycode) {
super();
this.city = city;
this.isStateCapital = isStateCapital;
this.isCountryCapital = isCountryCapital;
this.statecode = statecode;
this.countrycode = countrycode;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int cityid;
private String city;
private boolean isStateCapital;
private boolean isCountryCapital;
private String statecode;
private String countrycode;
public String getStatecode() {
return statecode;
}
public void setStatecode(String statecode) {
this.statecode = statecode;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public boolean isStateCapital() {
return isStateCapital;
}
public void setStateCapital(boolean isStateCapital) {
this.isStateCapital = isStateCapital;
}
public boolean isCountryCapital() {
return isCountryCapital;
}
public void setCountryCapital(boolean isCountryCapital) {
this.isCountryCapital = isCountryCapital;
}
public String getCountrycode() {
return countrycode;
}
public void setCountrycode(String countrycode) {
this.countrycode = countrycode;
}
}
下面是Application.java
class
package com.application;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import com.models.City;
import com.models.Country;
import com.models.State;
public class Application {
public static void main(String[] args) {
SessionFactory sessionFactory = getSessionFactory();
Session session = sessionFactory.openSession();
Transaction t = session.beginTransaction();
Country country = new Country("India","IN");
State state= new State("Delhi","DL");
City city = new City("New Delhi",true,true,"DL","IN");
country.getState().add(state);
state.getCity().add(city);
session.save(country);
session.save(state);
session.save(city);
t.commit();
session.close();
sessionFactory.close();
}
private static SessionFactory getSessionFactory() {
Configuration configuartion = new Configuration().configure();
configuartion.addAnnotatedClass(Country.class);
configuartion.addAnnotatedClass(State.class);
configuartion.addAnnotatedClass(City.class);
// StandardServiceRegistryBuilder registry = new StandardServiceRegistryBuilder();
// registry.applySettings(configuartion.getProperties());
// StandardServiceRegistry serviceRegistry = registry.build();
// SessionFactory sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
SessionFactory sessionFactory = configuartion.buildSessionFactory();
return sessionFactory;
}
}
hibernate.cfg.xml
文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">pwdrd</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/countryinfo</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL55Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
虽然我在国家和州实体上有可为空的检查,但我有级联和获取类型惰性属性集,但它仍然没有解析。我们将非常感谢您在这方面的帮助。谢谢Sid修改
Application.java
。保存顺序是错误的
public class Application {
public static void main(String[] args) {
SessionFactory sessionFactory = getSessionFactory();
Session session = sessionFactory.openSession();
Transaction t = session.beginTransaction();
Country country = new Country("India","IN");
State state= new State("Delhi","DL");
City city = new City("New Delhi",true,true,"DL","IN");
// first save city
session.save(city);
// then add city to state
state.getCity().add(city);
state.setCountry(country);
// save state
session.save(state);
// add state to country
country.getState().add(state);
// save country
session.save(country);
t.commit();
session.close();
sessionFactory.close();
}
}
您可以做以下更改并重试吗 在
国家/地区
实体中存在映射问题,mappedBy
字段设置为状态
,这将创建一个额外的外键。请更新如下
@OneToMany(mappedBy = "country", cascade = CascadeType.ALL,fetch = FetchType.LAZY)
private List<State> state = new ArrayList<State>();
现在在应用程序.java中进行以下更改
package com.models;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.sun.istack.NotNull;
@Entity
@Table(name = "COUNTRY")
public class Country {
public Country() {
super();
// TODO Auto-generated constructor stub
}
public Country(String country, String countryCode) {
super();
this.country = country;
this.countryCode = countryCode;
}
private String country;
@Id
@NotNull
private String countryCode;
@OneToMany(mappedBy = "state", cascade = CascadeType.ALL,fetch = FetchType.LAZY)
private List<State> state = new ArrayList<State>();
public List<State> getState() {
return state;
}
public void setState(List<State> state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
}
package com.models;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "STATE")
public class State {
public State() {
super();
}
public State(String state, String statecode) {
super();
this.state = state;
this.statecode = statecode;
}
private String state;
@Id
private String statecode;
@OneToMany
List<City> city = new ArrayList<City>();
@ManyToOne
@JoinColumn(name="countryCode", nullable=false)
private Country country;
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public List<City> getCity() {
return city;
}
public void setCity(List<City> city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getStatecode() {
return statecode;
}
public void setStatecode(String statecode) {
this.statecode = statecode;
}
}
package com.models;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="CITY")
public class City {
public City() {
super();
// TODO Auto-generated constructor stub
}
public City(String city, boolean isStateCapital, boolean isCountryCapital,
String statecode, String countrycode) {
super();
this.city = city;
this.isStateCapital = isStateCapital;
this.isCountryCapital = isCountryCapital;
this.statecode = statecode;
this.countrycode = countrycode;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int cityid;
private String city;
private boolean isStateCapital;
private boolean isCountryCapital;
private String statecode;
private String countrycode;
public String getStatecode() {
return statecode;
}
public void setStatecode(String statecode) {
this.statecode = statecode;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public boolean isStateCapital() {
return isStateCapital;
}
public void setStateCapital(boolean isStateCapital) {
this.isStateCapital = isStateCapital;
}
public boolean isCountryCapital() {
return isCountryCapital;
}
public void setCountryCapital(boolean isCountryCapital) {
this.isCountryCapital = isCountryCapital;
}
public String getCountrycode() {
return countrycode;
}
public void setCountrycode(String countrycode) {
this.countrycode = countrycode;
}
}
Country-Country=新国家(“印度”、“印度”);
州=新州(“德里”、“DL”);
城市=新市(“新德里”,真,真,“DL”,“IN”);
会议.拯救(城市);
state.getCity().add(城市);
country.addState(state);
会议.拯救(国家);
t、 提交();
session.close();
sessionFactory.close();
谢谢,我试过了,但现在遇到了这个错误<代码>Hibernate:在线程“main”org.Hibernate.PropertyValueException中插入CITY(CITY,countrycode,isCountryCapital,isStateCapital,statecode)值(?,,,?,?,?)异常:not null属性引用空值或临时值:com.models.State.country位于org.Hibernate.engine.internal.Nullability.checkNullability(Nullability.java:111)位于org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:55)
@Sid111Math just put state.setCountry(country);在此state.getCity()之后添加(city);我已经更新了我的答案,现在明白了,原因是:java.sql.SQLIntegrityConstraintViolationException:com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117)在com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)上的列“countryCode”不能为空com.mysql.cj.jd.jdbc.exceptions.sqlexceptions映射.translateException(sqlexceptions映射.java:122)com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1092)
@SID1111Math您在应用程序类的某个地方将countrycode设置为null。请检查。此外,请正确编写您的实体。注意:您在countrycode中保留了countrycode上的@NotNull
。因此,这就是为什么您会出现此错误。我已从Country
中删除了@NotNull
,如下所示现在。@Id private String countryCode;
在状态下
类中,我已经删除了nullable=false
,它看起来像是@manytone@JoinColumn(name=“countryCode”)private Country Country;
但我仍然得到了列“countryCode”不能为null的错误。