JSP虚拟主机,jsp空间,java空间,java虚拟空间JSP虚拟主机,jsp空间,java空间,java虚拟空间

第一个成功的Hibernate实例



作者:    文章来源:
发布日期:2007年03月06日
第一个成功的Hibernate实例
一、创建数据库(使用B)
A. 我的原数据库设计
--create database for restruant management system

USE master

DROP DATABASE RMSDB
CREATE DATABASE RMSDB
GO

USE RMSDB

--create base tables
DROP TABLE Houses
CREATE TABLE Houses
(
HID int IDENTITY(1,1) PRIMARY KEY,
--房间编号
alias varchar(20),
--房间的别称
description varchar(100),
--房间的描述
capity int NOT NULL
--房间的旅客容量
)
DROP TABLE Menus
CREATE TABLE Menus
(
MID int IDENTITY(1,1) PRIMARY KEY,
--菜单编号
mName varchar(20) NOT NULL,
--菜单名称
price float NOT NULL
--菜单价格
)
DROP TABLE Tables
CREATE TABLE Tables
(
TID int IDENTITY(1,1) PRIMARY KEY,
tAlias varchar(20)
)
DROP TABLE Customers
CREATE TABLE Customers
(
CID char(20) PRIMARY KEY,
cName varchar(20),
cAddress varchar(40),
cPhone varchar(20)
)

DROP TABLE Staffs
CREATE TABLE Staffs
(
SID VARCHAR(6) PRIMARY KEY,
username VARCHAR(20) NOT NULL,
password VARCHAR(20) NOT NULL,
sName VARCHAR(20) NOT NULL,
address VARCHAR(40),
phone VARCHAR(20),
sPosition VARCHAR(20)
--position including manager or common staffs
)

DROP TABLE Notice
CREATE TABLE Notice
(
NID int IDENTITY(1,1) PRIMARY KEY,
title NCHAR(20) NOT NULL,
content NCHAR(200)
)

GO

B. 修改后的数据库设计
注意:去掉了所有的IDENTITY(1,1),也就是在SQL Server上不让键自动增长,而这一功能将在以后的Mapping中由Hibernate 实现,在 Hibernate 中有很多种主键的生成算法
--create database for restruant management system

USE master

DROP DATABASE RMSDB
CREATE DATABASE RMSDB
GO

USE RMSDB

--create base tables
DROP TABLE Houses
CREATE TABLE Houses
(
HID INT PRIMARY KEY,
--房间编号
alias VARCHAR(20),
--房间的别称
description VARCHAR(100),
--房间的描述
capity INT NOT NULL
--房间的旅客容量
)
DROP TABLE Menus
CREATE TABLE Menus
(
MID INT PRIMARY KEY,
--菜单编号
mName VARCHAR(20) NOT NULL,
--菜单名称
price FLOAT NOT NULL
--菜单价格
)
DROP TABLE Tables
CREATE TABLE Tables
(
TID INT PRIMARY KEY,
tAlias VARCHAR(20)
)
DROP TABLE Customers
CREATE TABLE Customers
(
CID VARCHAR(20) PRIMARY KEY,
cName VARCHAR(20),
cAddress VARCHAR(40),
cPhone VARCHAR(20)
)

DROP TABLE Staffs
CREATE TABLE Staffs
(
SID VARCHAR(6) PRIMARY KEY,
username VARCHAR(20) NOT NULL,
password VARCHAR(20) NOT NULL,
sName VARCHAR(20) NOT NULL,
address VARCHAR(40),
phone VARCHAR(20),
sPosition VARCHAR(20)
--position including manager or common staffs
)

DROP TABLE Notice
CREATE TABLE Notice
(
NID INT PRIMARY KEY,
title VARCHAR(20) NOT NULL,
content VARCHAR(200)
)

GO
二、在Eclipse中生成POLO和映射文件
1) 在Eclipse中配置环境,包括数据库驱动程序(此处略)
2) 生成POLO类和映射文件
此处以Houses为例,生成的原代码和映射文件如下:
a) hibernate.cfg.xml

<?xml version=´1.0´ encoding=´UTF-8´?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<hibernate-configuration>

<session-factory>
<!-- properties -->
<property name="connection.username">sa</property>
<property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;databaseName=RMSDB</property>
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>
<property name="connection.password">jckjdkmcj</property>
<property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>

<!-- mapping files -->


<mapping resource="zy/pro/hrms/dao/Staffs.hbm.xml"/>
<mapping resource="zy/pro/hrms/dao/Houses.hbm.xml"/>

<mapping resource="zy/pro/hrms/dao/Menus.hbm.xml"/>

<mapping resource="zy/pro/hrms/dao/Tables.hbm.xml"/>



<mapping resource="zy/pro/hrms/dao/Customers.hbm.xml"/>

<mapping resource="zy/pro/hrms/dao/Notice.hbm.xml"/>


</session-factory>

</hibernate-configuration>
b) Houses.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" >

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Fri May 13 14:09:45 CST 2005 -->
<hibernate-mapping package="zy.pro.hrms.dao">

<class name="Houses" table="Houses">
<id name="hid" column="HID" type="java.lang.Integer">
<generator class="increment"/>
</id>

<property name="alias" column="alias" type="java.lang.String" />
<property name="description" column="description" type="java.lang.String" />
<property name="capity" column="capity" type="java.lang.Integer" not-null="true" />
</class>

</hibernate-mapping>
c) Houses类
/*
* Created Fri May 13 14:09:45 CST 2005 by MyEclipse Hibernate Tool.
*/
package zy.pro.hrms.dao;

import java.io.Serializable;

/**
* A class that represents a row in the ´Houses´ table.
* This class may be customized as it is never re-generated
* after being created.
*/
public class Houses
extends AbstractHouses
implements Serializable
{
/**
* Simple constructor of Houses instances.
*/
public Houses()
{
}

/**
* Constructor of Houses instances given a simple primary key.
* @param hid
*/
public Houses(java.lang.Integer hid)
{
super(hid);
}

/* Add customized code below */

}
3) 将生成的文件导入到JBuilder工程中
三、编写测试类
我们的第一个测试类的代码如下:
package zy.pro.hrms.test;

import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import zy.pro.hrms.util.HibernateSessionFactory;
import zy.pro.hrms.dao.*;

public class HouseTest {
public HouseTest() {
}

public static void main(String args[]) {
try {
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();
for (int i = 0; i < 50; i++) {
Houses h = new Houses();
//h.setHid(new Integer(2));
h.setAlias("dddd");
h.setCapity(new Integer(4));
session.save(h);
tx.commit();
}
//tx.commit();
}
catch (Exception e) {
e.printStackTrace();
}

}
}
注意:
1) 此处的粗体内容,放的位置不同对程序有什么影响和不同效果(这是我的一个疑点)。
2) 我们在创建数据库时不能在数据库端实现主键的自动增长,而应该留给Hibernate来实现主键的生成

--------------------------此程序运行调试成功--------------------------
运行和编程环境:
window 2000+Jbuilder X+ SQL Server 2000
运行环境的配置另附
还有问题,请和我联系zhyiwww@21cn.com
 
Copyright © 2002-2012 JSPCN.net. All rights reserved.
JSP中文网    备案号:粤ICP备09171188号
成都恒海科技发展有限公司    成都市一环路南二段6号新瑞楼三楼8号