package model;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String username;
private String password;
private Profile profile;
public User(){
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Profile getProfile() {
return profile;
}
public void setProfile(Profile profile) {
this.profile = profile;
}
public void addProfile(Profile profile){
profile.setUser(this);
this.setProfile(profile);
}
}
User.hbm.xml如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="model.User" table="users">
<id name="id" type="java.lang.Integer">
<column name="ID" precision="8"></column>
<generator class="increment"></generator>
</id>
<property name="username" column="username" length="40" type="java.lang.String"></property>
<property name="password" column="password" length="40" type="java.lang.String"></property>
<one-to-one name="profile" class="model.Profile" cascade="all">
</one-to-one>
</class>
</hibernate-mapping>
package model;
import java.io.Serializable;
public class Profile implements Serializable {
private static final long serialVersionUID = -7010557039785604098L;
private Integer id;
private String email;
private String address;
private String postcode;
private String mobile;
private String phone;
private User user;
public Profile(){
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
profile.hbm.xml如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="model.Profile" table="profile" >
<id name="id" type="java.lang.Integer">
<column name="ID" precision="8"></column>
<generator class="increment">
</generator>
</id>
<property name="email" column="email" length="100" type="java.lang.String"></property>
<property name="address" column="address" length="200" type="java.lang.String"></property>
<property name="postcode" column="postcode" length="8" type="java.lang.String"></property>
<property name="mobile" column="mobile" length="11" type="java.lang.String"></property>
<property name="phone" column="phone" length="16" type="java.lang.String"></property>
<many-to-one name="user" class="model.User" unique="true">
<column name="user_id" precision="8" scale="0"></column>
</many-to-one>
</class>
</hibernate-mapping>