数据库脚本:
create table customer (id varchar ( 10 ) primary key ,name varchar ( 10 )); create table address (id varchar ( 10 ) primary key , detail varchar ( 10 ), customerid varchar ( 10 ), foreign key (customerid) references customer(id)); insert into customer values (" 1 ","john"); insert into customer values (" 2 ","marry"); insert into customer values (" 3 ","cathy"); insert into address values (" 1 ","beijng"," 1 "); insert into address values (" 2 ","tianjin"," 1 "); insert into address values (" 3 ","shanghai"," 2 "); insert into address values (" 4 ","guangzhou"," 2 "); insert into address values (" 5 ","chongqing"," 3 "); insert into address values (" 6 ","shenzhen"," 3 "); 或者创建表后用如下语句增加外键约束 alter table address add constraint pf foreign key (customerid) references customer(id);
Domain对象
package ch9.SimpleOneToMany; import java.util.HashSet; import java.util.Set; /** */ /** * Customer generated by MyEclipse Persistence Tools */ public class Customer implements java.io.Serializable ... { // Fields private String id; private String name; private Set addresses = new HashSet( 0 ); // Constructors /** */ /** default constructor */ public Customer() ... { } /** */ /** minimal constructor */ public Customer(String id) ... { this .id = id; } /** */ /** full constructor */ public Customer(String id, String name, Set addresses) ... { this .id = id; this .name = name; this .addresses = addresses; } // Property accessors public String getId() ... { return this .id; } public void setId(String id) ... { this .id = id; } public String getName() ... { return this .name; } public void setName(String name) ... { this .name = name; } public Set getAddresses() ... { return this .addresses; } public void setAddresses(Set addresses) ... { this .addresses = addresses; } }
package ch9.SimpleOneToMany; /** */ /** * Address generated by MyEclipse Persistence Tools */ public class Address implements java.io.Serializable ... { // Fields private String id; private Customer customer; private String detail; // Constructors /** */ /** default constructor */ public Address() ... { } /** */ /** minimal constructor */ public Address(String id) ... { this .id = id; } /** */ /** full constructor */ public Address(String id, Customer customer, String detail) ... { this .id = id; this .customer = customer; this .detail = detail; } // Property accessors public String getId() ... { return this .id; } public void setId(String id) ... { this .id = id; } public Customer getCustomer() ... { return this .customer; } public void setCustomer(Customer customer) ... { this .customer = customer; } public String getDetail() ... { return this .detail; } public void setDetail(String detail) ... { this .detail = detail; } }
hbm文件:
<? xml version="1.0" encoding="utf-8" ?> <! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> < hibernate-mapping > < class name ="ch9.SimpleOneToMany.Customer" table ="customer" catalog ="spring" > < id name ="id" type ="java.lang.String" > < column name ="id" length ="10" /> < generator class ="assigned" /> </ id > < property name ="name" type ="java.lang.String" > < column name ="name" length ="10" /> </ property > < set name ="addresses" inverse ="true" lazy ="false" > < key > < column name ="customerid" length ="10" /> </ key > < one-to-many class ="ch9.SimpleOneToMany.Address" /> </ set > </ class > </ hibernate-mapping >
<? xml version="1.0" encoding="utf-8" ?> <! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> < hibernate-mapping > < class name ="ch9.SimpleOneToMany.Address" table ="address" catalog ="spring" > < id name ="id" type ="java.lang.String" > < column name ="id" length ="10" /> < generator class ="assigned" /> </ id > < many-to-one name ="customer" class ="ch9.SimpleOneToMany.Customer" lazy ="false" > < column name ="customerid" length ="10" /> </ many-to-one > < property name ="detail" type ="java.lang.String" > < column name ="detail" length ="10" /> </ property > </ class > </ hibernate-mapping >
DAO接口:
package ch9.SimpleOneToMany; import java.util.List; public interface IDAO ... { public List getAllCustomer(); }
DAO实现:
package ch9.SimpleOneToMany; import java.util.ArrayList; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class TestDAOImpl extends HibernateDaoSupport implements IDAO ... { public List getAllCustomer() ... { return getHibernateTemplate().find( " from Customer " ); } }
配置文件:
<? xml version="1.0" encoding="UTF-8" ?> < beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" > < bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" > < property name ="driverClassName" > < value > com.mysql.jdbc.Driver </ value > </ property > < property name ="username" > < value > root </ value > </ property > < property name ="password" > < value > 1234 </ value > </ property > < property name ="url" > < value > jdbc:mysql://localhost:3306/spring </ value > </ property > </ bean > < bean id ="sessionFactory" class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" > < property name ="dataSource" > < ref bean ="dataSource" /> </ property > < property name ="mappingResources" > < list > < value > ch9/SimpleOneToMany/Address.hbm.xml </ value > < value > ch9/SimpleOneToMany/Customer.hbm.xml </ value > </ list > </ property > < property name ="hibernateProperties" > < props > < prop key ="hibernate.dialect" > org.hibernate.dialect.MySQL5Dialect </ prop > < prop key ="hibernate.show_sql" > true </ prop > </ props > </ property > </ bean > < bean id ="testDAO" class ="ch9.SimpleOneToMany.TestDAOImpl" > < property name ="sessionFactory" > < ref bean ="sessionFactory" /> </ property > </ bean > </ beans >
测试代码:
package ch9.SimpleOneToMany; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test ... { /** */ /** * @param args */ public static void main(String[] args) ... { ApplicationContext context = new ClassPathXmlApplicationContext( " ch9/SimpleOneToMany/applicationContext.xml " ); TestDAOImpl testDAOImpl = (TestDAOImpl)context.getBean( " testDAO " ); List result = new ArrayList(); result = testDAOImpl.getAllCustomer(); for (Iterator iter = result.iterator(); iter.hasNext();) ... { Customer element = (Customer) iter.next(); System.out.println(element.getName() + element.getAddresses().size()); Iterator iter1 = element.getAddresses().iterator(); while (iter1.hasNext()) ... { Address element1 = (Address) iter1.next(); System.out.println( " -- " + element1.getDetail()); } } } }
结果:
Hibernate: select customer0_.id as id1_, customer0_.name as name1_ from spring.customer customer0_ Hibernate: select addresses0_.customerid as customerid1_, addresses0_.id as id1_, addresses0_.id as id0_0_, addresses0_.customerid as customerid0_0_, addresses0_.detail as detail0_0_ from spring.address addresses0_ where addresses0_.customerid=? Hibernate: select addresses0_.customerid as customerid1_, addresses0_.id as id1_, addresses0_.id as id0_0_, addresses0_.customerid as customerid0_0_, addresses0_.detail as detail0_0_ from spring.address addresses0_ where addresses0_.customerid=? Hibernate: select addresses0_.customerid as customerid1_, addresses0_.id as id1_, addresses0_.id as id0_0_, addresses0_.customerid as customerid0_0_, addresses0_.detail as detail0_0_ from spring.address addresses0_ where addresses0_.customerid=?john2 --tianjin --beijng marry2 --guangzhou --shanghai cathy2 --shenzhen --chongqing