标签:hibernate
首先我们将创建一个简单的基于控制台的(console-based)Hibernate应用程序。
我们所做的第一件事就是创建我们的开发目录,并且把所有需要用到的Java库文件放进去。解压缩从Hibernate网站下载的Hibernate发布包,并把所有需要的库文件拷到我们项目中去。(a)项目右键-buildpath-configure build path-add library
(c)在该library中加入hibernate所需jar包
到编写本文时为止,这些是Hibernate运行所需要的最小库文件集合(注意我们也拷贝了 Hibernate3.jar,这个是最主要的文件)。
你正使用的Hibernate版本可能需要比这更多或少一些的库文件。请参见发布包中的lib/目录下的README.txt,以获取更多关于所需和可选的第三方库文件信息(事实上,Log4j并不是必须的库文件,但被许多开发者所喜欢)。
(d) 引入sqlserver的JDBC驱动包
在sqlserver中创建表StudentInfo
接下来我们创建一个类,用来代表那些我们希望储存在数据库里的student。
我们的第一个持久化类是一个带有一些属性(property)的简单JavaBean类:
package com.model;
public class StudentInfo {
private int id;
private String name;
private int age;
private String sex;
//构造方法
public StudentInfo(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
Hibernate需要知道怎样去加载(load)和存储(store)持久化类的对象。这正是Hibernate映射文件发挥作用的地方。
映射文件告诉Hibernate它,应该访问数据库(database)里面的哪个表(table)及应该使用表里面的哪些字段(column)。
一个映射文件的基本结构看起来像这样:
<?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>
[...]
</hibernate-mapping>
<hibernate-mapping>
<class name="com.model.StudentInfo" table="StudentInfo">
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.model.StudentInfo" table="StudentInfo">
<id name="id" column="ID">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.model.StudentInfo" table="StudentInfo">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="date" type="timestamp" column="EnterDate"/>
<property name="title"/>
</class>
</hibernate-mapping>
和id元素一样,property元素的name属性告诉Hibernate使用哪个getter和setter方法。在此例中,Hibernate会寻找getDate()/setDate(), 以及getTitle()/setTitle()。
为什么date属性的映射含有column attribute,而title却没有?当没有设定column attribute 的时候,Hibernate缺省地使用JavaBean的属性名作为字段名。对于title,这样工作得很好。然而,date在多数的数据库里,是一个保留关键字,所以我们最好把它映射成一个不同的名字。
另一有趣的事情是title属性缺少一个type attribute。我们在映射文件里声明并使用的类型,却不是我们期望的那样,是Java数据类型,同时也不是SQL数据库的数据类型。这些类型就是所谓的Hibernate 映射类型(mapping types),它们能把Java数据类型转换到SQL数据类型,反之亦然。再次重申,如果在映射文件中没有设置type属性的话,Hibernate会自己试着去确定正确的转换类型和它的映射类型。在某些情况下这个自动检测机制(在Java
类上使用反射机制)不会产生你所期待或需要的缺省值。date属性就是个很好的例子,Hibernate无法知道这个属性(java.util.Date类型的)应该被映射成:SQL
date,或timestamp,还是time 字段。在此例中,把这个属性映射成timestamp 转换器,这样我们预留了日期和时间的全部信息。
应该把这个映射文件保存为Event.hbm.xml,且就在StudentInfo Java类的源文件目录下。映射文件可随意地命名,但hbm.xml的后缀已成为Hibernate开发者社区的约定。
对于StudentInfo的配置如下:
<?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.model.StudentInfo" table="StudentInfo">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="name" column = "Name"/>
<property name="age" column = "Age"/>
<property name="sex" column = "Sex"/>
</class>
</hibernate-mapping>我们继续进行Hibernate的主要配置。
现在我们已经有了一个持久化类和它的映射文件,该是配置Hibernate的时候了。在此之前,我们需要一个数据库。
Hibernate是你的应用程序里连接数据库的那层,所以它需要连接用的信息。连接(connection)是通过一个也由我们配置的JDBC连接池(connection pool)来完成的。
Hibernate的发布包里包含了许多开源的(open source)连接池。注意,如果你希望使用一个产品级(production-quality)的第三方连接池软件,你必须拷贝所需的库文件到你的classpath下,并使用不同的连接池设置。
为了保存Hibernate的配置,我们可以使用一个简单的hibernate.properties文件,或者一个稍微复杂的hibernate.cfg.xml,甚至可以完全使用程序来配置Hibernate。多数用户更喜欢使用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">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<!-- MySql配置 -->
<!--<property name="connection.driver_class">com.mysql.jdbc.Driver</property> -->
<!--<property name="connection.url">jdbc:mysql://localhost/hibernate</property> -->
<!-- SQL dialect -->
<!--<property name="dialect">org.hibernate.dialect.MySQLDialect</property>-->
<!-- SqlServer配置 -->
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://localhost/Hibernate</property>
<!-- 数据库用户名 -->
<property name="connection.username">sa</property>
<!-- 数据库密码 -->
<property name="connection.password">123</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="com/model/StudentInfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Hibernate学习笔记(1)Hibernate配置,布布扣,bubuko.com
标签:hibernate
原文地址:http://blog.csdn.net/sunnyyoona/article/details/38702805