
//目录类:
//Category文件:
package yang.www.hibernate;
import java.util.Set;
public class Category
{
//主键标识符,也是OID
private Long id;
private String name;
//父目录
private Category parentCategory;
//子目录
private Set<Category> childCategories;
public Category(){}
public Category(String name, Category parentCategory, Set<Category> childCategories)
{
super();
this.name = name;
this.parentCategory = parentCategory;
this.childCategories = childCategories;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public Category getParentCategory() {
return parentCategory;
}
public Set<Category> getChildCategories() {
return childCategories;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setParentCategory(Category parentCategory) {
this.parentCategory = parentCategory;
}
public void setChildCategories(Set<Category> childCategories) {
this.childCategories = childCategories;
}
}
//测试Java文件:
package yang.www.hibernate;
import java.util.HashSet;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test2
{
private static SessionFactory sessionFactory;
static
{
try
{
//加载hibernate.cfg.xml文件并且创建一个session工厂.
//因为工厂一般情况下只需一个,所以只在初始化时初始化一次,以后就可以直接使用它来产生session
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args)
{
//产生一次与数据库的连接:
Session session = sessionFactory.openSession();Transaction tx = null;
try
{
//开始事务:
Category category1 = new Category("Level1", null, new HashSet<Category>());
Category category2 = new Category("Level2", null, new HashSet<Category>());
Category category3 = new Category("Level2", null, new HashSet<Category>());
Category category4 = new Category("Level3", null, new HashSet<Category>());
Category category5 = new Category("Level3", null, new HashSet<Category>());
Category category6 = new Category("Level3", null, new HashSet<Category>());
Category category7 = new Category("Level3", null, new HashSet<Category>());
//设置category2和3的父目录为category1,以下的set方法类似
category2.setParentCategory(category1);
category3.setParentCategory(category1);
//不要忘记还要为父目录添加子目录,以下的get方法类似
category1.getChildCategories().add(category2);
category1.getChildCategories().add(category3);
category4.setParentCategory(category2);
category5.setParentCategory(category2);
category2.getChildCategories().add(category4);
category2.getChildCategories().add(category5);
category6.setParentCategory(category3);
category7.setParentCategory(category3);
category3.getChildCategories().add(category6);
category3.getChildCategories().add(category7);
//因为在Category.hbm.xml中已经设置好cascade="all"了,
//所以只需要保存根目录就可以联级保存了。同样的道理,删除全部时,只需要删除根目录就可以了。
tx.commit();
}
catch(Exception ex)
{
if (null != tx)
{
tx.rollback();
}
ex.printStackTrace();
}
finally
{
session.close();
}
}
}
//Category.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="yang.www.hibernate.Category" table="categories">
<id name="id" column="id" type="long">
<generator class="increment"></generator>
</id>
<property name="name" type="string">
<column name="name" length="13"></column>
</property>
//多对一,即多个子目录,对应一个父目录
<many-to-one name="parentCategory" column="category_id" class="yang.www.hibernate.Category"></many-to-one>
//一对多,即一个父目录可以有多个子目录
//key为对应的父目录的ID,即表中的外键
<set name="childCategories" cascade="all" inverse="true">
<key column="category_id"></key>
<one-to-many class="yang.www.hibernate.Category"/>
</set>
</class>
</hibernate-mapping>
//hibernate.cfg.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/myhibernate</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">yang</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="Category.hbm.xml" />
</session-factory>
</hibernate-configuration>
//SQL文件:
create database myhibernate;
create table categories
(
id bigint not null,
name varchar(15),
category_id bigint,
primary key(id)
);
alter table categories add constraint fk_category_id foreign key(category_id) references categories(id);
本文介绍了一个使用Hibernate框架实现的目录类关联映射案例,包括多对一和一对多的关系处理,通过Category类展示了如何建立目录间的层级关系,并演示了如何在Java中进行目录结构的保存。

被折叠的 条评论
为什么被折叠?



