Hibernate框架中的,could not find the main class 的问题

92浩 2015-04-13 09:07:40
实体类如下:
com.mys.model
package com.mys.model;
// default package

import java.util.Date;


/**
* User entity. @author MyEclipse Persistence Tools
*/

public class User implements java.io.Serializable {


// Fields

private String id;
private String userName;
private String userPwd;
private String sex;
private Integer age;
private String jiguan;
private Date userRegisterTime;


// Constructors

/** default constructor */
public User() {
}

/** minimal constructor */
public User(String id, String userName, String userPwd, Date userRegisterTime) {
this.id = id;
this.userName = userName;
this.userPwd = userPwd;
this.userRegisterTime = userRegisterTime;
}

/** full constructor */
public User(String id, String userName, String userPwd, String sex, Integer age, String jiguan, Date userRegisterTime) {
this.id = id;
this.userName = userName;
this.userPwd = userPwd;
this.sex = sex;
this.age = age;
this.jiguan = jiguan;
this.userRegisterTime = userRegisterTime;
}


// Property accessors

public String getId() {
return this.id;
}

public void setId(String id) {
this.id = id;
}

public String getUserName() {
return this.userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserPwd() {
return this.userPwd;
}

public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}

public String getSex() {
return this.sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public Integer getAge() {
return this.age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getJiguan() {
return this.jiguan;
}

public void setJiguan(String jiguan) {
this.jiguan = jiguan;
}

public Date getUserRegisterTime() {
return this.userRegisterTime;
}

public void setUserRegisterTime(Date userRegisterTime) {
this.userRegisterTime = userRegisterTime;
}

}


业务类如下:
com.mys.util
package com.mys.util;
/**
* 我们必须启动Hibernate,此过程包括创建一个全局的SessoinFactory,并把它储存在应用程序代码容易访问的地方。
*/

import java.util.List;

import com.mys.model.*;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateUtil{

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory from hibernate.cfg.xml
//以前hibernate文档里的: sessionFactory = new Configuration().configure().buildSessionFactory();

Configuration config=new Configuration();
config.configure();
config.addClass(User.class);//完整类名?
sessionFactory=config.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 void showUsers()
{
Session session = sessionFactory.openSession();
Transaction tx=null;
try{
tx=session.beginTransaction();
Query query=session.createQuery("from tb_User as u");
List list=query.list();//List是util.*类库里的接口
System.out.println("id"+"用户名"+"用户密码"+"性别"+"年龄"+"籍贯"+"注册时间");
for(int i=0;i<list.size();i++)
{
User u=(User)list.get(i);
System.out.print(u.getId());
// 省略部分
System.out.print(u.getUserName());
System.out.print(u.getUserPwd());
System.out.print(u.getSex());
System.out.print(u.getAge());
System.out.print(u.getJiguan());
System.out.print(u.getUserRegisterTime());
System.out.println("");
}
tx.commit();
}
catch(Exception e){
e.printStackTrace();
}finally{
session.close();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
HibernateUtil h=new HibernateUtil();
h.showUsers();
}

}


配置文件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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/db_test
</property>
<property name="connection.username">root</property>
<property name="connection.password">425175</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
com.mysql.jdbc.Driver
</property>
<mapping resource="./User.hbm.xml" />

</session-factory>

</hibernate-configuration>


映射文件在src目录下User.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd ">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="User" table="db_user" catalog="db_test">
<id name="id" type="java.lang.String">
<column name="id" length="15" />
<generator class="assigned"></generator>
</id>
<property name="userName" type="java.lang.String">
<column name="userName" length="20" not-null="true" />
</property>
<property name="userPwd" type="java.lang.String">
<column name="userPwd" length="20" not-null="true" />
</property>
<property name="sex" type="java.lang.String">
<column name="sex" length="4" />
</property>
<property name="age" type="java.lang.Integer">
<column name="age" />
</property>
<property name="jiguan" type="java.lang.String">
<column name="jiguan" length="20" />
</property>
<property name="userRegisterTime" type="java.util.Date">
<column name="userRegisterTime" length="10" not-null="true" />
</property>
</class>
</hibernate-mapping>


我想用Hibernate实现一个查询数据库里所有数据的功能,映射文件和实体类都是自动生成的,业务类是自己参照着数编写的。
错误如下:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Could not parse mapping document from resource ./User.hbm.xml
java.lang.ExceptionInInitializerError
at com.mys.util.HibernateUtil.<clinit>(HibernateUtil.java:32)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource ./User.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
at com.mys.util.HibernateUtil.<clinit>(HibernateUtil.java:25)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:508)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
... 7 more
Caused by: org.dom4j.DocumentException: http://hibernate.org/dtd/hibernate-mapping-3.0.dtd%20 Nested exception: http://hibernate.org/dtd/hibernate-mapping-3.0.dtd%20
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499)
... 8 more
Exception in thread "main"
...全文
96 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
liangtu 2015-04-14
  • 打赏
  • 举报
回复
异常信息中没有:could not find the main class

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