以下是说明,1对多 单向外键关联 的实例.同样以person 和 address 为例.
为了方便测试,在原有的Hibernate框架上面,建立两个hbm.xml文件..
一个是Address_One_to_Many.hbm.xml,另外一个是Person_One_to_Many.hbm.xml。
然后,建立两个JavaBean 分别是:Address_One_to_Many 和 Person_One_to_Many
Person_One_to_Many具体内容如下:
/** * 1对多,单向关联 * @author Administrator * */ public class Person_One_to_Many implements Serializable{ private static final long serialVersionUID = -6313867775683964530L; private Integer id; private String name; private Integer age; //一个人,有多个地址; private Set<Address_One_to_Many> setAddress = new HashSet<Address_One_to_Many>(); //getter 和 setter 方法省略 }
Address_One_to_Many 具体代码如下:
/** * * 一对多的关系; * @author Administrator * */ public class Address_One_to_Many implements Serializable{ private static final long serialVersionUID = 3635140598485086087L; private Integer addressID; private String addressDetail; //getter 和 setter方法省略. }
接下来就是配置Mapping映射文档.
Person_One_to_Many.hbm.xml 具体代码如下:
<?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"> <hibernate-mapping package="pack.java.model"> <class name="Person_One_to_Many" table="ZHT_Person_One_To_Many"> <id name="id" column="ID" type="java.lang.Integer"> <!-- mySql中主键id自动生成. --> <generator class="identity"> </generator> </id> <!-- PERSON_ONE_TO_MANY属性的定义 --> <property name="name" column="NAME" type="java.lang.String"></property> <property name="age" column="AGE" type="java.lang.Integer"></property> <!-- 设置Address地址set集合,inverse = false代表主控制端; lazy=false代表不延迟加载. table=ADDRESS_ONE_TO_MANY 指定数据库的ADDRESSS一张表;方便关联查询; cascade=all 支持所有的级联操作; --> <set name="setAddress" inverse="false" lazy="false" table="ADDRESS_ONE_TO_MANY" cascade="all"> <!-- 确定关联的外键列,可以任意取个名字;--> <key column="id" not-null="false"></key> <!-- 1个人多对应多个地址,所以class指向的是Address --> <one-to-many class="Address_One_to_Many"/> </set> </class> </hibernate-mapping>
Address_One_to_Many.hbm.xml:
<?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"> <hibernate-mapping package="pack.java.model"> <class name="Address_One_to_Many" table="ADDRESS_ONE_TO_MANY"> <id column="ADDRESS_ID" name="addressID" type="java.lang.Integer"> <generator class="identity"/> </id> <!-- ADDRESS_ONE_TO_MANY属性的定义; --> <property lazy="false" name="addressDetail" column="ADDRESS_DETAIL" not-null="true"/> </class> </hibernate-mapping>
配置完之后,记得在Hibernate.cfg.xml中加上 刚才新建xxx.hbm.xml文件的映射地址:
比如:
<mapping resource="pack/java/model/Address_One_to_Many.hbm.xml"/>
<mapping resource="pack/java/model/Person_One_to_Many.hbm.xml"/>
最后,在建立一个Test类进行测试刚刚配置的1对多的映射关系.
package pack.java.test; import java.io.Serializable; import java.util.Set; import org.hibernate.Session; import pack.java.hibernate.HibernateSessionFactory; import pack.java.model.Address_One_to_Many; import pack.java.model.Person_One_to_Many; /** * * Hibernate 测试类; 一对多外键单向关联; * @author ZhouHaiTao * */ public class HibernateDemoOneToMany { public static void main(String[] args) { HibernateDemoOneToMany hibernateDemoOneToMany = new HibernateDemoOneToMany(); //保存地址; hibernateDemoOneToMany.saveAddress(); //保存人物; hibernateDemoOneToMany.savePerson(); //根据Person id查询person; hibernateDemoOneToMany.loadPersonByID(1); } /** * 保存Address地址; */ private void saveAddress(){ Session session = getSession(); Address_One_to_Many address1 = new Address_One_to_Many(); address1.setAddressDetail("湖南省株洲市"); Address_One_to_Many address2 = new Address_One_to_Many(); address2.setAddressDetail("广东省深圳市"); Address_One_to_Many address3 = new Address_One_to_Many(); address3.setAddressDetail("湖北省武汉市"); //开始事务; session.beginTransaction().begin(); //单独保存地址; session.save(address1); session.save(address2); session.save(address3); //提交事务; session.beginTransaction().commit(); //关闭session; closeSession(session); } /** * 保存Person,并且给Person指定多个地址; */ private void savePerson(){ //获取session; Session session = getSession(); Person_One_to_Many person1 = new Person_One_to_Many("李四",23); Person_One_to_Many person2 = new Person_One_to_Many("张三",23); Address_One_to_Many address1 = new Address_One_to_Many(); address1.setAddressDetail("湖南省株洲市"); Address_One_to_Many address2 = new Address_One_to_Many(); address2.setAddressDetail("广东省深圳市"); Address_One_to_Many address3 = new Address_One_to_Many(); address3.setAddressDetail("湖北省武汉市"); session.beginTransaction().begin(); //给添加person1对象添加两个地址; person1.getSetAddress().add(address1); person1.getSetAddress().add(address2); //给person2对象,添加一个地址; person2.getSetAddress().add(address3); session.save(person1); session.save(person2); session.beginTransaction().commit(); //关闭session; closeSession(session); } /** * 根据person id查询出,person对象,以及地址; * @param id */ private void loadPersonByID(Serializable id){ Session session = getSession(); Person_One_to_Many personOneToMany = (Person_One_to_Many) session.load(Person_One_to_Many.class, id); System.out.println("打印出Person中的属性信息:"); System.out.println(personOneToMany.getId()+","+personOneToMany.getName()+","+personOneToMany.getAge()); Set<Address_One_to_Many> addressOneToManies = personOneToMany.getSetAddress(); System.out.println("\n打印出Address中的信息:"); for(Address_One_to_Many addressOneToMany : addressOneToManies){ System.out.println(addressOneToMany.getAddressID()+","+addressOneToMany.getAddressDetail()); } closeSession(session); } /** * 创建session; * @return */ private static Session getSession(){ return HibernateSessionFactory.getSession(); } /** * 创建session; * @return */ private static void closeSession(Session session){ session.close(); } }
测试之后,可以在MySql中查看数据数据是否正常插入成功..
以下是控制台输出的信息,代表成功插入数据库:
打印出Person中的属性信息:
1,周海涛,23
打印出Address中的信息:
5,湖南省株洲市
4,广东省深圳市