`
hacer9791
  • 浏览: 1106 次
  • 性别: Icon_minigender_1
  • 来自: 江苏
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

好心人帮忙哈 Could not read mappings from resource

阅读更多
    

我用struts+hibernate开发一个bbs论坛出现了问题
工具是 Myeclipse+MySql
jdk6.0      tomcat6.0
问题描述:有两个.hbm.xml文件分别是user.hbm.xml和sort.hbm.xml.   与之对应的是两个.java文件User.java和Sort.java.
还有个hibernate.cfg.xml文件
user.hbm.xml和sort.hbm.xml在hibernate包内
hibernate.cfg.xml在hibernate包的上级目录里
以下为正文:
hibernate.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://127.0.0.1:3306/bookstore</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="myeclipse.connection.profile">MySQL</property>
        <property name="connection.password">123</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <mapping resource="hibernate/user.hbm.xml"/>
    <mapping resource="hibernate/sort.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

user.hbm.xml文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping >

<class table="bbsuser" name="StudyBbs.User">
<id name="id" column="id">
<generator class="increment"/>
</id>
<property name="username" column="username" type="String"></property>
<property name="password" column="password" type="String"></property>
<property name="nickname" column="nickname" type="String"></property>
<property name="sex" column="sex" type="String"></property>
<property name="birthyear" column="birthyear" type="String"></property>
<property name="birthmonth" column="birthmonth" type="String"></property>
<property name="birthday" column="birthday" type="String"></property>
<property name="email" column="email" type="String"></property>
<property name="mobile" column="mobile" type="String"></property>
<property name="signiture" column="signiture" type="String"></property>
<property name="grade" column="grade" type="String"></property>
<property name="score" column="score" type="Integer"></property>
</class>
</hibernate-mapping>

User.java类:
package StudyBbs;
import java.util.*;
import java.sql.Connection;
import java.sql.ResultSet;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.cfg.*;
import StudyBbsForm.LoginUserForm;
import hibernate.HibernateSessionFactory;
import org.hibernate.Session;
public class User { 
private int id;
private String username = null;
private String password = null;
private String nickname = null;
private String sex = null;
private String birthyear = null;
private String birthmonth = null;
private String birthday = null;
private String email = null;
private String mobile = null;
private String signiture = null;
private String grade = null;
private int score = 0;

 
public User(){}
public void setUsername(String username) {
  this.username = username;
}


  
public int getId() {
  return id;
}
public void setId(int id) {
  this.id = id;
}
public String getUsername() {
  return username;
}
  
public void setPassword(String password) {
  this.password = password;
}
  
public String getPassword() {
  return password;
}
public void setNickname(String nickname) {
  this.nickname = nickname;
}
  
public String getNickname() {
  return nickname;
}

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

public void setBirthyear(String birthyear) {
  this.birthyear = birthyear;
}
  
public String getBirthyear() {
  return birthyear;
}

public void setBirthmonth(String birthmonth) {
  this.birthmonth = birthmonth;
}
  
public String getBirthmonth() {
  return birthmonth;
}

public void setBirthday(String birthday) {
  this.birthday = birthday;
}
  
public String getBirthday() {
  return birthday;
}

public void setEmail(String email) {
  this.email = email;
}
  
public String getEmail() {
  return email;
}

public void setMobile(String mobile) {
  this.mobile = mobile;
}
  
public String getMobile() {
  return mobile;
}

public void setSigniture(String signiture) {
  this.signiture = signiture;
}
  
public String getSigniture() {
  return signiture;
}

public void setGrade(String grade) {
  this.grade = grade;
}
  
public String getGrade() {
  return grade;
}
public void setScore(int score) {
  this.score = score;
}
  
public int getScore() {
  return score;
}

public static boolean checkUser(String name,String psw)
throws Exception{
  Session session=null;
 
   session=HibernateSessionFactory.getSession();
     Transaction trancation=session.beginTransaction();
     Query query=session.createQuery("from User as a where a.username='"+name+"' and a.password='"+psw+"'");
     Iterator it=query.iterate();
     if(it.hasNext()){
      return true;
     }else{
      return false;
     }
 
   
}

public static String  getUserGrade(DB db,String name) throws Exception{
        String strSql;
  ResultSet rs;
      
       String strHql="from User as u where u.username='"+name+"'";
  Iterator it=db.OpenSql(strHql);
        if ( it.hasNext()) {
         User user=(User)it.next();
   return user.getGrade();
  }
  else{
   return null;
  }

}

public static Vector Search(DB db ,String username) throws Exception{
  Vector Users = new Vector();
  ResultSet rs,rsNest;
        String strSql=null;
 
  String strHql="from User as u where u.username like '%"+username+"%'";
        Iterator it = db.OpenSql(strHql);
 
  while  (it.hasNext()){
   User u=(User)it.next();
   User user = new User();
  
   user.setUsername(u.getUsername()) ;
   user.setGrade(u.getGrade()) ;
  
   Users.add(user);
  }
  return Users;
}

public static Vector SearchMaster(DB db) throws Exception{
  Vector Users = new Vector();
  ResultSet rs,rsNest;
        String strSql=null;
 
       String strHql="from User as u where u.grade='斑竹'";
        Iterator it = db.OpenSql(strHql);
 
  while  (it.hasNext()){
   User u=(User)it.next();
   User user = new User();
  
   user.setUsername(u.getUsername()) ;
   user.setGrade(u.getGrade()) ;
  
   Users.add(user);
  }
    
  return Users;
}
}

sort.hbm.xml文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping >
<class table="sort" name="StudyBbsForm.SortForm">
<id name="id" column="id">
<generator class="increment"/>
</id>
<property name="name" column="sortname" type="String"></property>
<property name="master" column="master" type="String"></property>
</class>
</hibernate-mapping>

Sort.java文件:
package StudyBbs;
import java.util.*;
import java.sql.Connection;
import java.sql.ResultSet;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import hibernate.HibernateSessionFactory;
public class Sort implements java.io.Serializable{

private int id;
private String name;
private String master;
private int topicNum;
private int lastTopicId;
private String lastTopic="";
private String lastTopicOwner="";
private String lastTopicTime="";
public Sort(){}
public Sort(int id,String name,String master,int topicNum,int lastTopicId,String lastTopic,String lastTopicOwner,String lastTopicTime){
  this.id = id;
  this.name = name;
  this.master = master;
  this.topicNum = topicNum;
  this.lastTopicId = lastTopicId;
  this.lastTopic = lastTopic;
  this.lastTopicOwner = lastTopicOwner;
  this.lastTopicTime = lastTopicTime;
  
}

public int getId(){
  return id;
}

public String getName(){
  return name;
}

public String getMaster(){
  return master;
}

public int getTopicNum(){
  return topicNum;
}

public int getLastTopicId(){
 
  return lastTopicId;
 
}

public String getLastTopic(){
 
  return lastTopic;
 
}

public String getLastTopicOwner(){
 
  return lastTopicOwner;
 
}

public String getLastTopicTime(){
 
  return lastTopicTime;
 
}

public static Vector Search(DB db) throws Exception{
  Vector sorts=new Vector();
  Iterator it,topicIt;
  int topicNum=0;
  int lastTopicId;
  String lastTopic;
  String lastTopicOwner;
  String lastTopicTime;
  String strHql;
  String countHql;
  String master,sortname;
  int sortid;
  strHql="from Sort order by id desc";
  it=db.OpenSql(strHql);
  while(it.hasNext()){
   lastTopicId =0;
   lastTopic=null;
   lastTopicOwner=null;
   lastTopicTime=null;
   Sort sort=(Sort)it.next();
   sortid=sort.getId();
   master=sort.getMaster();
   sortname=sort.getName();
   countHql="select count(*) from Topic as a where a.id="+sortid;
   topicNum=db.getCount(countHql);
   strHql="from Topic as a where a.sortid="+sortid+"order by id desc";
   topicIt=db.OpenSql(strHql);
   if(topicIt.hasNext()){
    Topic topic=(Topic)it.next();
    lastTopicId = topic.getId();
    lastTopic = topic.getTitle();
    lastTopicTime = topic.getTime();
    lastTopicOwner = topic.getOwner();
   
   }
   sorts.add(new Sort(
     sortid,
     sortname,
     master,
     topicNum,
     lastTopicId,
     lastTopic,
     lastTopicTime,
     lastTopicOwner   
     ));
  }
  return sorts;
}

抛出的异常是:
%%%% Error Creating SessionFactory %%%%
org.hibernate.MappingException: Could not read mappings from resource: hibernate/sort.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:485)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1465)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1433)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1414)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1390)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1310)
at hibernate.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:30)
at StudyBbs.User.checkUser(User.java:139)
at StudyBbsAction.LoginAction.execute(LoginAction.java:46)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.PropertyNotFoundException: field not found: id
at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:97)
at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:104)
at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:104)
at org.hibernate.property.DirectPropertyAccessor.getGetter(DirectPropertyAccessor.java:112)
at org.hibernate.util.ReflectHelper.getter(ReflectHelper.java:89)
at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:77)
at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:276)
at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:410)
at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:343)
at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:282)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:153)
at org.hibernate.cfg.Configuration.add(Configuration.java:386)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:427)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:482)
... 26 more
%%%% Error Creating SessionFactory %%%%
org.hibernate.MappingException: Could not read mappings from resource: hibernate/user.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:485)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1465)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1433)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1414)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1390)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1310)
at hibernate.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:69)
at hibernate.HibernateSessionFactory.getSession(HibernateSessionFactory.java:53)
at StudyBbs.User.checkUser(User.java:139)
at StudyBbsAction.LoginAction.execute(LoginAction.java:46)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping StudyBbs.User
at org.hibernate.cfg.Mappings.addClass(Mappings.java:118)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:154)
at org.hibernate.cfg.Configuration.add(Configuration.java:386)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:427)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:482)
... 27 more
2008-8-11 18:41:20 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet action threw exception
java.lang.NullPointerException
at StudyBbs.User.checkUser(User.java:140)
at StudyBbsAction.LoginAction.execute(LoginAction.java:46)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Unknown Source)

急。。。。。希望你们能帮到我,感谢所有看到此贴的人





  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics