当前位置: 代码迷 >> J2EE >> Hibernate保存对象属性为空,该如何处理
  详细解决方案

Hibernate保存对象属性为空,该如何处理

热度:342   发布时间:2016-04-22 01:36:47.0
Hibernate保存对象属性为空
调用save时提示not-null property references a null or transient value: com.pb.hibernate.po.Student.sname,好像sname为空,不知道是对象还是数据库的原因,请各位帮忙看下

public class Grade {
private int gid;
private String gname;
private String gdesc;
private Set<Student> students = new HashSet<Student>();
}
<hibernate-mapping>
  <class name="com.pb.hibernate.po.Grade" table="GRADE" >
  <id name="gid" type="java.lang.Integer">
  <column name="GID" />
  <generator class="assigned" />
  </id>
  <property name="gname" type="java.lang.String">
  <column name="GNAME" length="10" not-null="true" />
  </property>
  <property name="gdesc" type="java.lang.String">
  <column name="GDESC" length="50" />
  </property>
  <set name="students">
  <key column="GID"/>
  <one-to-many class="com.pb.hibernate.po.Student"/>
  </set>
  </class>
</hibernate-mapping>
public class Student {
private int sid;
private String sname;
private String sex;
}
  <class name="com.pb.hibernate.po.Student" table="STUDENT" >
  <id name="sid" type="java.lang.Integer">
  <column name="SID" />
  <generator class="assigned" />
  </id>
  <property name="sname" type="java.lang.String">
  <column name="SNAME" length="10" not-null="true" />
  </property>
  <property name="sex" type="java.lang.String">
  <column name="SEX" length="20" />
  </property> 
 
  </class> 

主代码:
Grade g = new Grade();
g.setGid(110701);
g.setGname("计应一班");
g.setGdesc("计算机应用专业一班");

Student stu1 = new Student();
stu1.setSid(11070101);
stu1.setSname("张华");
stu1.setSex("男");

Student stu2 = new Student();
stu1.setSid(11070125);
stu1.setSname("刘萍");
stu1.setSex("女");

Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tran = session.beginTransaction();
session.save(g);
session.save(stu1);
session.save(stu2);
tran.commit();
session.close();

------解决方案--------------------
把 sql打印出来看看
  相关解决方案