数据表设计如下图:

DDL文件:

  1. CREATE TABLE user (
  2.     id INT(11) NOT NULL auto_increment PRIMARY KEY,
  3.     name VARCHAR(100) NOT NULL default '',
  4.     room_id INT(11)
  5. );

  6. CREATE TABLE room (
  7.     id INT(11) NOT NULL auto_increment PRIMARY KEY,
  8.     address VARCHAR(100) NOT NULL default ''
  9. );

Room.java

  1. package com.hb3.pack_16.model;

  2. public class Room {

  3.     private Integer id; 
  4.     private String address; 
  5.     
  6.     public Room() {
  7.     }
  8.     
  9.     public Integer getId() { 
  10.         return id; 
  11.     } 
  12.     public void setId(Integer id) { 
  13.         this.id = id; 
  14.     }
  15.     public String getAddress() { 
  16.         return address; 
  17.     }
  18.     public void setAddress(String address) { 
  19.         this.address = address; 
  20.     }
  21. }

User.java

  1. package com.hb3.pack_16.model;

  2. public class User {

  3.     private Integer id;
  4.     private String name;
  5.     private Room room;

  6.     public User() {
  7.     }

  8.     public Integer getId() {
  9.         return id;
  10.     }
  11.     public void setId(Integer id) {
  12.         this.id = id;
  13.     }
  14.     public String getName() {
  15.         return name;
  16.     }
  17.     public void setName(String name) {
  18.         this.name = name;
  19.     }
  20.     public Room getRoom() {
  21.         return room;
  22.     }
  23.     public void setRoom(Room room) {
  24.         this.room = room;
  25.     }
  26. }

Room.hbm.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping 
  3.     PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

  5. <hibernate-mapping> 

  6.     <class name="com.hb3.pack_16.model.Room" table="room">
  7.     
  8.         <id name="id" column="id"> 
  9.             <generator class="native"/> 
  10.         </id> 

  11.         <property name="address" 
  12.                   column="address" 
  13.                   type="java.lang.String"/> 
  14.     </class> 

  15. </hibernate-mapping>

User.hbm.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping 
  3.     PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

  5. <hibernate-mapping> 

  6.     <class name="com.hb3.pack_16.model.User" table="user">
  7.     
  8.         <id name="id" column="id" type="java.lang.Integer"> 
  9.             <generator class="native"/> 
  10.         </id> 

  11.         <property name="name" column="name" type="java.lang.String"/> 
  12.         
  13.         <many-to-one name="room" 
  14.                      column="room_id" 
  15.                      class="com.hb3.pack_16.model.Room"
  16.                      cascade="all"
  17.                      outer-join="true"/>         
  18.     </class> 

  19. </hibernate-mapping>

测试代码:

  1. package com.hb3.pack_16;

  2. import java.io.IOException;
  3. import java.sql.SQLException;

  4. import org.hibernate.Session;
  5. import org.hibernate.SessionFactory;
  6. import org.hibernate.Transaction;
  7. import org.hibernate.cfg.Configuration;

  8. import com.hb3.pack_16.model.Room;
  9. import com.hb3.pack_16.model.User;


  10. public class BusinessService {

  11.     public static void main(String[] args) throws IOException, SQLException {
  12.         
  13.         Configuration config = new Configuration().configure();
  14.         SessionFactory sessionFactory = config.buildSessionFactory();      
  15.         Session session = sessionFactory.openSession();

  16.         Room room1 = new Room(); 
  17.         room1.setAddress("NTU-M8-419"); 
  18.         Room room2 = new Room(); 
  19.         room2.setAddress("NTU-G3-302"); 
  20.                 
  21.         User user1 = new User(); 
  22.         user1.setName("shenbin"); 
  23.         user1.setRoom(room1); 
  24.                 
  25.         User user2 = new User(); 
  26.         user2.setName("chenyan"); 
  27.         user2.setRoom(room1); 
  28.                 
  29.         User user3 = new User(); 
  30.         user3.setName("yangye"); 
  31.         user3.setRoom(room2); 

  32.         Transaction tx = session.beginTransaction();   
  33.         session.save(user1);
  34.         session.save(user2);
  35.         session.save(user3);    
  36.         tx.commit();

  37.         User user = (User) session.load(User.classnew Integer(1));
  38.         System.out.println(user.getName());
  39.         System.out.println(user.getRoom().getAddress());

  40.         
  41.         session.close();
  42.         sessionFactory.close();
  43.     }
  44. }

在<many-to-one>的设定中:

cascade表示操作时的级联关系。表示主控方在进行增删改操作时,被控方是否进行相关操作。

outer-join表示查询时是否使用外连接查询方式。

outer-join为true的时候,生成的查询SQL为:

Hibernate:
select user0_.id as id1_, user0_.name as name0_1_, user0_.room_id as room3_0_1_,
room1_.id as id0_, room1_.address as address1_0_ from user user0_
left outer join room room1_ on user0_.room_id=room1_.id where user0_.id=?

outer-join为false的时候,生成的查询SQL为:Hibernate:
select user0_.id as id0_, user0_.name as name0_0_, user0_.room_id as room3_0_0_
from user user0_ where user0_.id=?
Hibernate:
select room0_.id as id0_, room0_.address as address1_0_
from room room0_ where room0_.id=?

 

不过最好使用fetch属性来表示,及fetch="join"或者fetch="select"。



ExtJS教程- Hibernate教程-Struts2 教程-Lucene教程