我的第一个Hibernate实例

用的是Hibernate3.6+MyEclipse6.0,以下是一个简单的例子:

(1)导入所要用到的包

antlr-2.7.6.jar,commoms-collections-3.1.jar,dom4j-1.6.1.jar,javassist-3.12.0.GA.jar,jta-1.1.jar,slf4j-api-1.6.1.jar,hibernate3.jar,slf4j-nop-1.6.1.jar,hibernate-jpa-2.0-api-1.0.0.Final.jar,mysql驱动包。

(2)在mysql中建立对应的数据库以及表

create hibernate;

use hibernate;

create table student( id in primary key,name varchar(20),age int);

(3)建立hibernate的配置文件hibernate.cfg.xml(在src目录下)

<?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:3306/hibernate
</property>
<property name="connection.username">XXX</property>
<property name="connection.password">XXX</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="com/hibernate/Student.hbm.xml" />
</session-factory>

</hibernate-configuration>

(4)建立Student类

package com.hibernate;

public class Student {
private int id;
private String name;
private int age;

public Student(){

}

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;
}

}
(5)建立Student的映射文件Student.hbm.xml(和Student类放在一起)

<?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 package="com.hibernate" >
<class name="Student" >
<id name="id" >
</id>
<property name="name" />
<property name="age" />
</class>
</hibernate-mapping>

(6)将映射文件加入到hibernate.cfg.xml中

<mapping resource="com/hibernate/Student.hbm.xml" />
(7)测试类TestStudent
package test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.hibernate.Student;

public class TestStudent {
public static void main(String [] args){
Student stu=new Student();
stu.setId(1);
stu.setName("Bob");
stu.setAge(12);
Configuration config=new Configuration().configure();
SessionFactory sessionFactory=config.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction transaction=session.beginTransaction();
session.save(stu);
transaction.commit();
session.close();
sessionFactory.close();
}
}

运行,控制台输出Hibernate: insert into Student (name, age, id) values (?, ?, ?)

查看数据库表student,发现数据添加成功

(8)小结

实现这个例子的过程中出现了一些问题

1.Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
at org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults(JPAMetadataProvider.java:96)
at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaReflectionManager.java:226)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1358)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1829)
at test.TestStudent.main(TestStudent.java:17)

原因:hibernate-jpa-2.0-api-1.0.0.Final.jar没有导入

2.Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]

在网上查了好久,都说是没有加javassist包,其实不是这样,是我的映射文件写错了,<property name="age" />我写成了<property name="age " />就因为粗心大意害得我弄了好长时间,这也给了我一个教训,就是写程序一定要小心,注意细节。





你可能感兴趣的:(Hibernate)