第一个成功的Hibernate实例
数据库设计
注意:自增主键这一功能将在以后的Mapping中由Hibernate 实现,在 Hibernate 中有很多种主键的生成算法
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类和映射文件
3)不会用以上两种方法的同志请参阅MyEclipse的帮助文档,此处省略
4)此处以notice为例,生成的原代码和映射文件如下:
原代码:
注意:如果把get方法写为getname()或者getNAME(),会导致Hibernate在运行时抛出以下异常:
net.sf.hibernate.PropertyNotFoundException:
Could not find a getter
for property name in class mypack.Customer
如果已经出现这个异常,请您改下大小写
package
com.demo.hibernate;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
Notice
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private int nid;
private String title;
private String content;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getContent() ...{
return content;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setContent(String content) ...{
this.content = content;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public int getNid() ...{
return nid;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setNid(int nid) ...{
this.nid = nid;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getTitle() ...{
return title;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setTitle(String title) ...{
this.title = title;
}
}
配置文件:Notice.hbm.xml
<?
xml version="1.0" encoding="utf-8"
?>
<!
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<
hibernate-mapping
>
<
class
name
="com.demo.hibernate.Notice"
table
="notice"
catalog
="rmsdb"
>
<
id
name
="nid"
type
="java.lang.Integer"
>
<
column
name
="NID"
/>
<
generator
class
="increment"
></
generator
>
</
id
>
<
property
name
="title"
type
="java.lang.String"
>
<
column
name
="title"
length
="20"
not-null
="true"
/>
</
property
>
<
property
name
="content"
type
="java.lang.String"
>
<
column
name
="content"
length
="200"
/>
</
property
>
</
class
>
</
hibernate-mapping
>
生成的hibernate.cfg.xml
<?
xml version='1.0' encoding='UTF-8'
?>
<!
DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!--
Generated by MyEclipse Hibernate Tools.
-->
<
hibernate-configuration
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
session-factory
>
<
property
name
="connection.username"
>
root
</
property
>
<
property
name
="connection.url"
>
jdbc:mysql://localhost:3306/rmsdb
</
property
>
<
property
name
="dialect"
>
org.hibernate.dialect.MySQLDialect
</
property
>
<
property
name
="myeclipse.connection.profile"
>
MySQL
</
property
>
<
property
name
="connection.password"
>
admin
</
property
>
<
property
name
="connection.driver_class"
>
com.mysql.jdbc.Driver
</
property
>
<
mapping
resource
="com/demo/hibernate/Customers.hbm.xml"
/>
<
mapping
resource
="com/demo/hibernate/Houses.hbm.xml"
/>
<
mapping
resource
="com/demo/hibernate/Menus.hbm.xml"
/>
<
mapping
resource
="com/demo/hibernate/Notice.hbm.xml"
/>
<
mapping
resource
="com/demo/hibernate/Staffs.hbm.xml"
/>
<
mapping
resource
="com/demo/hibernate/Tables.hbm.xml"
/>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
</
session-factory
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
</
hibernate-configuration
>
完成插入Notice表的代码,可将此代码放到NoticeDAO文件中,也可放在main中,此例中,我放在了NoticeDAO文件中,代码如下:
Configuration configuration;
SessionFactory sessionFactory;
Session session;
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
void
saveNotice(Notice notice)
...
{
configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(notice);
tx.commit();
session.close();
}
在主函数中调用即可完成,主函数代码如下:
public
class
ExecuteTransaction
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main(String[] args)...{
Notice notice = new Notice();
notice.setTitle("NoticeTitle");
notice.setContent("NoticeContent");
NoticeDAO noticeDAO = new NoticeDAO();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for(int i=0;i<50;i++)...{
noticeDAO.saveNotice(notice);
}
}
}