1.映射关系:树型结构中当前节点与其父节点是many-to-one的关系,与其子节点是one-to-many的关系;
2.创建:将节点的所有子节点递归存入数据库;
3.查询:递归查找节点的所有子节点;
hibernate.cfg.xml:
01.
<?
xml
version
=
'1.0'
encoding
=
'UTF-8'
?>
02.
<!DOCTYPE hibernate-configuration PUBLIC
03.
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
05.
06.
<!-- Generated by MyEclipse Hibernate Tools. -->
07.
<
hibernate-configuration
>
08.
09.
<
session-factory
>
10.
<
property
name
=
"connection.username"
>scott</
property
>
11.
<
property
name
=
"connection.url"
>
12.
jdbc:oracle:thin:@127.0.0.1:1521:MGC
13.
</
property
>
14.
<
property
name
=
"dialect"
>
15.
org.hibernate.dialect.Oracle9Dialect
16.
</
property
>
17.
<
property
name
=
"myeclipse.connection.profile"
>MGC</
property
>
18.
<
property
name
=
"connection.password"
>tiger</
property
>
19.
<
property
name
=
"connection.driver_class"
>
20.
oracle.jdbc.driver.OracleDriver
21.
</
property
>
22.
23.
<
property
name
=
"show_sql"
>false</
property
>
24.
<
mapping
resource
=
"cn/edu/ahau/mgc/hibernate/pojo/Node.hbm.xml"
/>
25.
26.
</
session-factory
>
27.
28.
</
hibernate-configuration
>
Node.java:
01.
package
cn.edu.ahau.mgc.hibernate.pojo;
02.
03.
import
java.util.Set;
04.
05.
public
class
Node {
06.
07.
private
long
id;
08.
private
String name;
09.
private
int
level;
10.
private
boolean
leaf;
11.
private
Node parent;
12.
private
Set<Node> children;
13.
14.
public
long
getId() {
15.
return
id;
16.
}
17.
18.
public
void
setId(
long
id) {
19.
this
.id = id;
20.
}
21.
22.
public
String getName() {
23.
return
name;
24.
}
25.
26.
public
void
setName(String name) {
27.
this
.name = name;
28.
}
29.
30.
public
int
getLevel() {
31.
return
level;
32.
}
33.
34.
public
void
setLevel(
int
level) {
35.
this
.level = level;
36.
}
37.
38.
public
boolean
isLeaf() {
39.
return
leaf;
40.
}
41.
42.
public
void
setLeaf(
boolean
leaf) {
43.
this
.leaf = leaf;
44.
}
45.
46.
public
Node getParent() {
47.
return
parent;
48.
}
49.
50.
public
void
setParent(Node parent) {
51.
this
.parent = parent;
52.
}
53.
54.
public
Set<Node> getChildren() {
55.
return
children;
56.
}
57.
58.
public
void
setChildren(Set<Node> children) {
59.
this
.children = children;
60.
}
61.
}
Node.hbm.xml:
01.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02.
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
04.
<!--
05.
Mapping file autogenerated by MyEclipse Persistence Tools
06.
-->
07.
<
hibernate-mapping
>
08.
<
class
name
=
"cn.edu.ahau.mgc.hibernate.pojo.Node"
table
=
"TREE_NODE"
schema
=
"SCOTT"
>
09.
<
id
name
=
"id"
type
=
"java.lang.Long"
>
10.
<
column
name
=
"ID"
precision
=
"22"
scale
=
"0"
/>
11.
<
generator
class
=
"native"
/>
12.
</
id
>
13.
<
property
name
=
"name"
type
=
"java.lang.String"
column
=
"NAME"
/>
14.
<
property
name
=
"level"
type
=
"java.lang.Integer"
column
=
"LEVELNO"
/>
15.
<
property
name
=
"leaf"
type
=
"java.lang.Boolean"
column
=
"LEAF"
/>
16.
<
many-to-one
name
=
"parent"
column
=
"PID"
/>
17.
<
set
name
=
"children"
>
18.
<
key
column
=
"PID"
/>
19.
<
one-to-many
class
=
"cn.edu.ahau.mgc.hibernate.pojo.Node"
/>
20.
</
set
>
21.
</
class
>
22.
</
hibernate-mapping
>
HibernateSessionFactory.java:
001.
package
cn.edu.ahau.mgc.hibernate.many2one.factory;
002.
003.
import
org.hibernate.HibernateException;
004.
import
org.hibernate.Session;
005.
import
org.hibernate.cfg.Configuration;
006.
007.
/**
008.
* Configures and provides access to Hibernate sessions, tied to the
009.
* current thread of execution. Follows the Thread Local Session
010.
* pattern, see {@link http://hibernate.org/42.html ;}.
011.
*/
012.
public
class
HibernateSessionFactory {
013.
014.
/**
015.
* Location of hibernate.cfg.xml file.
016.
* Location should be on the classpath as Hibernate uses
017.
* #resourceAsStream style lookup for its configuration file.
018.
* The default classpath location of the hibernate config file is
019.
* in the default package. Use #setConfigFile() to update
020.
* the location of the configuration file for the current session.
021.
*/
022.
private
static
String CONFIG_FILE_LOCATION =
"/hibernate.cfg.xml"
;
023.
private
static
final
ThreadLocal<Session> threadLocal =
new
ThreadLocal<Session>();
024.
private
static
Configuration configuration =
new
Configuration();
025.
private
static
org.hibernate.SessionFactory sessionFactory;
026.
private
static
String configFile = CONFIG_FILE_LOCATION;
027.
028.
static
{
029.
try
{
030.
configuration.configure(configFile);
031.
sessionFactory = configuration.buildSessionFactory();
032.
}
catch
(Exception e) {
033.
System.err
034.
.println(
"%%%% Error Creating SessionFactory %%%%"
);
035.
e.printStackTrace();
036.
}
037.
}
038.
private
HibernateSessionFactory() {
039.
}
040.
041.
/**
042.
* Returns the ThreadLocal Session instance. Lazy initialize
043.
* the <code>SessionFactory</code> if needed.
044.
*
045.
* @return Session
046.
* @throws HibernateException
047.
*/
048.
public
static
Session getSession()
throws
HibernateException {
049.
Session session = (Session) threadLocal.get();
050.
051.
if
(session ==
null
|| !session.isOpen()) {
052.
if
(sessionFactory ==
null
) {
053.
rebuildSessionFactory();
054.
}
055.
session = (sessionFactory !=
null
) ? sessionFactory.openSession()
056.
:
null
;
057.
threadLocal.set(session);
058.
}
059.
060.
return
session;
061.
}
062.
063.
/**
064.
* Rebuild hibernate session factory
065.
*
066.
*/
067.
public
static
void
rebuildSessionFactory() {
068.
try
{
069.
configuration.configure(configFile);
070.
sessionFactory = configuration.buildSessionFactory();
071.
}
catch
(Exception e) {
072.
System.err
073.
.println(
"%%%% Error Creating SessionFactory %%%%"
);
074.
e.printStackTrace();
075.
}
076.
}
077.
078.
/**
079.
* Close the single hibernate session instance.
080.
*
081.
* @throws HibernateException
082.
*/
083.
public
static
void
closeSession()
throws
HibernateException {
084.
Session session = (Session) threadLocal.get();
085.
threadLocal.set(
null
);
086.
087.
if
(session !=
null
) {
088.
session.close();
089.
}
090.
}
091.
092.
/**
093.
* return session factory
094.
*
095.
*/
096.
public
static
org.hibernate.SessionFactory getSessionFactory() {
097.
return
sessionFactory;
098.
}
099.
100.
/**
101.
* return session factory
102.
*
103.
* session factory will be rebuilded in the next call
104.
*/
105.
public
static
void
setConfigFile(String configFile) {
106.
HibernateSessionFactory.configFile = configFile;
107.
sessionFactory =
null
;
108.
}
109.
110.
/**
111.
* return hibernate configuration
112.
*
113.
*/
114.
public
static
Configuration getConfiguration() {
115.
return
configuration;
116.
}
117.
118.
}
ExportToDBCreate.java:
01.
package
cn.edu.ahau.mgc.hibernate.export;
02.
03.
import
org.hibernate.cfg.Configuration;
04.
import
org.hibernate.tool.hbm2ddl.SchemaExport;
05.
06.
public
class
ExportToDBCreate {
07.
08.
public
static
void
main(String[] args) {
09.
Configuration cfg =
new
Configuration().configure();
10.
SchemaExport export =
new
SchemaExport(cfg);
11.
export.create(
true
,
true
);
12.
}
13.
14.
}
NodeManager.java:
01.
package
cn.edu.ahau.mgc.hibernate.export;
02.
03.
import
java.io.File;
04.
import
java.util.Iterator;
05.
06.
import
org.hibernate.Session;
07.
08.
import
cn.edu.ahau.mgc.hibernate.factory.HibernateSessionFactory;
09.
import
cn.edu.ahau.mgc.hibernate.pojo.Node;
10.
11.
public
class
NodeManager {
12.
13.
private
static
NodeManager nodeManager;
14.
15.
private
NodeManager() {
16.
17.
}
18.
19.
public
static
synchronized
NodeManager getInstance() {
20.
if
(nodeManager ==
null
) {
21.
nodeManager =
new
NodeManager();
22.
}
23.
return
nodeManager;
24.
}
25.
26.
public
void
createTree(String filePath) {
27.
Session session =
null
;
28.
try
{
29.
session = HibernateSessionFactory.getSession();
30.
session.beginTransaction();
31.
32.
File root =
new
File(filePath);
33.
saveTree(root,
0
,
null
, session);
34.
35.
36.
session.getTransaction().commit();
37.
}
catch
(Exception e) {
38.
e.printStackTrace();
39.
session.getTransaction().rollback();
40.
}
finally
{
41.
HibernateSessionFactory.closeSession();
42.
}
43.
}
44.
45.
public
void
saveTree(File root,
int
level, Node parent, Session session) {
46.
boolean
leaf = root.isFile() || root.listFiles().length ==
0
;
47.
Node node =
new
Node();
48.
node.setName(root.getName());
49.
node.setLevel(level);
50.
node.setLeaf(leaf);
51.
node.setParent(parent);
52.
session.save(node);
53.
if
(!leaf) {
54.
for
(
int
i =
0
; i < root.listFiles().length; i++) {
55.
File file = root.listFiles()[i];
56.
saveTree(file, level +
1
, node, session);
57.
}
58.
}
59.
}
60.
61.
public
void
printTree(
long
id) {
62.
Session session =
null
;
63.
try
{
64.
session = HibernateSessionFactory.getSession();
65.
Node node = (Node) session.load(Node.
class
, id);
66.
printNode(node);
67.
}
catch
(Exception e) {
68.
e.printStackTrace();
69.
}
finally
{
70.
HibernateSessionFactory.closeSession();
71.
}
72.
73.
}
74.
75.
public
void
printNode(Node node) {
76.
StringBuffer space =
new
StringBuffer();
77.
if
(node.getLevel() !=
0
) {
78.
for
(
int
i =
0
; i < node.getLevel(); i++) {
79.
space.append(
" "
);
80.
}
81.
space.append(
"|_"
);
82.
}
83.
System.out.println(space + node.getName());
84.
if
(node.getChildren() !=
null
&& node.getChildren().size() !=
0
) {
85.
for
(Iterator<Node> iterator = node.getChildren().iterator(); iterator.hasNext();) {
86.
Node nodeChild = iterator.next();
87.
printNode(nodeChild);
88.
}
89.
}
90.
}
91.
}
TestNode.java:
01.
package
cn.edu.ahau.mgc.hibernate.export;
02.
03.
public
class
TestNode {
04.
05.
public
static
void
main(String[] args) {
06.
NodeManager.getInstance().createTree(
"E:/CODE/J2EE/Hibernate/hibernateTree"
);
07.
NodeManager.getInstance().printTree(
1
);
08.
}
09.
}
http://mgc.ahau.edu.cn/article.asp?id=534