Hibernate xml配置方法之ManyToOne(单向关联关系)

从Order到Customer是多对一的关系,这意味着每个Order都对应一个Customer.在关系数据库中,只存在外键参照关系,而且总是由“many”方参照“one”方,因为这样才能消除数据冗余.

Java代码如下:

package com.lwp.hibernate.Model;

import java.io.Serializable;

public class Customer implements Serializable{
	
	private static final long serialVersionUID = 1L;

	public Customer(){}
	public Customer(String name){
		this.name=name;
	}
	private Long id;
	
	private String name;

	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;
	}
}

package com.lwp.hibernate.Model;

import java.io.Serializable;

public class Order implements Serializable{

	private static final long serialVersionUID = 1L;
	public Order(){}

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

	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;
	}
}

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import com.lwp.hibernate.Model.Customer;
import com.lwp.hibernate.Model.Order;
import com.lwp.hibernate.Model.Student;


public class CustomerTest {

	public static void main(String[] args) {
		new SchemaExport(new AnnotationConfiguration().configure("/hibernate.cfg.xml")).create(true, false);
		Customer customer = new Customer("Tom");
		Order order1 = new Order("Tom_Order001",customer);
		Order order2 = new Order("Tom_Order002",customer);
		save(customer);
		save(order1);
		save(order2);
	}

	public static void save(Object o){
		Configuration cfg = new Configuration();
		SessionFactory sf = cfg.configure().buildSessionFactory();
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(o);
		session.getTransaction().commit();
		session.close();
		sf.close();
	}
}


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.lwp.hibernate.Model.Customer">
		<id name="id" type="long" column="ID" >
			<generator class="increment"/>
		</id>
		<property name="name" type="string">
			<column name="NAME" length="15"></column>
		</property>
    </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.lwp.hibernate.Model.Order" table="ORDERS">
		<id name="id" type="long" column="ID" >
			<generator class="increment"/>
		</id>
		<property name="orderNumber" type="string">
			<column name="ORDER_NUMBER" length="15"></column>
		</property>
		<!-- casecade属性的默认值为”none",如果设为save-update,Hibernate持久化Order对象时
		自动持久化所关联的Customer对象 -->
		<many-to-one name="customer" column="CUSTOMER_ID" cascade="save-update"
		 class="com.lwp.hibernate.Model.Customer" not-null="true"/>
    </class>
</hibernate-mapping>


生成的SQL如下:

alter table ORDERS drop foreign key FK8B7256E5C1E4F651
drop table if exists Customer
drop table if exists ORDERS
create table Customer (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 FK8B7256E5C1E4F651 (CUSTOMER_ID), add constraint FK8B7256E5C1E4F651 foreign key (CUSTOMER_ID) references Customer (ID)
Hibernate: select max(ID) from Customer
Hibernate: insert into Customer (NAME, ID) values (?, ?)
Hibernate: select max(ID) from ORDERS
Hibernate: insert into ORDERS (ORDER_NUMBER, CUSTOMER_ID, ID) values (?, ?, ?)
Hibernate: select max(ID) from ORDERS
Hibernate: insert into ORDERS (ORDER_NUMBER, CUSTOMER_ID, ID) values (?, ?, ?)



数据关系图如下:

Hibernate xml配置方法之ManyToOne(单向关联关系)_第1张图片

表结构:

Hibernate xml配置方法之ManyToOne(单向关联关系)_第2张图片  Hibernate xml配置方法之ManyToOne(单向关联关系)_第3张图片


你可能感兴趣的:(Hibernate xml配置方法之ManyToOne(单向关联关系))