Hibernate xml配置方法之OneToMany(双向关联关系)

java代码:

package com.lwp.hibernate.Model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

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

	public Customer(){}
	public Customer(String name,Set<Order> orders){
		this.name=name;
		this.orders = orders;
	}

	private Long id;
	
	private String name;
	
	private Set<Order> orders = new HashSet<Order>();

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

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){
		this.orderNumber =orderNumber;
	}

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

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>
		<set name="orders" inverse="true" >  
                <key column="customer_id"></key>  
                <one-to-many class="com.lwp.hibernate.Model.Order"/>  
        </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.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>
		<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 ORDERS
Hibernate: select max(ID) from Customer
Hibernate: insert into Customer (NAME, ID) values (?, ?)
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: update Customer set NAME=? where ID=?

你可能感兴趣的:(xml,Hibernate,String,table,Class,generator)