和单向的类似,只不过多加了一个关联User关联Frofile,Profile同时也关联User
User类
import java.io.Serializable;
import com.lbx.hibernate.model.Profile;
/**
* 组件双向关联
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class User implements Serializable{
private int id;
private String username;
private String password;
private Profile profile;
public int getId() {
return id;
}
public void setId(int 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;
}
}
Profile类
import java.io.Serializable;
@SuppressWarnings("serial")
public class Profile implements Serializable{
private User user;
private String email;
private String phone;
private String mobile;
private String address;
private String postcode;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
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;
}
}
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 package="com.lbx.hibernate.model"> <class name="User" table="user" lazy="true"> <id name="id" type="int"> <column name="id" scale="0" /> <generator class="increment" /> </id> <property name="username" type="java.lang.String" /> <property name="password" type="java.lang.String" /> <component name="profile" class="com.lbx.hibernate.model.Profile"> <parent name="user"/> <property name="email" type="string" /> <property name="phone" type="string" /> <property name="mobile" type="string" /> <property name="address" type="string" /> <property name="postcode" type="string" /> </component> </class> </hibernate-mapping>