问题描述
<hibernate-mapping> <class name="org.vo.Type" table="type"> <id name="typeid" type="java.lang.Integer"> <column name="typeid"> <generator class="native"> </id> <property name="typename" type="java.lang.String"> <column name="typename" length="50" not-null="true"> </property> <set name="xuqius"> table="xuqiu" lazy="true" order-by="typeid"> <key> <column name="typeid"> </key> <one-to-many class="org.vo.Xuqiu"> </set> </class> </hibernate-mapping> private Integer typeid; private String typename; private Set xuqius; <hibernate-mapping> <class name="org.vo.Xuqiu" table="xuqiu"> <id name="id" type="java.lang.Integer"> <column name="id"> <generator class="native"> </id> <many-to-one name="type" class="org.vo.Type"> <column name="typeid"> </many-to-one> <property name="name" type="java.lang.String"> <column name="name" length="50" not-null="true"> </property> <property name="xuqiu" type="java.lang.String"> <column name="xuqiu" length="2000" not-null="true"> </property> <property name="fdate" type="java.lang.String"> <column name="fdate" length="23"> </property> <property name="gdate" type="java.lang.String"> <column name="gdate" length="23"> </property> <property name="state" type="java.lang.Integer"> <column name="state"> </property> </class> </hibernate-mapping> private Integer id; private Integer typeid; private String name; private String xuqiu; private String fdate; private String gdate; private Integer state; private Type type; 在action中 XuqiuForm xuqiuForm = (XuqiuForm) form; Xuqiu xuqiu = new Xuqiu(); xuqiu.setName(xuqiuForm.getName()); System.out.println(xuqiuForm.getName()); Type t = new Type(); t.setTypeid(Integer.getInteger(xuqiuForm.getType())); xuqiu.setType(t); System.out.println(xuqiuForm.getType()); xuqiu.setTypeid(Integer.getInteger(xuqiuForm.getType())); xuqiu.setXuqiu(xuqiuForm.getXuqiu()); System.out.println(xuqiuForm.getXuqiu()); xuqiu.setFdate(xuqiuForm.getFdate()); System.out.println(xuqiuForm.getFdate()); xuqiu.setGdate(xuqiuForm.getGdate()); System.out.println(xuqiuForm.getGdate()); xuqiu.setState(Integer.getInteger(xuqiuForm.getState())); System.out.println(xuqiuForm.getState()); try { this.ixuqiudao.Save(xuqiu); } catch (Exception e) { // TODO 自动生成 catch 块 e.printStackTrace(); } typeid为null 但是我System.out显示了值
解决方案
尝试把你的pojo写成这样,在Type 上我添加了一个方便的方法 addXuqiu。另外,你的cascade控制在one端,所以保存时候只要保存one端,one端所依赖的另一端就自动保存了。package org.vo;import java.io.Serializable;public class Xuqiu implements Serializable {private Integer id; private Integer typeid; private String name; private String xuqiu; private String fdate; private String gdate; private Integer state; private Type type; public Xuqiu() { }public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public Integer getTypeid() {return typeid;}public void setTypeid(Integer typeid) {this.typeid = typeid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getXuqiu() {return xuqiu;}public void setXuqiu(String xuqiu) {this.xuqiu = xuqiu;}public String getFdate() {return fdate;}public void setFdate(String fdate) {this.fdate = fdate;}public String getGdate() {return gdate;}public void setGdate(String gdate) {this.gdate = gdate;}public Integer getState() {return state;}public void setState(Integer state) {this.state = state;}public Type getType() {return type;}public void setType(Type type) {this.type = type;} }package org.vo;import java.util.*;import java.io.Serializable;public class Type implements Serializable {private Integer typeid; private String typename; private Set xuqius = new HashSet(); public Type() { } public Integer getTypeid() {return typeid;}public void setTypeid(Integer typeid) {this.typeid = typeid;}public String getTypename() {return typename;}public void setTypename(String typename) {this.typename = typename;}public Set getXuqius() {return xuqius;}public void setXuqius(Set xuqius) {this.xuqius = xuqius;} public void addXuqiu(Xuqiu xq) { xq.setType(this); xuqius.add(xq); } }package org.vo;import java.util.List;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import persistence.HibernateUtil;public class Test {public static void main(String[] args) {SessionFactory sessionFactory = HibernateUtil.getSessionFactory();Session session = sessionFactory.openSession();Transaction tx = session.beginTransaction();Type type = new Type();type.setTypename("a");Xuqiu xq = new Xuqiu();xq.setFdate("a");xq.setGdate("a");xq.setName("a");xq.setState(1);xq.setXuqiu("a");type.addXuqiu(xq);session.save(type);tx.commit();session.close();HibernateUtil.shutdown();}}