代码如下,Gender是个枚举类型:
public class Buyer implements Serializable{
/** 用户名 **/
private String username;//只允许字母/数字/下划线
/** 密码 **/
private String password;//采用MD5加密
/** 真实姓名 **/
private String realname;
/** 电子邮箱 **/
private String email;
/** 性别 **/
private Gender gender=Gender.MAN;
/** 联系信息 **/
private ContactInfo contactInfo;
/** 是否启用 **/
private Boolean visible=true;
/** 注册时间 **/
private Date regTime = new Date();
//...
}public class ContactInfo implements Serializable{
private Integer contactid;
/** 地址 **/
private String address;
/** 邮编 **/
private String postalcode;
/** 座机 **/
private String phone;
/** 手机 **/
private String mobile;
/** 所属用户 **/
private Buyer buyer;
//...
}
hibernate配置如下:
<hibernate-mapping>
<class name="com.liaoqian.bean.user.Buyer">
<id name="username" type="string" length="18">
<generator class="assigned"></generator>
</id> <property name="password" type="string" not-null="true" length="32"></property>
<property name="realname" type="string" length="8"></property>
<property name="email" type="string" length="50" not-null="true"></property>
<property name="gender"></property>
<property name="visible" not-null="true"></property>
<property name="regTime"></property>
<one-to-one name="contactInfo" property-ref="buyer" cascade="all"></one-to-one>
</class>
</hibernate-mapping><hibernate-mapping>
<class name="com.liaoqian.bean.user.ContactInfo">
<id name="contactid">
<generator class="native"></generator>
</id> <property name="address" type="string" length="100" not-null="true"></property>
<property name="postalcode" type="string" length="6"></property>
<property name="phone" type="string" length="20"></property>
<property name="mobile" type="string" length="11"></property>
<many-to-one name="buyer" column="buyer_id" unique="true"/>
</class>
</hibernate-mapping>单元测试方法:
@Test
public void testSave() {
Buyer buyer = new Buyer("liaoqian","123456","[email protected]");
ContactInfo contactInfo = new ContactInfo();
contactInfo.setAddress("广州");
contactInfo.setMobile("13671323507");
contactInfo.setPhone("010-64469090-9");
contactInfo.setPostalcode("100028");
buyer.setContactInfo(contactInfo);
buyer.setRealname("才");
buyerService.save(buyer);
}
保存后发现contactinfo表的buyer_id为null,没有和buyer表的username关联上, 怎么解决
public class Buyer implements Serializable{
/** 用户名 **/
private String username;//只允许字母/数字/下划线
/** 密码 **/
private String password;//采用MD5加密
/** 真实姓名 **/
private String realname;
/** 电子邮箱 **/
private String email;
/** 性别 **/
private Gender gender=Gender.MAN;
/** 联系信息 **/
private ContactInfo contactInfo;
/** 是否启用 **/
private Boolean visible=true;
/** 注册时间 **/
private Date regTime = new Date();
//...
}public class ContactInfo implements Serializable{
private Integer contactid;
/** 地址 **/
private String address;
/** 邮编 **/
private String postalcode;
/** 座机 **/
private String phone;
/** 手机 **/
private String mobile;
/** 所属用户 **/
private Buyer buyer;
//...
}
hibernate配置如下:
<hibernate-mapping>
<class name="com.liaoqian.bean.user.Buyer">
<id name="username" type="string" length="18">
<generator class="assigned"></generator>
</id> <property name="password" type="string" not-null="true" length="32"></property>
<property name="realname" type="string" length="8"></property>
<property name="email" type="string" length="50" not-null="true"></property>
<property name="gender"></property>
<property name="visible" not-null="true"></property>
<property name="regTime"></property>
<one-to-one name="contactInfo" property-ref="buyer" cascade="all"></one-to-one>
</class>
</hibernate-mapping><hibernate-mapping>
<class name="com.liaoqian.bean.user.ContactInfo">
<id name="contactid">
<generator class="native"></generator>
</id> <property name="address" type="string" length="100" not-null="true"></property>
<property name="postalcode" type="string" length="6"></property>
<property name="phone" type="string" length="20"></property>
<property name="mobile" type="string" length="11"></property>
<many-to-one name="buyer" column="buyer_id" unique="true"/>
</class>
</hibernate-mapping>单元测试方法:
@Test
public void testSave() {
Buyer buyer = new Buyer("liaoqian","123456","[email protected]");
ContactInfo contactInfo = new ContactInfo();
contactInfo.setAddress("广州");
contactInfo.setMobile("13671323507");
contactInfo.setPhone("010-64469090-9");
contactInfo.setPostalcode("100028");
buyer.setContactInfo(contactInfo);
buyer.setRealname("才");
buyerService.save(buyer);
}
保存后发现contactinfo表的buyer_id为null,没有和buyer表的username关联上, 怎么解决
解决方案 »
- 关于struts2 的action中接收来自jsp页面数据的问题,有经验的来看看
- java mail问题
- JSP input:file中的值如何通过JavaScript存到input :hidden中
- Hibernate的异常TransientObjectException
- html:optionsCollection 如何设定一个默认值,例如默认值为空。
- 如何在jsp脚本中获取actionform中的值
- tomcat集成iis,图片不显示??
- IT集团招聘优秀软件研发人员(JAVA、.NET)
- hibernate在mssql中查询中的问题?
- 关于session facade 模式的一个问题
- 求救JavaEE的达人= =
- Action 里面继承ActionSupport有何作用?
先把这个cascade这个级联属性去掉,以前碰到过,也觉得奇怪,没有找出根本的原因,这样去掉又可以关联上FORM:QQ群Java 2 Enterprise Edition(88509302)
如果要 就可以考虑修改 hbm.xml<hibernate-mapping>
<class name="com.liaoqian.bean.user.Buyer">
.....
<many-to-one name="contactInfo" class="ContactInfo" unique="true" cascade="all"></many-to-one>
</class>
</hibernate-mapping> <class name="com.liaoqian.bean.user.ContactInfo">
...
<one-to-one name="buyer" class="Buyer"></one-to-one>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.liaoqian.bean.user.ContactInfo">
<id name="contactid">
<generator class="native"> </generator>
</id>
id应该是foriegn
this.contactInfo = contactInfo;
contactInfo.setBuyer(this);
}