Tomcat中配置Hibernate的一个问题
各位大侠帮帮忙啊,这个问题搞了好几天了,都不知道错在哪了。问题描述如下:
Tomcat中的MySQL数据库连接池安装正确,我自己测试过了
D:\program file\Tomcat 5.0\webapps\register
Hibernate目录\lib下的所有.jar文件已经全部复制到Tomcat安装目录下的\webapps\register\WEB-INF\lib中(包括hibernate3.jar),hibernate.cfg.xml和register.hbm.xml已经放入到WEB-INF\classes中,
MySQL中有一个数据库名为quickstart,其中表t_register的字段有id(主键) int型,username varchar型,password varchar型,sex varchar型,age int型
HibernateUtil.java的源文件如下:
package register;
import org.hibernate.HibernateException;
import org.hibernate.cfg.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.*;
import java.io.*;
public class HibernateUtil{
private static final SessionFactory sessionFactory;
static{
try{
Configuration config=new Configuration().configure();
sessionFactory=config.buildSessionFactory();
}
catch(Throwable ex){
ex.printStackTrace();
System.out.println( "HibernateUtil出错 ");
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session=new ThreadLocal();
public static Session currentSession() throws HibernateException{
Session s=(Session)session.get();
if(s==null||!s.isOpen()){
s=sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException{
Session s=(Session)session.get();
session.set(null);
if(s!=null)
s.close();
}
}
持久化类如下 Register.java
package register;
public class Register{
private int id;
private String username;
private String password;
private String sex;
private int age;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id=id;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username=username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password=password;
}
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex=sex;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age=age;
}
}
hibernate.cfg.xml如下:
<?xml version= "1.0 " encoding= "utf-8 "?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN "
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
<hibernate-configuration>
<session-factory>
<property name= "connection.datasource "> java:comp/env/jdbc/quickstart </property>
<property name= "dialect "> org.hibernate.dialect.MySQLDialect </property>
<property name= "show_sql "> false </property>
<!-- Mapping files -->
<mapping resource= "register.hbm.xml "/>
</session-factory>
</hibernate-configuration>
映射文件如下 register.hbm.xml
<?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= "register.Register " table= "t_register ">
<id name= "id " column= "id " type= "int ">
<generator class= "increment "/>
</id>
<property name= "username " column= "username " type= "string "/>
<property name= "password " column= "password " type= "string "/>
<property name= "sex " column= "sex " type= "string "/>
<property name= "age " column= "age " type= "int "/>
</class>
</hibernate-mapping>
测试JSP文件如下:
testRegister.jsp文件
<%@ page language= "java " pageEncoding= "GB2312 " %>
<%@ page import= "org.hibernate.Transaction "%>
<%@ page import= "org.hibernate.Session "%>
<%@ page import= "org.hibernate.cfg.* "%>
<%@ page import= "org.hibernate.Query "%>
<%@ page import= "register.HibernateUtil "%>
<%@ page import= "register.Register "%>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en ">
<html>
<head> <title> Lomboz JSP </title> </head>
<body bgcolor= "#FFFFFF ">
<%
try{
Session ses=HibernateUtil.currentSession();
Transaction tx=ses.beginTransaction();
Register princess=new Register();
princess.setUsername( "oyhj ");
princess.setPassword( "oyhj ");
princess.setSex( "n ");
princess.setAge(25);
ses.save(princess);
tx.commit();
HibernateUtil.closeSession();
}
catch(Exception excep){
excep.printStackTrace();
out.println( "出错 ");
}
%>
</body>
</html>
输入
http://localhost:8088/register/testRegister.jsp
提示下面的错误:
java.lang.NoClassDefFoundError:
咱们论坛里好像有人遇见过这样的情况,但是他没有具体说如何解决的。
请各位高手帮忙啊
[解决办法]
错误的详细信息
用哪个class时报java.lang.NoClassDefFoundError
[解决办法]
java.lang.NoClassDefFoundError
提示的是类没有找到,你自己用单步跟踪调试看看在哪一步出错!
看你的代码好象没错误
[解决办法]
<id name= "id " column= "id " type= "int ">
<property name= "age " column= "age " type= "int "/>
把type中int改为Integer试试