欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hibernate(3):hibernate映射关系

程序员文章站 2024-03-02 15:59:52
...

hibernate(3):hibernate映射关系

hibernate(3):hibernate映射关系

 

hibernate(3):hibernate映射关系

 

例:建表脚本

create table customers(
    id bigint not null,
    name varchar(15),
    primary key (id)
)

create table orders(
    id bigint not null,
    order_number varchar(15),
    customer_id bigint not null,
    primary key(id)
)

alter table orders add index idx_customer_id(customer_id),add constraint fk_customer_id 
foreign key(customer_id) references customers(id);

 

Customer.java

package com.hibernate.model;

import java.util.Set;

public class Customer {
	private Long id;
	private String name;
	private Set orders;
	
	public Customer(String name,Set orders){
		this.name = name;
		this.orders = orders;
	}
	
	public Customer(){
	}
	
	public Customer(Set orders){
		this.orders = orders;
	}

	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 Set getOrders() {
		return orders;
	}

	public void setOrders(Set orders) {
		this.orders = orders;
	}
	
}

 

Order.java

package com.hibernate.model;

public class Order {
	private Long id;
	private String orderNumber;
	private Customer customer; // 多对一,通过该变量可以引用到对应的Customer

	public Order(String orderNumber, Customer customer) {
		this.orderNumber = orderNumber;
		this.customer = customer;
	}
	
	public Order(){
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getOrderNumber() {
		return orderNumber;
	}

	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
}

 

Customer.hbm.xml

<?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.hibernate.model.Customer" table="customers">
		<id name="id" column="id" type="long">
			<generator class="increment"></generator>
		</id>
		<property name="name" type="string">
			<column name="name" length="15"></column>
		</property>
		<!--cascade表示保存或更新customers对象时,会同步保存或更新与之关联的orders对象 -->
		<!-- 因为Customer类中的orders属性是set类型的,所以下面这样用 -->
		<!-- inverse为true表示由对方来维护关联关系 -->
		<set name="orders" cascade="save-update" inverse="true">
			<key column="customer_id"></key><!-- key元素设定与所关联的持久化类对应的表单外键-->
			<one-to-many class="com.hibernate.model.Order"/>
		</set>	
	</class>
</hibernate-mapping>

 

Order.hbm.xml

<?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.hibernate.model.Order" table="orders">
		<id name="id" column="id" type="long">
			<generator class="increment"></generator>
		</id>
		<property name="orderNumber" type="string">
			<column name="order_number" length="15"></column>
		</property>
		<many-to-one name="customer" column="customer_id" class="com.hibernate.model.Customer">
		</many-to-one>
	</class>
</hibernate-mapping>

在hibernate.cfg.xml文件中加上上面这两个配置文件的映射

<mapping resource="Customer.hbm.xml"></mapping>
<mapping resource="Order.hbm.xml"></mapping>

  

测试

package com.hibernate.util;

import java.util.HashSet;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.hibernate.model.Customer;
import com.hibernate.model.Order;

public class Test {
	public static void main(String[] args) {
		saveCustomerAndOrderWithCascade();
	}

	public static void saveCustomerAndOrderWithCascade() {
		Session session = HibernateUtil.currentSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Customer customer = new Customer("zhangsan", new HashSet());

			Order order1 = new Order();
			order1.setOrderNumber("zhangsan_order1");
			order1.setCustomer(customer);

			Order order2 = new Order();
			order2.setOrderNumber("zhangsan_order2");
			order2.setCustomer(customer);

			Order order3 = new Order();
			order3.setOrderNumber("zhangsan_order3");
			order3.setCustomer(customer);

			customer.getOrders().add(order1);
			customer.getOrders().add(order2);
			customer.getOrders().add(order3);
			//只需要customer,与其关联的order都会被保存
			session.save(customer);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			HibernateUtil.closeSession(session);
		}
	}
}

 

hibernate(3):hibernate映射关系

 

 

 

hibernate(3):hibernate映射关系

 

建表脚本

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

alter table categories add index idx_category_id 
(category_id),add constraint fk_category_id foreign key
 (category_id) references categories(id)

 

Category.java

package com.hibernate.model;

import java.util.Set;

public class Category {
	private Long id;
	private String name;
	private Category parentCategory;
	private Set childCategories;
	
	public Category(String name,Category parentCategory,Set childCategories){
		this.name = name;
		this.parentCategory = parentCategory;
		this.childCategories = childCategories;
	}
	
	public Category(){}
	
	public Category(Set childCategories){
		this.childCategories = 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 getChildCategories() {
		return childCategories;
	}

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

 

Category.hbm.xml

<?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.hibernate.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="15"></column>
		</property>
		<set name="childCategories" cascade="all" inverse="true">
			<key column="category_id"></key>
			<one-to-many class="com.hibernate.model.Category"/>
		</set>	
		<many-to-one name="parentCategory" column="category_id" class="com.hibernate.model.Category">
		</many-to-one>		
	</class>
</hibernate-mapping>

 

级联保存与级联删除

public static void saveCategoryWithCascade(){
		Session session = HibernateUtil.currentSession();
		Transaction tx = null;
		try{
			tx = session.beginTransaction();
			//食品分类
			Category foodCategory = new Category("food",null,new HashSet());
			//蔬菜分类
			Category vegetableCategory = new Category("vegetable",null,new HashSet());
			//水果分类
			Category fruitCategory = new Category("fruit",null,new HashSet());
			//西红柿分类
			Category tomotaCategory = new Category("tomota",null,new HashSet());
			//苹果分类
			Category appleCategory = new Category("apple",null,new HashSet());
			//桔子分类
			Category orangeCategory = new Category("orange",null,new HashSet());
			
			//设置上下级关系
			foodCategory.getChildCategories().add(vegetableCategory);  //食品与蔬菜
			vegetableCategory.setParentCategory(foodCategory);
			
			foodCategory.getChildCategories().add(fruitCategory);  //食品与水果
			fruitCategory.setParentCategory(foodCategory);
			
			vegetableCategory.getChildCategories().add(tomotaCategory);//蔬菜与西红柿
			tomotaCategory.setParentCategory(vegetableCategory);
			
			fruitCategory.getChildCategories().add(appleCategory);  //水果与苹果
			appleCategory.setParentCategory(fruitCategory);
			
			orangeCategory.getChildCategories().add(orangeCategory);  //水果与桔子
			orangeCategory.setParentCategory(fruitCategory);
			session.save(foodCategory);  //级联保存所有的关联对象
			tx.commit();
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			HibernateUtil.closeSession(session);
		}
	}
	
	//级联删除
	public static void deleteCategoryWithCascade(){
		Session session = HibernateUtil.currentSession();
		Transaction tx = null;
		try{
			tx = session.beginTransaction();
			Category category = (Category) session.get(Category.class, new Long(1));
			session.delete(category);
			tx.commit();
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			HibernateUtil.closeSession(session);
		}
	}