实例
用户与身份证信息
一条用户记录对应一条身份证信息!一对一的关系!
基于外键映射实例
com.cx.one2one.User
package com.cx.one2one; /** * Created by cxspace on 16-8-3. */ public class User { private int userId; private String userName; //用户与身份证信息,一对一映射 private IdCard idCard; public IdCard getIdCard() { return idCard; } public void setIdCard(IdCard idCard) { this.idCard = idCard; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
com.cx.one2one.IdCard
package com.cx.one2one; /** * Created by cxspace on 16-8-3. */ public class IdCard { private String cardNum; //身份证号作为主键 private String place; private User user; public String getCardNum() { return cardNum; } public void setCardNum(String cardNum) { this.cardNum = cardNum; } public String getPlace() { return place; } public void setPlace(String place) { this.place = place; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
com/cx/one2one/User.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.cx.one2one"> <class name="User" table="t_user"> <id name="userId"> <generator class="native"></generator> </id> <property name="userName" length="20"></property> <!--一对一映射--> <one-to-one name="idCard" class="IdCard"></one-to-one> </class> </hibernate-mapping>
com/cx/one2one/IdCard.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.cx.one2one"> <class name="IdCard" table="t_IdCard"> <id name="cardNum"> <generator class="assigned"></generator> </id> <property name="place" length="20"></property> <!-- 一对一映射,有外键方 unique=“true” 给外键字段添加唯一约束 --> <many-to-one name="user" unique="true" column="user_id" class="User" cascade="save-update"></many-to-one> </class> </hibernate-mapping>
com.cx.one2one.Test
package com.cx.one2one; /** * Created by cxspace on 16-8-3. */ import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class Test { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(IdCard.class) .addClass(User.class) // 测试时候使用 .buildSessionFactory(); } @org.junit.Test public void save(){ Session session = sf.openSession(); session.beginTransaction(); User user = new User(); user.setUserName("jack"); IdCard idCard = new IdCard(); idCard.setCardNum("3324778###"); idCard.setPlace("广州×××"); idCard.setUser(user); session.save(idCard); session.getTransaction().commit(); session.close(); /* * Hibernate: insert into t_user (userName) values (?) Hibernate: insert into t_IdCard (place, user_id, cardNum) values (?, ?, ?) * * */ } }
表的结构
基于主键的实例
com.cx.one2one2.User
package com.cx.one2one2; /** * Created by cxspace on 16-8-3. */ public class User { private int userId; private String userName; private IdCard idCard; public IdCard getIdCard() { return idCard; } public void setIdCard(IdCard idCard) { this.idCard = idCard; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
com.cx.one2one2.IdCard
package com.cx.one2one2; /** * Created by cxspace on 16-8-3. */ public class IdCard { private int user_id; private String cardNum; private String place; private User user; public String getCardNum() { return cardNum; } public void setCardNum(String cardNum) { this.cardNum = cardNum; } public String getPlace() { return place; } public void setPlace(String place) { this.place = place; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public int getUser_id() { return user_id; } public void setUser_id(int user_id) { this.user_id = user_id; } }
com/cx/one2one2/User.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.cx.one2one2"> <class name="User" table="t_user"> <id name="userId"> <generator class="native"></generator> </id> <property name="userName" length="20"></property> <!-- 一对一映射 没有外键的一方 --> <one-to-one name="idCard" class="IdCard"></one-to-one> </class> </hibernate-mapping>
com/cx/one2one2/IdCard.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.cx.one2one2"> <class name="IdCard" table="t_IdCard"> <id name="user_id"> <!-- id节点指定是主键映射,即user_id 是主键 主键的生成方式未foreign 即把别的表的主键作为当前表的主键 属性说明 property指定引用的对象 --> <generator class="foreign"> <param name="property">user</param> </generator> </id> <property name="cardNum" length="20"></property> <property name="place" length="20"></property> <!-- 一对一映射,有外键的一方 constrained="true" 指定在主键上添加外键约束 --> <one-to-one name="user" class="User" constrained="true" cascade="save-update"></one-to-one> </class> </hibernate-mapping>
com.cx.one2one2.Test
package com.cx.one2one2; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; /** * Created by cxspace on 16-8-3. */ public class Test { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(IdCard.class) .addClass(User.class) // 测试时候使用 .buildSessionFactory(); } @org.junit.Test public void save(){ Session session = sf.openSession(); session.beginTransaction(); // 用户 User user = new User(); user.setUserName("mike"); // 身份证 IdCard idCard = new IdCard(); idCard.setCardNum("441202XXX"); idCard.setPlace("南昌XXX"); // 关系 idCard.setUser(user); // ----保存---- session.save(idCard); session.getTransaction().commit(); session.close(); } /* * * Hibernate: insert into t_user (userName) values (?) Hibernate: insert intt_projecto t_IdCard (cardNum, place, user_id) values (?, ?, ?) * * */ }