- 论坛徽章:
- 0
|
Hibernate3.2版本与以前的2.x相比,最令人激动的特征是加入了JDK5.0的Annotation支持,而且与JPA兼容。这个我早就听说了。与他们有相同功能的是XDoclet。不过我今天想接绍给大家的并不是他,在实际的开发中XDoclet往往需要更多的维护。Hibernate Annotation 中文文档已经由满江红翻译团队翻译完成,你可以到这里下载:
![]()
文件:
hibernate_annotations.pdf
大小:
447KB
下载:
下载
Hibernate annotation可以减轻我们每一次都需要配置XXX.hbm.xml,可以减轻dba的工作量,使程序从一个数据库移植到另一个数据库更轻松,这些工作都交于hibernate内部自动维护。
在我们开始之前你需要做一些准备工作:
下载:
hibernate core3.2+
Hibernate Annotation
并将这些包导入到我们的classpath中去,这些类包括:ejb-persistence.jar,hibernate-annotations.jar,hibernate-commons-annotatians.jar,log4j.jar,slf4j-log4j12-1.5.2.jar,slf4j-api-1.5.2.jar.当然hibernate运行时的核心包及数据库驱动程序也是必不可少的。
让hibernate自动更新数据库schema,我们只需要在hibernate.cfg.xml文件中加入属性hibernate.hbm2ddl.auto,如下:
?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 >
property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver/property>
property name="hibernate.connection.password">/property>
property name="hibernate.connection.url">jdbc:mysql://localhost:3306/danan/property>
property name="hibernate.connection.username">root/property>
property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect/property>
property name="hibernate.hbm2ddl.auto">create-drop/property>
property name="show_sql">true/property>
mapping class="com.danan.User"/>
/session-factory>
/hibernate-configuration>
参数hibernate.hbm2ddl.auto可以是以下值:
validate 加载hibernate时,验证创建数据库表结构
create 每次加载hibernate,重新创建数据库表结构
create-drop 加载hibernate时创建,退出是删除表结构
update 加载hibernate自动更新数据库结构
create-drop在项目测试阶段特别的好用,我们姑且有这一选项。如果你够仔细的话,可以发现上面配置文件的:mapping class="com.danan.User"/>,请注意的是这个不再是以前我们见到的resource。
我们的com.danan.User如下所示:
package com.danan;
import java.io.Serializable;
import javax.persistence.*;
/**
* @author Administrator
*
*/
@Entity(name = "Tuser")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "userId")
private Long id = null;
@Column(name = "userName")
private String name = null;
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;
}
}
在我们第一次初始化SessionFactory 时,hibernate就会在指定的位置生成数据库结构:
mysql> describe tuser;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| userId | bigint(20) | NO | PRI | NULL | auto_increment |
| userName | varchar(255) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
2 rows in set (0.02 sec)
我们需要注意的是:我们第一次初始化sessionFactory时必须以这样的形式调用:
// build sessionFactory
static {
try {
sessionFactory = new AnnotationConfiguration().configure()
.buildSessionFactory();
// sessionFactory is now in JNDI ,see hibernate.cfg.xml
} catch (Throwable ex) {
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/63956/showart_1134072.html |
|