Hibernate学习实例:关联表的树状结构设计
来源:互联网 发布:大数据与股票预测 编辑:程序博客网 时间:2024/05/23 15:06
说明:
一、使用的Hibernate版本是hibernate-release-4.3.11.Final.
二、实例:一个公司有许多子公司
三、映射的时候,在同一个类中使用ManyToOne 和 OneToMany
四、本次只使用Annotation版本,其他版本可参考之前的博客
实体类:
Org.java:
package com.buaa.hibernate.homework;import java.util.HashSet;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.OneToMany;@Entitypublic class Org {private int id;private String name;private Set<Org> children = new HashSet<Org>();private Org parent;public Org(String name){this.name = name;}@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@OneToMany(cascade={CascadeType.ALL},mappedBy="parent",fetch=FetchType.EAGER)public Set<Org> getChildren() {return children;}public void setChildren(Set<Org> children) {this.children = children;}@ManyToOne@JoinColumn(name="pid")public Org getParent() {return parent;}public void setParent(Org parent) {this.parent = parent;}}hibernate配置文件 :hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="connection.driver_class">com.mysql.jdbc.Driver</property><!-- Assume test is the database name --><property name="connection.url">jdbc:mysql://localhost/test</property><property name="connection.username">root</property><property name="connection.password">123456</property><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="show_sql">true</property><property name="format_sql">true</property><property name="hbm2ddl.auto">create</property><mapping class="com.buaa.hibernate.homework.Org" /></session-factory></hibernate-configuration>Junit测试类:
OrgTest.java:
package com.buaa.hibernate.homework;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.junit.After;import org.junit.Before;import org.junit.Test;public class OrgTest {private SessionFactory factory;private Session session;@Beforepublic void before(){Configuration configuration = new Configuration().configure();ServiceRegistry service = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); factory = configuration.buildSessionFactory(service); session = factory.openSession();}//往数据库中存储数据@Testpublic void test(){Org o = new Org("company");Org o1 = new Org("company_1");Org o2 = new Org("company_2");Org o11 = new Org("company_1_1");Org o12 = new Org("company_1_2");o.getChildren().add(o1);o.getChildren().add(o2);o1.getChildren().add(o11);o1.getChildren().add(o12);o1.setParent(o);o2.setParent(o);o11.setParent(o1);o12.setParent(o1);session.beginTransaction();session.save(o);session.getTransaction().commit();}//从数据库中读取数据@Testpublic void loadTest(){test();session.beginTransaction();Org o = (Org)session.load(Org.class, 1);print(o,0);session.getTransaction().commit();}//用递归的方法,获取所有的子类,并且缩进显示private void print(Org o,int level) {String buffer = "";for(int i=0;i<level;i++){buffer += "----";}System.out.println(buffer + o.getName());for(Org child : o.getChildren()){print(child,level+1);}}@Afterpublic void after(){session.close();factory.close();}}数据库的建表语句:
Hibernate: alter table Org drop foreign key FK_n4sv1hojgjugxnfjsourei39bHibernate: drop table if exists OrgHibernate: create table Org ( id integer not null auto_increment, name varchar(255), pid integer, primary key (id) )Hibernate: alter table Org add constraint FK_n4sv1hojgjugxnfjsourei39b foreign key (pid) references Org (id)
最后是从数据库中读取到的数据:
company----company_2----company_1--------company_1_2--------company_1_1
0 0
- Hibernate学习实例:关联表的树状结构设计
- 《Hibernate学习笔记十一》:树状结构设计
- hibernate注解形式的树状结构设计
- Hibernate之树状结构设计
- Hibernate 另一个作业:Org树状结构设计
- 【hibernate框架】练习-树状结构设计(非常重要)
- Hibernate的一对一关联实例
- Hibernate的一对多关联实例
- Hibernate学习之---Hibernate的关联映射
- Hibernate学习文档_表的自我关联
- 树状自关联 hibernate配置,查询
- hibernate 树状自关联 *.hbm.xml 配置
- Hibernate的学习总结(数据关联)
- SSH学习之Hibernate的关联关系
- hibernate的关联表查询
- Hibernate 一对一主键关联实例
- Hibernate中一对一关联实例
- Hibernate中一对一关联实例
- Linux下51单片机环境配置
- Android Studio 快捷键
- Unity Manual 笔记01 第一章 使用Unity工作——资源工作流
- Jquery学习之旅之删除元素
- 【Leetcode】6.ZigZag Conversion 解题
- Hibernate学习实例:关联表的树状结构设计
- ubuntu14.04+svn
- Flash builder4.7更新AIR SDK
- c/c++实现浏览器的下载功能
- windows界面编程之自绘菜单
- windows内核编程之如何防止一个相同的程序重复运行
- 114. Flatten Binary Tree to Linked List
- windows界面编程之自绘窗体(模仿腾讯qq)
- 创建动态数据窗口的两种方法