数据表设计如下图:
DDL文件:
- CREATE TABLE user (
- id INT(11) NOT NULL auto_increment PRIMARY KEY,
- name VARCHAR(100) NOT NULL default '',
- room_id INT(11)
- );
- CREATE TABLE room (
- id INT(11) NOT NULL auto_increment PRIMARY KEY,
- address VARCHAR(100) NOT NULL default ''
- );
Room.java
- package com.hb3.pack_16.model;
- public class Room {
- private Integer id;
- private String address;
-
- public Room() {
- }
-
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- }
User.java
- package com.hb3.pack_16.model;
- public class User {
- private Integer id;
- private String name;
- private Room room;
- public User() {
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Room getRoom() {
- return room;
- }
- public void setRoom(Room room) {
- this.room = room;
- }
- }
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">
- <hibernate-mapping>
- <class name="com.hb3.pack_16.model.Room" table="room">
-
- <id name="id" column="id">
- <generator class="native"/>
- </id>
- <property name="address"
- column="address"
- type="java.lang.String"/>
- </class>
- </hibernate-mapping>
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">
- <hibernate-mapping>
- <class name="com.hb3.pack_16.model.User" table="user">
-
- <id name="id" column="id" type="java.lang.Integer">
- <generator class="native"/>
- </id>
- <property name="name" column="name" type="java.lang.String"/>
-
- <many-to-one name="room"
- column="room_id"
- class="com.hb3.pack_16.model.Room"
- cascade="all"
- outer-join="true"/>
- </class>
- </hibernate-mapping>
测试代码:
- package com.hb3.pack_16;
- import java.io.IOException;
- import java.sql.SQLException;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import com.hb3.pack_16.model.Room;
- import com.hb3.pack_16.model.User;
- public class BusinessService {
- public static void main(String[] args) throws IOException, SQLException {
-
- Configuration config = new Configuration().configure();
- SessionFactory sessionFactory = config.buildSessionFactory();
- Session session = sessionFactory.openSession();
- Room room1 = new Room();
- room1.setAddress("NTU-M8-419");
- Room room2 = new Room();
- room2.setAddress("NTU-G3-302");
-
- User user1 = new User();
- user1.setName("shenbin");
- user1.setRoom(room1);
-
- User user2 = new User();
- user2.setName("chenyan");
- user2.setRoom(room1);
-
- User user3 = new User();
- user3.setName("yangye");
- user3.setRoom(room2);
- Transaction tx = session.beginTransaction();
- session.save(user1);
- session.save(user2);
- session.save(user3);
- tx.commit();
- User user = (User) session.load(User.class, new Integer(1));
- System.out.println(user.getName());
- System.out.println(user.getRoom().getAddress());
-
- session.close();
- sessionFactory.close();
- }
- }
在<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教程