一、数据库的建立(使用MySQL)
create database if not exists `sampledb`;
use `sampledb`;
/*table structure for table `sampledb`.`personinfo` */
drop table if exists `sampledb`.`room`;
create table room (
room_id int not null auto_increment,
address varchar(32) not null default '',
primary key (room_id)
) type=innodb;
create database if not exists `sampledb`;
use `sampledb`;
/*table structure for table `sampledb`.`personinfo` */
drop table if exists `sampledb`.`user`;
create table user (
user_id int not null auto_increment,
name varchar(16) not null default '',
room_id int not null,
index (room_id),
foreign key (room_id) references room(room_id),
primary key (user_id)
) type=innodb;
二、User.java
package com.tanm.test;
publicclass User {
privatelongid;
private String name;
private Room room;
publiclong getId() {
returnid;
}
publicvoid setId(long id) {
this.id = id;
}
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
public Room getRoom() {
returnroom;
}
publicvoid setRoom(Room room) {
this.room = room;
}
}
三、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.tanm.test.User" table="user">
<id name="id" column="user_id" type="long">
<generator class="increment"/>
</id>
<property name="name" column="name" type="string" not-null="true" />
<many-to-one name="room" column="room_id" class="com.tanm.test.Room" />
</class>
</hibernate-mapping>
四、Room.java
package com.tanm.test;
import java.util.HashSet;
import java.util.Set;
publicclass Room {
privatelongid;
private String address;
private Set users = new HashSet();
public String getAddress() {
returnaddress;
}
publicvoid setAddress(String address) {
this.address = address;
}
publiclong getId() {
returnid;
}
publicvoid setId(long id) {
this.id = id;
}
public Set getUsers() {
returnusers;
}
publicvoid setUsers(Set users) {
this.users = users;
}
}
五、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.tanm.test.Room" table="room">
<id name="id" column="room_id" unsaved-value="0">
<generator class="increment"/>
</id>
<property name="address" column="address" type="string" not-null="true" />
<set name="users" table="user" inverse="true" cascade="all">
<key column="room_id"/>
<one-to-many class="com.tanm.test.User"/>
</set>
</class>
</hibernate-mapping>
六、Test.java
package com.tanm.test;
import org.hibernate.*;
import org.hibernate.cfg.*;
publicclass Test {
publicstaticvoid main(String[] args) throws HibernateException {
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
Room room = new Room();
room.setAddress("China_xi'an");
User user1 = new User();
user1.setName("111");
User user2 = new User();
user2.setName("222");
user1.setRoom(room);
user2.setRoom(room);
room.getUsers().add(user1);
room.getUsers().add(user2);
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(room);
tx.commit();
session.close();
sessionFactory.close();
}
}
成功运行后的结果:
Hibernate: insert into room (address, room_id) values (?, ?)
Hibernate: insert into user (name, room_id, user_id) values (?, ?, ?)
Hibernate: insert into user (name, room_id, user_id) values (?, ?, ?)
posted on 2007-10-10 20:53
谭明 阅读(303)
评论(0) 编辑 收藏 所属分类:
Hibernate