Hibernate 关联映射之一对一
Hibernate的关联映射包括:
一对一(Persion - IDCard)
一对多(Department - Employee)
多对一(Employee - Department)
多对多(Teacher - Student)
组件映射(User - Name)
集合映射(Set、List、Map)
inverse和cascade(Employee - Department)
本文以一对一为例:
一个人对应一个身份证(Persion- IdCard)
(1) 基于主键的one-to-one(Persion的配置文件)
从对象IdCard使用了和主对象Person相同的主键,IdCard的主键在此即充当了主键又充当的外键。
<id name="id">
<generator class="foreign"/>
<param name="property">idCard</param>
</generator>
</id>
<one-to-one name="idCard" constrained="true">
实体类:Persion.java 和 IdCard.java
package com.cos.entity; public class Person { private int id; private String name; private IdCard idCard;//一对一 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public IdCard getIdCard() { return idCard; } public void setIdCard(IdCard idCard) { this.idCard = idCard; } }
package com.cos.entity; public class IdCard { private int id; private String idNo; private Person person;//一对一 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getIdNo() { return idNo; } public void setIdNo(String idNo) { this.idNo = idNo; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } }
映射文件:Person.hbm.xml 和 IdCard.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="com.cos.entity.Person" lazy="true"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <one-to-one name="idCard"/> </class> </hibernate-mapping>
<?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="com.cos.entity.IdCard" table="id_card" lazy="true"> <!-- 此id既是主键又是外键 --> <id name="id"> <generator class="foreign"> <!-- 此外键的值根据one-to-one中的person获得 --> <param name="property">person</param> </generator> </id> <property name="idNo"/> <!-- constrained="true"表示是受约束的 --> <one-to-one name="person" constrained="true"/> </class> </hibernate-mapping>
Hibernate配置文件:hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hi</property> <property name="hibernate.connection.username"></property> <property name="hibernate.connection.password"></property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">create</property> <property name="show_sql">true</property> <mapping resource="com/cos/entity/Person.hbm.xml"/> <mapping resource="com/cos/entity/IdCard.hbm.xml"/> </session-factory> </hibernate-configuration>
工具类:HibernateUtil.java
package com.cos.util; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public final class HibernateUtil { private static SessionFactory sesseionFactory; static { Configuration conf = new Configuration(); conf.configure(); sesseionFactory = conf.buildSessionFactory(); } public static SessionFactory getSesseionFactory() { return sesseionFactory; } }
测试类:One2One.java
package com.cos.main; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import com.cos.entity.IdCard; import com.cos.entity.Person; import com.cos.util.HibernateUtil; public class One2One { public static void main(String[] args) { add(); } public static void add(){ SessionFactory sessionFactory = null; Session s = null; Transaction t = null; try{ sessionFactory = HibernateUtil.getSesseionFactory(); s = sessionFactory.openSession(); t = s.beginTransaction(); Person person = new Person(); person.setName("person 1"); IdCard idCard = new IdCard(); idCard.setIdNo("450846222002212131"); idCard.setPerson(person); s.save(person); s.save(idCard); t.commit(); s.close(); sessionFactory.close(); }catch(Exception e){ e.printStackTrace(); } } }
测试类(查询):One2One.java
package com.cos.main; import org.hibernate.Session; import org.hibernate.SessionFactory; import com.cos.entity.IdCard; import com.cos.entity.Person; import com.cos.util.HibernateUtil; public class One2One { public static void main(String[] args) { query(1);//Person为主对象 query1(1);//IdCard为从对象 } //查询主对象 public static void query(int id){ SessionFactory sessionFactory = null; Session s = null; try{ sessionFactory = HibernateUtil.getSesseionFactory(); s = sessionFactory.openSession(); Person person = (Person)s.get(Person.class, id); System.out.println(person.getIdCard().getIdNo()); s.close(); sessionFactory.close(); }catch(Exception e){ e.printStackTrace(); } } //查询从对象 public static void query1(int idCard){ SessionFactory sessionFactory = null; Session s = null; try{ sessionFactory = HibernateUtil.getSesseionFactory(); s = sessionFactory.openSession(); IdCard idc = (IdCard)s.get(IdCard.class,idCard); System.out.println(idc.getPerson().getName()); s.close(); sessionFactory.close(); }catch(Exception e){ e.printStackTrace(); } } }
表结构:person表 和id_card表
CREATE TABLE `person` ( `id` int(11) NOT NULL auto_increment, `name` varchar(255) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 CREATE TABLE `id_card` ( `id` int(11) NOT NULL, `idNo` varchar(255) default NULL, PRIMARY KEY (`id`), KEY `FK627C1FB48F4EE9A7` (`id`), CONSTRAINT `FK627C1FB48F4EE9A7` FOREIGN KEY (`id`) REFERENCES `person` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1
(2) 基于外键的one-to-one 可以描述为多对一,加unique="true"约束
从对象IdCard和主对象Person使用了各自的主键
Persion.hbm.xml的配置文件:
<one-to-one name="idCard" property-ref="person"/>
IdCard.hbm.xml的配置文件:
<many-to-one name="person" column="person_id" unique="true" not-null="true"/>
<?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="com.cos.entity.Person" lazy="true"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <one-to-one name="idCard" property-ref="person"/> </class> </hibernate-mapping>
<?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="com.cos.entity.IdCard" table="id_card" lazy="true"> <id name="id"> <generator class="native"/> </id> <property name="idNo"/> <many-to-one name="person" column="person_id" unique="true"/> </class> </hibernate-mapping>
表结构:
CREATE TABLE `person` ( `id` int(11) NOT NULL auto_increment, `name` varchar(255) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 CREATE TABLE `id_card` ( `id` int(11) NOT NULL auto_increment, `idNo` varchar(255) default NULL, `person_id` int(11) default NULL, PRIMARY KEY (`id`), UNIQUE KEY `person_id` (`person_id`), KEY `FK627C1FB4C22976D1` (`person_id`), CONSTRAINT `FK627C1FB4C22976D1` FOREIGN KEY (`person_id`) REFERENCES `person` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1