many –to –one
假设2个对象,分别是Person and Address .
1个Address中含有多个Person
分别建立类
package cn.itcase.hibernate.domain; public class Address { private int id; private String street; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } } package cn.itcase.hibernate.domain; public class Person { private int id; private String name; private Address address; public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } 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; } }
相对应的配置文件为
<?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="cn.itcase.hibernate.domain"> <class name="Address" table="address"> <id name="id" > <generator class="native" /> </id> <property name="street"></property> </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 package="cn.itcase.hibernate.domain"> <class name="Person" table="person"> <id name="id" > <generator class="native" /> </id> <property name="name" /> <!--留意这里 name的名称必须和address类中的private Address addr;对象名称一致 列名可任意命名,最好是具有相对意义 --> <many-to-one name="address" column="addressId"/> </class> </hibernate-mapping>
//测试多对一例子
package cn.itcast.hibernate; import java.util.HashSet; import java.util.Set; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.Transaction; import cn.itcase.hibernate.domain.Address; import cn.itcase.hibernate.domain.Person; public class many2one2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Person depart=add(); Person emp=query(1); //System.out.println(emp.getDepart().getName()); //queryDepart(3); //System.out.println(addrquery(1).getPer().getName()); } static Address addrquery(int id){ Session s=null; Transaction tx=null; try{ s=HibernateUtil.getSession(); tx=s.beginTransaction(); Address addr=(Address)s.get(Address.class, id); //System.out.print(emp.getDepart().getName()); Hibernate.initialize(addr.getStreet()); tx.commit(); return addr; }finally{ if(s!=null) s.close(); } } static Person query(int pid){ Session s=null; Transaction tx=null; try{ s=HibernateUtil.getSession(); tx=s.beginTransaction(); Person p=(Person)s.get(Person.class, pid); tx.commit(); return p; }finally{ if(s!=null) s.close(); } } static Person add(){ Session s=null; Transaction tx=null; try{ Address addr1=new Address(); addr1.setStreet("add1"); Person p1=new Person(); p1.setName("p1"); Person p2=new Person(); p2.setName("p2"); p1.setAddress(addr1); p2.setAddress(addr1); s=HibernateUtil.getSession(); tx=s.beginTransaction(); s.save(addr1); s.save(p1); s.save(p2); tx.commit(); return p1; }finally{ if(s!=null) s.close(); } } }