【一对多自身关联双向映射】

关于一对多自身关联双向映射我们直接看例子:
    \SPAN style="COLOR: #cc0000; FONT-SIZE: 14px">一个一对多双向自身关联映射案例</SPAN>
    一个一对多双向自身关联映射案例[java] view plaincopyprint?<?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    <class name="com.bjpowernode.hibernate.Category" table="t_category">
    <id name="id">
    <generator class="native"/>
    </id>
    <property name="name"/>
    <many-to-one name="parentCategory" column="category_id" cascade="save-update"/>
    <set name="childCategory" cascade="save-update">
    <key column="category_id"/>
    <one-to-many class="com.bjpowernode.hibernate.Category"/>
    </set>
    </class>
    </hibernate-mapping>
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    <class name="com.bjpowernode.hibernate.Category" table="t_category">
    <id name="id">
    <generator class="native"/>
    </id>
    <property name="name"/>
    <many-to-one name="parentCategory" column="category_id" cascade="save-update"/>
    <set name="childCategory" cascade="save-update">
    <key column="category_id"/>
    <one-to-many class="com.bjpowernode.hibernate.Category"/>
    </set>
    </class>
    </hibernate-mapping>
    [java]
    package com.bjpowernode.hibernate;
    import java.io.Serializable;
    import java.util.HashSet;
    import java.util.Set;
    public class Category implements Serializable{
    private Integer id;
    private String name;
    private Category parentCategory;
    private Set childCategory = new HashSet();
    public Integer getId() {
    return id;
    }
    private void setId(Integer id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public Category getParentCategory() {
    return parentCategory;
    }
    public void setParentCategory(Category parentCategory) {
    this.parentCategory = parentCategory;
    }
    public Set getChildCategory() {
    return childCategory;
    }
    public void setChildCategory(Set childCategory) {
    this.childCategory = childCategory;
    }
    }
    package com.bjpowernode.hibernate;
    import java.io.Serializable;
    import java.util.HashSet;
    import java.util.Set;
    public class Category implements Serializable{
    private Integer id;
    private String name;
    private Category parentCategory;
    private Set childCategory = new HashSet();
    public Integer getId() {
    return id;
    }
    private void setId(Integer id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public Category getParentCategory() {
    return parentCategory;
    }
    public void setParentCategory(Category parentCategory) {
    this.parentCategory = parentCategory;
    }
    public Set getChildCategory() {
    return childCategory;
    }
    public void setChildCategory(Set childCategory) {
    this.childCategory = childCategory;
    }
    }
    工具类
    [java]
    package com.bjpowernode.hibernate;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    public class HibernateUtils {
    private static SessionFactory factory;
    static {
    try {
    //读取hibernate.cfg.xml文件
    Configuration cfg = new Configuration()。configure();
    //建立SessionFactory
    factory = cfg.buildSessionFactory();
    }catch(Exception e) {
    e.printStackTrace();
    }
    }
    public static Session getSession() {
    return factory.openSession();
    }
    public static void closeSession(Session session) {
    if (session != null) {
    if (session.isOpen()) {
    session.close();
    }
    }
    }
    public static SessionFactory getSessionFactory() {
    return factory;
    }
    }

posted on 2013-09-18 15:39  挖掘者者者  阅读(218)  评论(0编辑  收藏  举报