1 Hibernate 需要的jar包
asm-3.3.jar
cglib-2.2.2
commons-beanutils
commons-collections-3.1
commons-lang3-3.3.1
commons-logging
dom4j-1.6
hibernate-3.2
mysql-connector-java-5.1.5-bin
odmg-3.0
2Hibernate.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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test?<!-- test为数据库的名字 -->
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<mapping resource="com/lin/po/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
3 po 类的代码
package com.lin.po;
public class Person {
public Person() {
super();
// TODO Auto-generated constructor stub
}
private float id;
private String name;
public float getId() {
return id;
}
public void setId(float id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4 测试的代码
package com.lin.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.lin.po.Person;
public class TestHBM {
public static void main(String[] args) {
Person p =new Person();
p.setId(1.0f);
p.setName("linda");
add(p);
}
public static void add(Person p){
Transaction t =null;
try {
Configuration c =new Configuration().configure();
SessionFactory sf =c.buildSessionFactory();
Session s =sf.openSession();
s.beginTransaction();
s.save(p);
s.getTransaction().commit();
s.close();
sf.close();
System.out.println(c);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
5 常见的错误
(1)java.lang.NoClassDefFoundError: org/objectweb/asm/Type 解决:添加asm-3.3.jar
(2) java.lang.NoClassDefFoundError: net/sf/cglib/proxy/CallbackFilter 解决:添加cglib-2.2.2.jar
(3)java.lang.NoClassDefFoundError: org/apache/commons/collections/SequencedHashMap 解决:添加commons-collections-3.1.jar
(4) java.lang.NoClassDefFoundError: org/dom4j/DocumentException 解决:添加dom4j-1.6.jar
(5) org.apache.commons.logging.LogFactory 解决:添加commons-logging.jar
本文详细介绍了Hibernate框架的基础设置,包括所需Jar包、配置文件编写、POJO类定义及基本的增删改查操作,并针对常见错误提供了相应的解决方案。
1067

被折叠的 条评论
为什么被折叠?



