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

本文介绍了一个使用Hibernate框架实现的数据库表与Java对象之间的关联映射案例,包括一对多和多对一的关系处理。通过具体的代码示例展示了如何定义实体类、配置映射文件,并进行数据的存取操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值