首页 > 代码库 > Hibernate _one to one 主键关联 XML配置
Hibernate _one to one 主键关联 XML配置
人 与身份证 一对一 主键关联:
1 package com.helen.model; 2 3 public class People1 { 4 private int id; 5 private String name; 6 private int age; 7 private IDcard1 idcard; 8 9 10 public int getId() {11 return id;12 }13 14 public void setId(int id) {15 this.id = id;16 }17 18 public String getName() {19 return name;20 }21 22 public void setName(String name) {23 this.name = name;24 }25 26 public int getAge() {27 return age;28 }29 30 public void setAge(int age) {31 this.age = age;32 }33 34 public IDcard1 getIdcard() {35 return idcard;36 }37 38 public void setIdcard(IDcard1 idcard) {39 this.idcard = idcard;40 }41 42 43 44 }
1 package com.helen.model; 2 3 public class IDcard1 { 4 private int id; 5 private String cardCode; 6 private People1 people; 7 8 public int getId() { 9 return id;10 }11 12 public void setId(int id) {13 this.id = id;14 }15 16 public String getCardCode() {17 return cardCode;18 }19 20 public void setCardCode(String cardCode) {21 this.cardCode = cardCode;22 }23 24 public People1 getPeople() {25 return people;26 }27 28 public void setPeople(People1 people) {29 this.people = people;30 }31 32 }
XML 配置
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <hibernate-mapping> 5 <class name="com.helen.model.People1" table="t_PEOPLE"> 6
7 <id name="id" type="int"> 8 <column name="ID" /> 9 <generator class="native" />10 </id>
11 12 <property name="name" type="java.lang.String">13 <column name="NAME" />14 </property>15 <property name="age" type="int">16 <column name="AGE" />17 </property>18 19 <one-to-one name="idcard" class="com.helen.model.IDcard1" />20
21 </class>22 </hibernate-mapping>
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <hibernate-mapping> 5 <class name="com.helen.model.IDcard1" table="t_IDCARD"> 6 7 <id name="id" type="int"> 8 <column name="ID" /> 9 <generator class="foreign"> //外键10 <param name="property">people</param>11 </generator>12 </id>13 14 15 <property name="cardCode" type="java.lang.String">16 <column name="CARDCODE" />17 </property>18 19 20 <one-to-one name="people" class="com.helen.model.People1" constrained="true"> 22 </one-to-one>
23 </class>24 </hibernate-mapping>
SessionFactory:
1 package com.helen.dao; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.cfg.AnnotationConfiguration; 6 import org.hibernate.cfg.Configuration; 7 8 public class HibernateUtil { 9 private static SessionFactory sf;10 static {11 // sf = new AnnotationConfiguration().configure().buildSessionFactory();12 sf = new Configuration().configure().buildSessionFactory();13 }14 15 public static SessionFactory getSessionFactory() {16 return sf;17 }18 19 public static Session getSession() {20 return sf.openSession();21 }22 }
Test 类:
1 package com.helen.test; 2 3 import org.hibernate.Session; 4 5 import com.helen.dao.HibernateUtil; 6 import com.helen.model.IDcard1; 7 import com.helen.model.People1; 8 9 public class Test {10 static Session session;11 12 public static void main(String args[]) {13 14 session = HibernateUtil.getSession();15 session.beginTransaction();16 People1 p = new People1();17 p.setName("Helen");18 p.setAge(21);19 IDcard1 idcard = new IDcard1();20 idcard.setCardCode("100111011012356");21 idcard.setPeople(p);22 session.save(idcard);23 session.getTransaction().commit();24 session.close();25 }26 27 }
people 表:
IDcard 表:
Hibernate _one to one 主键关联 XML配置
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。