-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Can't get mapping to class
PostPosted: Thu Feb 22, 2007 6:57 am 
Newbie

Joined: Thu Feb 22, 2007 6:42 am
Posts: 9
Hi,

I am using Hibernate3 for the first time. Tried to map a simple MySQL of 2 tables.

here is hibernate.cfg.xml file
Code:
<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : hibernate.cfg.xml
    Created on : February 21, 2007, 5:12 PM
    Author     : alim
    Description:
        Purpose of the document follows.
-->

<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

   
<hibernate-configuration>
    <session-factory>
   <property name="connection.username">
       ali
   </property>
   
   <property name="connection.url">
       jdbc:mysql://localhost/emailnotifier
   </property>
   
   <property name="dialect">
       org.hibernate.dialect.MySQLDialect
   </property>
   
   <property name="connection.password">
       ali
   </property>
   
   <property name="connection.driver_class">
       com.mysql.jdbc.Driver
   </property>
   
   <property name="hibernate.current_session_context_class">
       thread
   </property>

   <mapping resource="/database/beans/Users.hbm.xml" />
   
    </session-factory>
</hibernate-configuration>


The User.hbm.xml file is

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : Users.hbm.xml
    Created on : February 21, 2007, 5:21 PM
    Author     : alim
    Description:
        Purpose of the document follows.
-->

<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
   
<hibernate-mapping>
    <class name="database.beans.Users" table="users">
   <id name="id" type="integer">
       <column name="id"/>
       <generator class="assigned"/>
   </id>
   
   <property name="userName" type="string">
       <column name="UserName" length="20"/>
   </property>
   
   <propery name="userPassword" type = "string">
       <column name="UserPassword" length="20"/>
   </propery>
   
   <many-to-one name="role" class="database.beans.Roles">
       <column name="Role" />
   </many-to-one>
    </class>
</hibernate-mapping>


and my HibernateUtil class code is

Code:
package database.beans;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.HibernateException;
import org.hibernate.Session;


public class HibernateUtil {

    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";


    private static final ThreadLocal threadLocal = new ThreadLocal();

    private static final Configuration cfg = new Configuration();

    private static org.hibernate.SessionFactory sessionFactory;

    public static Session currentSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

      if (session == null || !session.isOpen()) {
         if (sessionFactory == null) {
            try {
               cfg.configure(CONFIG_FILE_LOCATION);
               sessionFactory = cfg.buildSessionFactory();
            } catch (Exception e) {
               System.err
                     .println("%%%% Error Creating SessionFactory %%%%");
               e.printStackTrace();
            }
         }
         session = (sessionFactory != null) ? sessionFactory.openSession()
               : null;
         threadLocal.set(session);
      }

        return session;
    }

    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);

        if (session != null) {
            session.close();
        }
    }
}


Now, i am using it to get a user with the following code
Code:
   Session session = HibernateUtil.currentSession();
   session.beginTransaction();
   try{
       userList = session.createQuery("from users ORDER by id").list();
       session.getTransaction().commit();
   }
   catch (HibernateException hx){
       session.getTransaction().rollback();
       throw hx;
   }


And finally i am getting this ERROR ... :(

Code:
org.hibernate.MappingException: Could not read mappings from resource: /database/beans/Users.hbm.xml
        at org.hibernate.cfg.Configuration.addResource(Configuration.java:518)
        at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1506)
        at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1474)
        at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1453)
        at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1427)
        at org.hibernate.cfg.Configuration.configure(Configuration.java:1347)
        at database.beans.HibernateUtil.currentSession(HibernateUtil.java:64)
        at database.beans.UsersManager.getUserById(UsersManager.java:41)
        at servlets.ProcessLogin.processRequest(ProcessLogin.java:38)
        at servlets.ProcessLogin.doPost(ProcessLogin.java:86)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
        at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
        at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
        at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
        at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)



Help me out.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 7:06 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi Muhammad ,

