**********
一,多对一
1,部门和员工:多对一
Department.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.maple.hibernate.domain"> <class name="Department"> <id name="id"> <generator class="native" /> </id> <property name="name" /> </class> </hibernate-mapping>
Employee.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.maple.hibernate.domain"> <class name="Employee"> <id name="id"> <generator class="native" /> </id> <property name="name" /> <many-to-one name="depart" column="depart_id"/> </class> </hibernate-mapping>
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://localhost:3306/test</property> <property name="hibernate.connection.username">root </property> <property name="hibernate.connection.password">root </property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">create</property> <property name="show_sql">true</property> <mapping resource="com/maple/hibernate/domain/Department.hbm.xml"/> <mapping resource="com/maple/hibernate/domain/Employee.hbm.xml"/> </session-factory> </hibernate-configuration>
Department.java
package com.maple.hibernate.domain; public class Department { private int id; private String name; 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; } }
Employee.java
package com.maple.hibernate.domain; public class Employee { private int id; private String name; private Department depart; 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 Department getDepart() { return depart; } public void setDepart(Department depart) { this.depart = depart; } }
测试代码
package com.maple.hibernate; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.Transaction; import com.maple.hibernate.domain.Department; import com.maple.hibernate.domain.Employee; public class Many2One { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub add(); //System.out.println("ff"); Employee emp=query(1); System.out.println("ffff depart name:"+emp.getDepart().getName());//如果在query方法中没有 } static Employee query(int employeeid){ Session s=null; try{ s=HibernateUtil.getSession(); Employee emp=(Employee) s.get(Employee.class, employeeid); Hibernate.initialize(emp.getDepart());//若无,main中打印语句出错 // System.out.println("depart name:"+emp.getDepart().getName());//可以 return emp; }finally{ if(s!=null){ s.close(); } } } static Department add(){ Session s=null; Transaction tx=null; try{ s=HibernateUtil.getSession(); Department depart=new Department(); depart.setName("software"); Employee emp=new Employee(); emp.setDepart(depart); emp.setName("aaa"); tx=s.beginTransaction(); s.save(depart); //如果放下面,employee会多出一条更新语句 s.save(emp); tx.commit(); return depart; }finally{ if(s!=null){ s.close(); } } } }
二,一对一
***基于主键的一对一关联
一个人有一个身份证号,一个身份证属于一个人
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 package="com.maple.hibernate.domain"> <class name="IdCard" table="id_card"> <id name="id"> <generator class="foreign"> //id_card表的主键是参照person表的主键 <param name="property">person</param> </generator> </id> <property name="usefulLife" column="useful_life"/> <one-to-one name="person" constrained="true"/> //如果没有设置constrained,则表结构中体现不出外键关系 </class> </hibernate-mapping>
person.hbm.xml
<class name="Person"> <id name="id"> <generator class="native" /> </id> <property name="name" /> <one-to-one name="idCard"/> </class>
Person.java IdCard.java
public class Person { private int id; private String name; private IdCard idCard; } import java.util.Date; public class IdCard { private int id; private Date usefulLife; private Person person; }
一,
//Person p=(Person) session.get(Person.class, id);
//System.out.println(p.getIdCard().getUsefulLife());
二,
IdCard card=(IdCard) session.get(IdCard.class, id);
System.out.println(card.getPerson().getName());
上面两种都是person表 left outer join id_card表
基于外键的一对一
<class name="IdCard" table="id_card"> <id name="id"> <generator class="native"/> <!-- <generator class="foreign"> <param name="property">person</param> </generator> --> </id> <property name="usefulLife" column="useful_life"/> <!-- <one-to-one name="person" constrained="true"/> --> <many-to-one name="person" column="person_id" unique="true"/> </class> <class name="Person"> <id name="id"> <generator class="native" /> </id> <property name="name" /> <!-- <one-to-one name="idCard"/> --> <one-to-one name="idCard" property-ref="person"/> </class>
**********