为何找不到zpdb.hibernate.cfg.xml???

05-08-04 SmileWolf
我是利用Hibernate框架,写了一个测试程序,我把zpdb.hibernate.cfg.xml文件与类文件放在一个目录下,可系统报错找不到这个配置文件,请高手帮忙看看~~!程序如下:

package com.mytest.hibernate;

import net.sf.hibernate.HibernateException;

import net.sf.hibernate.Session;

import net.sf.hibernate.SessionFactory;

import net.sf.hibernate.Transaction;

import net.sf.hibernate.cfg.Configuration;

import net.sf.hibernate.cfg.Mappings;

import java.io.Serializable;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Hashtable;

import java.util.Map;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

public class HibernateUtil{

private static SessionFactory sessionFactory;

private static Map persistClassMap;

public static void init(){

persistClassMap=new Hashtable();

try {

Configuration conf=new Configuration();

sessionFactory=conf.configure("zpdb.hibernate.cfg.xml").buildSessionFactory();

} catch (HibernateException e) {

throw new RuntimeException("严重错误!加载Hibernate配置文件失败,原因是"+e.getMessage());

}

}

private static final ThreadLocal sessions=new ThreadLocal();

protected static Session getSession(boolean newSession) throws HibernateException{

if(newSession)

return sessionFactory.openSession();

Session s=(Session)sessions.get();

if(s==null){

s=sessionFactory.openSession();

sessions.set(s);

}

return s;

}

protected static void closeSession(){

Session s=(Session)sessions.get();

sessions.set(null);

if (s != null) {

try {

s.close();

} catch (HibernateException e) {

e.printStackTrace();

}

}

}

protected static void closeSession(Session s){

if (s != null)

try {

s.close();

} catch (HibernateException e) {

e.printStackTrace();

}

}

protected static void rollbackTransaction(Transaction tx){

if(tx!=null)

try {

tx.rollback();

} catch (HibernateException e) {

e.printStackTrace();

}

}

public static Connection getConnection(boolean newSession) throws HibernateException{

if(newSession)

return sessionFactory.openSession().connection();

Session s=(Session)sessions.get();

if(s==null){

s=sessionFactory.openSession();

sessions.set(s);

}

return s.connection();

}

public static void closeConnection(ResultSet rs,Statement stmt,Connection conn){

if(conn!=null){

try {

if(rs!=null)

rs.close();

if(stmt!=null)

stmt.close();

conn.close();

closeSession();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

public void addUser(UserPo user) throws Exception{

if( user == null )

throw new NullPointerException();

HibernateUtil.init();

Transaction tx = null;

Session s = null;

try{

s = this.getSession(false);

tx = s.beginTransaction();

s.save(user);

tx.commit();

}

catch(HibernateException e){

rollbackTransaction(tx);

}finally{

closeSession();

}

}

}

banq
2005-08-04 19:13
找不到配置文件 一定要在Hibernate.cfg.xml中加入该配置 或代码加入,如果还是找不到,就不是hibernate使用问题了,属于Web打包问题,需要将配置打包到WEB-INF/classess目录下或classpath

SmileWolf
2005-08-05 19:18
谢谢Banq,现在又有了另一个问题,就是mapping resource文件读取错误,我用的是websphere5.1应用服务器,系统报错如下:

E net.sf.hibernate.util.XMLHelper Error parsing XML: XML InputStream(17) 元素类型“class”的内容必须匹配“(meta*,(cache|jcs-cache)?,(id|composite-id),discriminator?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|any|map|set|list|bag|idbag|array|primitive-array)*,(subclass*|joined-subclass*))”。

mapping resource文件如下:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 2.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="com.mystrutstest.hibernate" auto-import="true">

<!-- com.mystrutstest.hibernate".UserPO root -->

<class name="UserPO" table="UserInfo">

<property name="username" column="username" type="string" length="10" not-null="true"/>

<property name="password" column="password" type="string" length="10" not-null="true"/>

<property name="userno" column="userno" type="string" length="10" not-null="true" unique="true"/>

<property name="imageno" column="imageno" type="int" not-null="true"/>

<property name="country" column="country" type="string"/>

<property name="city" column="city" type="string"/>

<property name="sex" column="sex" type="int"/>

<property name="onlinetag" column="onlinetag" type="int"/>

<property name="description" column="description" type="string"/>

</class>

</hibernate-mapping>

我的表结构如下:

create table UserInfo(

username char(10) not null,

userno char(10) not null primary key,

imageno int not null,

country varchar(20),

city varchar(20),

sex int,

description varchar(100),

onlinetag int,

password char(10) not null

)

我找不到哪里错了~~~~~

SmileWolf
2005-08-05 19:20
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 2.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="com.mystrutstest.hibernate" auto-import="true">

<!-- com.mystrutstest.hibernate".UserPO root -->

<class name="UserPO" table="UserInfo">

<property name="username" column="username" type="string" length="10" not-null="true"/>

<property name="password" column="password" type="string" length="10" not-null="true"/>

<property name="userno" column="userno" type="string" length="10" not-null="true" unique="true"/>

<property name="imageno" column="imageno" type="int" not-null="true"/>

<property name="country" column="country" type="string"/>

<property name="city" column="city" type="string"/>

<property name="sex" column="sex" type="int"/>

<property name="onlinetag" column="onlinetag" type="int"/>

<property name="description" column="description" type="string"/>

</class>

</hibernate-mapping>

banq
2005-08-06 09:37
不用着急,我早就说过,没有工具脚本辅助,hibernate出了问题查起来也是比较费劲的,我做Hibernate碰到过几次了,束手无策,我做EJB的CMP(使用JBuilder)从来没碰到过这么费劲的事情。

所以,我现在宁可使用SQL;就象“宁可使用socket”一样。以上只是个人意见。

SmileWolf
2005-08-08 15:42
谢谢Banq,错误确实比较奇怪,不过我还是解决了,解决的方法是不用原来的主键,原来的是Varchar类型的,我采用了一个自增的ID做主键,就解决了原来的问题。问题奇怪,解决问题的方法更奇怪~~!

shipenggedit
2005-08-22 16:08
这个问题我也遇到过,

你的映射配置文件语法有问题。需要检查一下。

具体映射关系描述有问题。

猜你喜欢