where is Role hbm


it should be added in mapping resource and check hbm path also

<mapping resource="/database/beans/Users.hbm.xml" />

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 7:28 am 
Newbie

Joined: Thu Feb 22, 2007 6:42 am
Posts: 9
Code:
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="database.beans.Roles" table="roles">
   <id name="id" type="integer">
       <column name="Id"/>
       <generator class="assigned"/>
   </id>
   
   <property name="roleName" type="string">
       <column name="RoleName" length="20"/>
   </property>   
    </class>
</hibernate-mapping>


My roles.hbm.xml file. Also i have changed mapping from "/database/beans/XXX.hbm.xml" to "database/beans/XXX.hbm.xml" but still getting the same error.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 9:11 am 
Newbie

Joined: Thu Feb 22, 2007 9:08 am
Posts: 1
Quote:
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


You need to change the dtd to "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"

in you User mapping file


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 9:23 am 
Newbie

Joined: Thu Feb 22, 2007 6:42 am
Posts: 9
I was expecting that it will remove the error. But nops, i am again getting the same error.

By the way thanks for pointing out this.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 9:28 am 
Beginner
Beginner

Joined: Fri Aug 12, 2005 7:05 am
Posts: 25
Location: TamilNadu
Not able to find the hbm resource. Just move your hbm files into root directory and change the path name in hibernate-configuration file and then try


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 9:45 am 
Newbie

Joined: Thu Feb 22, 2007 6:42 am
Posts: 9
Moved files to root directory where hibernate.cfg.xml is placed. changed the mapping resource tag to "Users.hbm.xml". But still getting errors.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 9:40 am 
Newbie

Joined: Thu Feb 22, 2007 6:42 am
Posts: 9
Ok, i am able to remove those errors. Just shifted my project from NetBEans 5.5 to Eclipse 3.2 with MyEclipse installed on it and i am able to run the relation free hibernate for Roles and Users.

Now, Roles object is being included in Users. When i earlier tried to load users without role object (using role as int instead of Roles class) it loaded fine. but now when i have made the relation it is giving me this exception.

Code:
org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of database.beans.Users.role


My Users.hbm.xml file is

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : Roles.xml
    Created on : February 22, 2007, 4:26 PM
    Author     : alim
    Description:
        Purpose of the document follows.
-->

<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">     

<hibernate-mapping>
   <class name="database.beans.Users" table="users">
      <id name="id" type="integer">
         <column name="Id"/>
         <generator class="assigned"/>
      </id>
      
      <property name="userName" type="string">
         <column name="UserName" />
      </property>
      
      <property name="userPassword" type="string">
         <column name="UserPassword" />
      </property>
      
      <many-to-one name="role" class="database.beans.Roles" fetch="join">
          <column name="Role" />
      </many-to-one>
      
   </class>
</hibernate-mapping>


and my Java file for Users is
Code:
/*
* Users.java
*
* Created on February 16, 2007, 1:12 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package database.beans;

/**
*
* @author alim
*/
public class Users {
   private int id;

   private String userName;

   private String userPassword;

   private String email;

   private Roles role;

   public Users() {
      id = 0;
      userName = new String("");
      userPassword = new String("");
      email = new String("");
//      role = 0;
      role = new Roles();
   }
   
   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 getUserPassword() {
      return userPassword;
   }

   public void setUserPassword(String userPassword) {
      this.userPassword = userPassword;
   }

   public String getEmail() {
      return email;
   }

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

   public int getRole() {
      return role.getId();
   }

   public void setRole(int role) {
      if (this.role == null) {
         this.role = new Roles();
      }
      this.role.setId(role);
   }
   
   public void setRole (Roles role){
      this.role = role;
   }
}


I hope this will be solved.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 26, 2007 7:18 am 
Newbie

Joined: Thu Feb 22, 2007 6:42 am
Posts: 9
no help yet ... :( ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.