文件
/
新建
/
其他
/
出现以下画面
点击
web project
下一步后在项目名写
test2,
点击完成即可
.
完成后在
test2
项目点击右键
,
如下图
然后进入下面页面
,(
记得选择
hibernate2.1 jar library installation
选第二项
,
确保设置与下图一致
)
下一步
/
下一步
将
db profile
选择为
MSSQL1
点击下一步
如
下图
( class:
如下图填写
)
点击完成
.
现在我们建立对象
Customer.java Order.java
文件
/
新建
/
类
在弹出的窗口中
源文件夹为
test2/src
包为
com
名字为
Customer,
点击完成即可
.
建立
Order.java
操作一样
.
添加代码
:
Customer.java
package com;
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class Customer implements Serializable {
/** identifier field */
private Long id;
/** nullable persistent field */
private String name;
/** full constructor */
public Customer(String name) {
this.name = name;
}
/** default constructor */
public Customer() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}
}
Order.java
package com;
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class Order implements Serializable {
/** identifier field */
private Long id;
/** nullable persistent field */
private String orderNumber;
/** persistent field */
private com.Customer customer;
/** full constructor */
public Order(String orderNumber,Customer customer) {
this.orderNumber = orderNumber;
this.customer = customer;
}
/** default constructor */
public Order() {
}
/** minimal constructor */
public Order(com.Customer customer) {
this.customer = customer;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getOrderNumber() {
return this.orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public com.Customer getCustomer() {
return this.customer;
}
public void setCustomer(com.Customer customer) {
this.customer = customer;
}
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}
}
建立两个映射文件
Customer.hbm..xml Order.hbm.xml
文件
/
新建
选择如下图
选择第一个
下一步
在下一步中
父文件夹为
test2/src/com
文件名为
Customer.hbm.xml
下一步
选择如下图
点击下一步
完成
建立
Order.hbm.xml
方法一致
.
将两个
xml
文件修改一下
修改部分粗体显示了
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
编辑
Customer.hbm..xml Order.hbm.xml
Customer.hbm..xml
<?
xml
version
=
"1.0"
?>
<!
DOCTYPE
hibernate-mapping
PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
<
hibernate-mapping
>
<
class
name
=
"com.Customer"
table
=
"CUSTOMER"
>
<
id
name
=
"id"
type
=
"long"
column
=
"ID"
>
<
generator
class
=
"increment"
/>
</
id
>
<
property
name
=
"name"
type
=
"string"
>
<
column
name
=
"NAME"
length
=
"15"
/>
</
property
>
</
class
>
</
hibernate-mapping
>
Order.hbm.xml
<?
xml
version
=
"1.0"
?>
<!
DOCTYPE
hibernate-mapping
PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
<
hibernate-mapping
>
<
class
name
=
"com.Order"
table
=
"[ORDER]"
>
<
id
name
=
"id"
type
=
"long"
column
=
"ID"
>
<
generator
class
=
"increment"
/>
</
id
>
<
property
name
=
"orderNumber"
type
=
"string"
>
<
column
name
=
"ORDER_NUMBER"
length
=
"15"
/>
</
property
>
<
many-to-one
name
=
"customer"
column
=
"CUSTOMER_ID"
class
=
"com.Customer"
not-null
=
"true"
cascade
=
"save-update"
/>
<!-- mapping with cascade -->
<!--
<many-to-one
name="customer"
column="CUSTOMER_ID"
class="mypack.Customer"
cascade="save-update"
not-null="true" />
-->
</
class
>
</
hibernate-mapping
>