映射Category一对多双向自身关联关系

create table categories (
	id bigint not null,
	name varchar(15),
	category_id bigint,
	primary key (id)
);


<?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 = "model.Category" table = "Categories">
	
		<id name = "id" column = "id" type = "long">
			<generator class="increment"></generator>
		</id>
		
		<property name = "name"  type = "string">
			<column name="name" length="50"></column>
		</property>
		<set name="childCategories" cascade="all" inverse="true">
			<key column="category_id"></key>
			<one-to-many class="model.Category"></one-to-many>
		</set>
		<many-to-one name="parentCategory" column="category_id" class = "model.Category"></many-to-one>
	</class>
</hibernate-mapping>

package model;

import java.util.Set;

public class Category {
	private Long id;
	
	private String name;
	
	private Category parentCategory;
	
	private Set<Category> childCategories;

	public Long getId() {
		return id;
	}

	public void setId(Long 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<Category> getChildCategories() {
		return childCategories;
	}

	public void setChildCategories(Set<Category> childCategories) {
		this.childCategories = childCategories;
	}

	public Category(String name, Category parentCategory,
			Set<Category> childCategories) {
		super();
		
		this.name = name;
		this.parentCategory = parentCategory;
		this.childCategories = childCategories;
	}

	public Category() {
		super();
	}
}

package model;



import java.util.HashSet;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateTest2 {
	private static SessionFactory sessionFactory = null;
	
	static {
		try {
			sessionFactory = new Configuration().configure().buildSessionFactory();
			
		}catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	public static void main(String[] args) {
		
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
		
			tx = session.beginTransaction();
			/*
			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.setParentCategory(category1);
			category3.setParentCategory(category1);
			
			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);
			session.save(category1);
			*/
			Category category = (Category)session.get(Category.class, new Long(1));
			System.out.println(category.getChildCategories().size());
			tx.commit();
		}catch(Exception ex) {
			ex.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		}finally {
			session.close();
		}
		
		
	}
}
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select category0_.id as id0_0_, category0_.name as name0_0_, category0_.category_id as category3_0_0_ from Categories category0_ where category0_.id=?
Hibernate: select childcateg0_.category_id as category3_1_, childcateg0_.id as id1_, childcateg0_.id as id0_0_, childcateg0_.name as name0_0_, childcateg0_.category_id as category3_0_0_ from Categories childcateg0_ where childcateg0_.category_id=?
2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis是一款优秀的ORM框架,支持一对一、一对多、多对一、多对多等关联映射。其中,一对多关联映射是指一个实体类中包含多个另一个实体类的对象。下面是一对多关联映射的实现方法: 1.在主实体类中定义一个包含从实体类对象的List集合属性。 2.在主实体类对应的Mapper.xml文件中,使用<collection>标签来映射从实体类对象的List集合属性。 3.在从实体类对应的Mapper.xml文件中,使用<association>标签来映射主实体类对象。 具体实现可以参考以下代码: 主实体类User: ``` public class User { private Integer id; private String username; private List<Order> orders; //getter和setter方法 } ``` 从实体类Order: ``` public class Order { private Integer id; private String orderNo; private User user; //getter和setter方法 } ``` 主实体类User对应的Mapper.xml文件: ``` <mapper namespace="com.biem.mapper.UsersMapper"> <resultMap id="userMap" type="User"> <id property="id" column="id"/> <result property="username" column="username"/> <collection property="orders" ofType="Order"> <id property="id" column="order_id"/> <result property="orderNo" column="order_no"/> </collection> </resultMap> <select id="getUserById" resultMap="userMap"> select * from user where id=#{id} </select> </mapper> ``` 从实体类Order对应的Mapper.xml文件: ``` <mapper namespace="com.biem.mapper.OrdersMapper"> <resultMap id="orderMap" type="Order"> <id property="id" column="id"/> <result property="orderNo" column="order_no"/> <association property="user" javaType="User"> <id property="id" column="user_id"/> <result property="username" column="username"/> </association> </resultMap> <select id="getOrderById" resultMap="orderMap"> select * from order where id=#{id} </select> </mapper> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值