说明:一个部门有若干个子部门,子部门还可以有子部门,本文通过这个演示myeclipse如何实现这种树形关系的持久化。
操作系统:windows xp professional 中文版
1、建立mysql5数据库testdb,脚本下面已经给出。
2、配置myeclipse的数据库服务器,并建立名称为mysql5的数据库链接。
3、建议myeclipse的web工程,名称为dx_d2yzs,并加入hibernate支持,选择hibernate3.1,最高就支持到3.1。
4、在myeclipse的数据库视图中链接数据库并通过表生成实体PO和配置文件,中间不生成DAO。
drop table if exists part;
-- alter table part drop foreign key fk_part;
id bigint not null primary key,
alter table part add index fk_part (father_id),
add constraint fk_part foreign key (father_id) references part(id);
| father_id |----------|<FK>
特别注意:因为有外键约束,需要事务支持,在安装数据库的时候,需要配置mysql数据库服务器的参数。数据库的引擎应该用InnoDB,关闭了自动提交模式,也就是SET AUTOCOMMIT=0。
------------------------------------
#Server=D:/mysql-5.0.37-win32/bin/mysqld.exe
# set basedir to your installation path
#basedir=D:/mysql-5.0.37-win32
# set datadir to the location of your data directory
read_rnd_buffer_size = 256K
default-character-set=gbk
init_connect='SET AUTOCOMMIT=0'
default-table-type=InnoDB
init_connect='set completion_type=1'
default-character-set=gbk
------------------------------------
public class Part implements java.io.Serializable {
private Part part; //父Part
private Set parts = new HashSet(0); //字Part
/** default constructor */
/** minimal constructor */
public Part(String name) {
public Part(Long id, Part part, String name, Set parts) {
public void setId(Long id) {
public void setPart(Part part) {
public String getName() {
public void setName(String name) {
public void setParts(Set parts) {
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
Mapping file autogenerated by MyEclipse Persistence Tools
<class name="org.lavasoft.Part" table="part">
<id name="id" type="java.lang.Long">
<generator class="increment" />
<property name="name" type="java.lang.String">
<column name="name" length="20" />
<many-to-one name="part" class="org.lavasoft.Part" fetch="select"> <column name="father_id" /> <set name="parts" cascade="save-update" inverse="true"> <column name="father_id" /> <one-to-many class="org.lavasoft.Part" /> import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
public static void main(String[] args) {
// TODO Auto-generated method stub
//if(HibernateSessionFactory.getSession()==null) System.out.println("null");
Part p11=new Part("p11");
Part p12=new Part("p12");
Session session = HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();
} catch (HibernateException e) {
// TODO Auto-generated catch block
------------------------------------------
Hibernate: select max(id) from part
Hibernate: insert into part (name, father_id, id) values (?, ?, ?)
Hibernate: insert into part (name, father_id, id) values (?, ?, ?)
Hibernate: insert into part (name, father_id, id) values (?, ?, ?)
------------------------------------------
D:\mysql-5.0.37-win32\bin>mysql -uroot -pleizhimin
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.0.37-community MySQL Community Edition (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id | bigint(20) | NO | PRI | | |
| name | varchar(20) | YES | | NULL | |
| father_id | bigint(20) | YES | MUL | NULL | |
+-----------+-------------+------+-----+---------+-------+
mysql> select * from part;
+----+------+-----------+
| id | name | father_id |
+----+------+-----------+
+----+------+-----------+
测试结果表明,保存父机关的时候,可以级联保存父机关下的子机关。
总结:这个表建立好后,由myeclipse生成的PO不需要做任何改动,生成的mapping也需要添加一个cascade="save-update"。然后就直接写测试类进行测试。
本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/30183,如需转载请自行联系原作者