一 环境:W7+Eclipse3.6
二 所用Hibernate版本:hibernate-distribution-3.6.0.Final
工程目录结构如下
三 参考资料
1 Hibernate3.6的Annotation问题
http://mayuchuan99.blog.163.com/blog/static/320023442011029438354/
hibernate3.6之前的版本使用Annotation,还需要下载Annotation库,需要添加ejb3-persistence.jar hibernate-annotations.jar hibernate-commons-annotations.jar,而获得SessionFactory必须以下这样写
Configuration cfg=new AnnotationConfiguration();
SessionFactory s=cfg.configure().buildSessionFactory();
在hibernate3.6的这个版本中,Annotation类库集成到了hibernate3.6,所以不在需要添加hibernate-annotations.jar hibernate-commons-annotations.jar等类库了。但是必须添加hibernate-jpa-2.0-api-1.0.0.Final.jar。
hibernate 3.6要获取一个SessionFactory,我可以直接像用xml来配置实体与数据库表的映射关系那样。代码如下:
Configuration cfg=new Configuration();
SessionFactory s=cfg.configure().buildSessionFactory();
四 具体代码
1 hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in)
<property name="connection.pool_size">1</property>
-->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</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">update</property>
-->
<!--
<mapping resource="org/hibernate/model/Student.hbm.xml"/>
-->
<mapping class="org.hibernate.model.Student"/>
</session-factory>
</hibernate-configuration>
2 Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
<class name="org.hibernate.model.Student">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="age"/>
</class>
</hibernate-mapping>
3 Student.java
@Entity
public class Student {
@Id
@GeneratedValue
private int id;
private String name;
private int age;
//get set
}
4 HibernateUtil.java
package org.hibernate.model;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
}catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
5 Test.java
import org.hibernate.Session;
public class Test {
public static void main(String[] args) {
Student s = new Student();
// 设置了ID的自动递增,就不用在指定ID值
// s.setId(1);
s.setName("zhangsan");
s.setAge(8);
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
}
}
6 mysql.sql
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(3) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
INSERT INTO `student` VALUES ('1', 'zhangsan', '8');