hibernate3学习笔记(十九)|关系映射:一对一 - 编程入门网
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-06-19
et/hibernate-mapping-3.0.dtd">
6.
7.<hibernate-mapping>8.
9. <class name="com.hb3.pack_02.model.Room" table="room">10. <id name="id" column="id">11. <generator class="native"/>12. </id>13.
14. <property name="address"15. column="address"16. type="java.lang.String"/>17.18. <one-to-one name="user"
19. class="com.hb3.pack_02.model.User"
20. property-ref="room"/>
21. </class>22.
23.</hibernate-mapping>
测试代码: 1.package com.hb3.pack_02;2.3.import org.hibernate.Session;4.import org.hibernate.SessionFactory;5.import org.hibernate.Transaction;6.import org.hibernate.cfg.Configuration;7. 8.import com.hb3.pack_02.model.Room;9.import com.hb3.pack_02.model.User;10. 11.public class BusinessService {12. 13. public static void main(String[] args) {14.15. Configuration config = new Configuration().configure();16. SessionFactory sessionFactory = config.buildSessionFactory();17. Session session = sessionFactory.openSession();18. 19. User user1 = new User();20. user1.setName("shenbin");21. Room room1 = new Room();22. room1.setAddress("NTU-M8-419");23. user1.setRoom(room1);24.25. User user2 = new User();26. user2.setName("chanyan");27. Room room2 = new Room();28. room2.setAddress("NTU-M8-418");29. user2.setRoom(room2);30.31. Transaction tx = session.beginTransaction();32. session.save(user1);33. session.save(user2);34. tx.commit();35.36. Room room = (Room) session.load(Room.class, new Integer(1));37. System.out.println(room.getUser().getName());38.39. session.close();40. sessionFactory.close();41. }42.} hibernate3学习笔记(十九)|关系映射:一对一(4)时间:2011-02-03A.主键关联 DDL: 1.CREATE TABLE user (2. id INT(11) NOT NULL auto_increment PRIMARY KEY,3. name VARCHAR(100) NOT NULL default ''''4.);5.6.CREATE TABLE room (7. id INT(11) NOT NULL auto_increment PRIMARY KEY,8. address VARCHAR(100) NOT NULL default ''''9.); Room和User类同上。 Room.hbm.xml 1.<?xml version="1.0" encoding="UTF-8"?>2. 3.<!DOCTYPE hibernate-mapping PUBLIC4. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"5. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 6.7.<hibernate-mapping>8.9. <class name="com.hb3.pack_18.model.Room" table="room"> 10.11. <id name="id" column="id">12. <generator class="foreign">13. <param name="property">user</param> 14. </generator> 15. </id> |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于hibernate3学习笔记(十九)|关系映射:一对一 - 编程入门网的所有评论