08-07
11
Hibernate多对一自身关联
作者:Java伴侣 日期:2008-07-11
步骤:
1)建立mysql5数据库testdb,脚本下面已经给出。
2)配置myeclipse的数据库服务器,并建立名称为mysql5的数据库链接。
3)建议myeclipse的web工程,名称为dx_d2yzs,并加入hibernate支持,选择hibernate3.1,最高就支持到3.1。
4)在myeclipse的数据库视图中链接数据库并通过表生成实体PO和配置文件,中间不生成DAO。
5)检查配置文件的正确性,然后测试类进行测试。
一.建立数据库的脚本:
表关系的逻辑图:
特别注意:因为有外键约束,需要事务支持,在安装数据库的时候,需要配置mysql数据库服务器的参数。数据库的引擎应该用InnoDB,关闭了自动提交模式,也就是SET AUTOCOMMIT=0。
my.ini
------------------------------------
**********************************************************
二.通过myeclipse生成实体和配置文件:
Part.java
---------------------
Part.hbm.xml
**********************************************************
三.写测试类进行测试:
**********************************************************
四.运行测试类Test,控制台打印信息:
**********************************************************
最后在去数据库查询1下.
1)建立mysql5数据库testdb,脚本下面已经给出。
2)配置myeclipse的数据库服务器,并建立名称为mysql5的数据库链接。
3)建议myeclipse的web工程,名称为dx_d2yzs,并加入hibernate支持,选择hibernate3.1,最高就支持到3.1。
4)在myeclipse的数据库视图中链接数据库并通过表生成实体PO和配置文件,中间不生成DAO。
5)检查配置文件的正确性,然后测试类进行测试。
一.建立数据库的脚本:
复制内容到剪贴板 程序代码
drop table if exists part;
-- alter table part drop foreign key fk_part;
create table part(
id bigint not null primary key,
name varchar(20),
father_id bigint
);
alter table part add index fk_part (father_id),
add constraint fk_part foreign key (father_id) references part(id);
-- alter table part drop foreign key fk_part;
create table part(
id bigint not null primary key,
name varchar(20),
father_id bigint
);
alter table part add index fk_part (father_id),
add constraint fk_part foreign key (father_id) references part(id);
表关系的逻辑图:
引用内容
+-----------+
| Part |
+-----------+
| id |<---------|<PK>
| name | |
| father_id |----------|<FK>
+-----------+
| Part |
+-----------+
| id |<---------|<PK>
| name | |
| father_id |----------|<FK>
+-----------+
特别注意:因为有外键约束,需要事务支持,在安装数据库的时候,需要配置mysql数据库服务器的参数。数据库的引擎应该用InnoDB,关闭了自动提交模式,也就是SET AUTOCOMMIT=0。
my.ini
------------------------------------
复制内容到剪贴板 程序代码
#[WinMySQLAdmin]
#Server=D:/mysql-5.0.37-win32/bin/mysqld.exe
[mysqld]
# set basedir to your installation path
#basedir=D:/mysql-5.0.37-win32
# set datadir to the location of your data directory
port = 3306
key_buffer = 16K
max_allowed_packet = 1M
table_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 64K
datadir=data
default-character-set=gbk
init_connect='SET AUTOCOMMIT=0'
default-table-type=InnoDB
init_connect='set completion_type=1'
default-character-set=gbk
#Server=D:/mysql-5.0.37-win32/bin/mysqld.exe
[mysqld]
# set basedir to your installation path
#basedir=D:/mysql-5.0.37-win32
# set datadir to the location of your data directory
port = 3306
key_buffer = 16K
max_allowed_packet = 1M
table_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 64K
datadir=data
default-character-set=gbk
init_connect='SET AUTOCOMMIT=0'
default-table-type=InnoDB
init_connect='set completion_type=1'
default-character-set=gbk
**********************************************************
二.通过myeclipse生成实体和配置文件:
Part.java
---------------------
复制内容到剪贴板 程序代码
public class Part implements java.io.Serializable {
// Fields
private Long id;
private String name;
private Part part; //父Part
private Set parts = new HashSet(0); //字Part
// Constructors
/** default constructor */
public Part() {
}
/** minimal constructor */
public Part(Long id) {
this.id = id;
}
public Part(String name) {
this.name = name;
}
/** full constructor */
public Part(Long id, Part part, String name, Set parts) {
this.id = id;
this.part = part;
this.name = name;
this.parts = parts;
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public Part getPart() {
return this.part;
}
public void setPart(Part part) {
this.part = part;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set getParts() {
return this.parts;
}
public void setParts(Set parts) {
this.parts = parts;
}
}
// Fields
private Long id;
private String name;
private Part part; //父Part
private Set parts = new HashSet(0); //字Part
// Constructors
/** default constructor */
public Part() {
}
/** minimal constructor */
public Part(Long id) {
this.id = id;
}
public Part(String name) {
this.name = name;
}
/** full constructor */
public Part(Long id, Part part, String name, Set parts) {
this.id = id;
this.part = part;
this.name = name;
this.parts = parts;
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public Part getPart() {
return this.part;
}
public void setPart(Part part) {
this.part = part;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set getParts() {
return this.parts;
}
public void setParts(Set parts) {
this.parts = parts;
}
}
Part.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="org.lavasoft.Part" table="part">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
<many-to-one name="part" class="org.lavasoft.Part" fetch="select">
<column name="father_id" />
</many-to-one>
<set name="parts" cascade="save-update" inverse="true">
<key>
<column name="father_id" />
</key>
<one-to-many class="org.lavasoft.Part" />
</set>
</class>
</hibernate-mapping>
<!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="org.lavasoft.Part" table="part">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
<many-to-one name="part" class="org.lavasoft.Part" fetch="select">
<column name="father_id" />
</many-to-one>
<set name="parts" cascade="save-update" inverse="true">
<key>
<column name="father_id" />
</key>
<one-to-many class="org.lavasoft.Part" />
</set>
</class>
</hibernate-mapping>
三.写测试类进行测试:
**********************************************************
复制内容到剪贴板 程序代码
package org.lavasoft;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//if(HibernateSessionFactory.getSession()==null) System.out.println("null");
Part p1=new Part("p1");
Part p11=new Part("p11");
Part p12=new Part("p12");
p1.getParts().add(p11);
p1.getParts().add(p12);
p11.setPart(p1);
p12.setPart(p1);
Session session = HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();
try {
session.save(p1);
tx.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
tx.rollback();
}finally{
session.close();
}
}
}
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//if(HibernateSessionFactory.getSession()==null) System.out.println("null");
Part p1=new Part("p1");
Part p11=new Part("p11");
Part p12=new Part("p12");
p1.getParts().add(p11);
p1.getParts().add(p12);
p11.setPart(p1);
p12.setPart(p1);
Session session = HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();
try {
session.save(p1);
tx.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
tx.rollback();
}finally{
session.close();
}
}
}
四.运行测试类Test,控制台打印信息:
**********************************************************
引用内容
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 (?, ?, ?)
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 (?, ?, ?)
最后在去数据库查询1下.
评论: 0 | 引用: 0 | 查看次数: 804
发表评论