原文地址,http://blog.csdn.net/love_080808/article/details/3758344
因为我用到了,所以将其整理了一下,以便其他人阅读。
1.建立数据库表
create table room ( id bigint not null IDENTITY(1,1), address varchar(255), primary key (id) ) create table users ( id bigint not null IDENTITY(1,1), name varchar(255), room_id bigint, primary key (id) )
users表示人room表示房间,多个人共有一个房间。
2.实体类Users.java
package com.crazyjava.vo; public class Users implements java.io.Serializable { private static final long serialVersionUID = -1481433969291380841L; private Long id; private String name; private Room room; public Users() { } public Users(String name, Room room) { this.name = name; this.room = room; } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Room getRoom() { return room; } public void setRoom(Room room) { this.room = room; } }
3.实体类Room.java
package com.crazyjava.vo; public class Room implements java.io.Serializable { private static final long serialVersionUID = 6788763073536851614L; private Long id; private String address; public Room() { } public Room(String address) { this.address = address; } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getAddress() { return this.address; } public void setAddress(String address) { this.address = address; } }
4.映射文件
User.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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.crazyjava.vo.Users" table="users" schema="dbo" catalog="bbs"> <id name="id" type="java.lang.Long"> <column name="id" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="name" /> </property> <many-to-one name="room" column="room_id" class="com.crazyjava.vo.Room" cascade="all" outer-join="true"/> </class> </hibernate-mapping>
5.映射文件
Room.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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.crazyjava.vo.Room" table="room" schema="dbo" catalog="bbs"> <id name="id" type="java.lang.Long"> <column name="id" /> <generator class="native" /> </id> <property name="address" type="java.lang.String"> <column name="address" /> </property> </class> </hibernate-mapping>
在<many-to-one>中,cascade表示主控方(Users)进行save-update,delete等操作时,被控方(Room)是否也一并进行相关操作。设定为all表示主控方任何操作,被控方也进行操作。
在查询时,设定outer-join为true的情况下,hibernate将使用以下sql
Hibernate:
select
user0_.id as id0_1_,
user0_.name as name0_1_,
user0_.room_id as room3_0_1_,
room1_.id as id1_0_,
room1_.address as address1_0_
from
user user0_
left outer join
room room1_
on user0_.room_id=room1_.id
where
user0_.id=?
设为false执行sql
Hibernate:
select
user0_.id as id0_0_,
user0_.name as name0_0_,
user0_.room_id as room3_0_0_
from
user user0_
where
user0_.id=?
Hibernate:
select
room0_.id as id1_0_,
room0_.address as address1_0_
from
room room0_
where
room0_.id=?