hibernate--树状映射(至关重要)
来源:互联网 发布:qq群秒加僵尸软件 编辑:程序博客网 时间:2024/06/07 02:28
一张表即可完成映射:
Org.java:
package com.bean;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;@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(mappedBy="parent",cascade=CascadeType.ALL,fetch=FetchType.EAGER)public Set<Org> getChildren() {return children;}public void setChildren(Set<Org> children) {this.children = children;}@ManyToOne(cascade=CascadeType.ALL)@JoinColumn(name="parent_id")public Org getParent() {return parent;}public void setParent(Org parent) {this.parent = parent;}}hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/user</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="current_session_context_class">thread</property> <mapping class="com.bean.Org"/> </session-factory></hibernate-configuration>测试类:
package com.test;import java.awt.print.Printable;import java.util.Set;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.bean.Org;public class TestOrg { public static void main(String []args) { // testSave(); testLoad(); } public static void testSave() {Configuration cfg = new Configuration();cfg.configure();SessionFactory sessionFactory = cfg.buildSessionFactory();Session session = sessionFactory.getCurrentSession();Org o = new Org();o.setName("总公司"); Org o1 = new Org(); o1.setName("分公司1"); Org o2 = new Org(); o2.setName("分公司2"); Org o11 = new Org(); o11.setName("分公司1下的部门1"); Org o12 = new Org(); o12.setName("分公司1下的部门2"); o12.setParent(o1); o11.setParent(o1); o1.setParent(o); o2.setParent(o); o1.getChildren().add(o12); o1.getChildren().add(o11); o.getChildren().add(o2); o.getChildren().add(o1);session.beginTransaction();session.save(o);session.getTransaction();} public static void testLoad() {Configuration cfg = new Configuration();cfg.configure();SessionFactory sessionFactory = cfg.buildSessionFactory();Session session = sessionFactory.getCurrentSession();session.beginTransaction(); Org o = (Org)session.load(Org.class, 1); print(o,0);session.getTransaction();}private static void print(Org o,int level) {//递归打印String preString = "";for (int i = 0; i < level; i++) {preString+="----";}System.out.println(preString+o.getName());for (Org org : o.getChildren()) {print(org,level+1);}} }
save后,数据库的内容:
load:
数据库表的设计原理:
阅读全文
0 0
- hibernate--树状映射(至关重要)
- 056:hibernate树状结设计(至关重要)
- hibernate之树状映射
- hibernate树状结构映射
- Hibernate 树状映射
- hibernate映射应用 - 树状结构
- Hibernate 树状映射简单模拟
- Hibernate——树状映射
- hibernate之树状映射结构
- Hibernate——树状映射
- Hibernate(annotation映射)
- Hibernate映射(一)
- Hibernate映射(二)
- 树状结构的设计(把一棵树存入数据库的hibernate映射,及数据库表的设计)
- Hibernate映射详解(六)--继承映射
- hibernate映射只读属性(多次映射)
- Hibernate旅程(六)Hibernate映射--继承映射
- hibernate映射(二) 之Hibernate单项一对多映射
- 莫队算法入门 Codeforces617E
- 安卓线程Thread初识
- Difference between / and /* in servlet mapping url pattern
- 嵌入式系统
- tomcat集群配置详解一之概念篇
- hibernate--树状映射(至关重要)
- MTK lcm 驱动加载流程 android M
- 洛谷 3365 改造二叉树(中序遍历+LIS)
- 前方车辆检测的常用方法
- 小程序怎么添加模板消息-微信小程序开发-视频教程12
- ubuntu安装ssh服务
- 算法好博客网址
- [Java多线程 十]---JUC包下线程池的理解
- Codeforces Round #431 (Div. 2) ABCD