-->
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.  [ 5 posts ] 
Author Message
 Post subject: MappingException: No persister for: com.review.data.User
PostPosted: Wed Sep 24, 2003 4:18 pm 
Newbie

Joined: Wed Sep 24, 2003 3:30 pm
Posts: 7
Hi,

I'm new to Hibernate and am having trouble accessing my data. I'm using MySQL and the MySQL connector driver 3.08. I created a user table and one user record where the primary key (id) = 1.

When I try to retrieve this, using either Session.load, Session.find, or Query.list, I get a MappingException: No persister for: com.review.data.User. If I try to add a new record, I also get this error.

I tried explicitly adding the persister atttibute to the class element in the User.hbm.xml file to the base hibernate persister, persister="net.sf.hibernate.persister.EntityPersister", but this didn't make
any difference. I expected Hibernate to use this base persister automatically if I didn't specify one in the mapping class.

I do notice the JNDI messages in the output but I'm planning to use Hibernate with Tomcat so I'm trying to follow the Thread Local Session
example in the Using Hibernate with Tomcat and JDNI Page.... www.hibernate.org/114.html

I'm fairly perplexed at this point. I'm sure I'm missing something obvious but haven't seen it in the reference guide or the FAQs. Any help would be greatly appreciated.

Linda :)

hibernate.cfg.xml
Code:
<hibernate-configuration>
  <session-factory>
    <property name="connection.url">
      jdbc:mysql://localhost/review
    </property>
    <property name="connection.driver_class">
      com.mysql.jdbc.Driver
    </property>
    <property name="connection.username">
      linda
    </property>
    <property name="connection.password">
      mysql
    </property>
    <property name="dialect">
      net.sf.hibernate.dialect.MySQLDialect
    </property>
    <property name="show_sql">
      true
    </property>
   
    <!-- mapping files -->
    <mapping resource="../mappings/User.hbm.xml"/>

  </session-factory>
</hibernate-configuration>


User.hbm.xml
Code:
<hibernate-mapping>
  <class name="com.review.data.User"
         table="USER"   
   >
     <id name="userID"   
         column="id"
         type ="string">
         <generator class="native"/>
      </id>
      <property name     ="username"       
                column   ="username"
                type     ="string"
                length   ="25"
                not-null ="true"
                unique   ="true"
      />         
      <property name     ="password"
                column   ="password"
                type     ="string"
                length   ="25"
                not-null ="false"
      />
      <property name     ="firstName"
                column   ="firstName"
                type     ="string"
                length   ="25"
                not-null ="false"
      />
      <property name     ="lastName"
                column   ="lastName"
                type     ="string"
                length   ="25"
                not-null ="false"
      />
      <property name     ="email"
                column   ="email"
                type     ="string"
                length   ="128"
                not-null ="false"
      />
      <property name     ="phone"
                column   ="phone"
                type     ="string"
                length   ="15"
                not-null ="false"
      />
      <property name     ="loggedIn"
                column   ="loggedIn"
                type     ="java.lang.Boolean"
                not-null ="false"
      />
  </class>
</hibernate-mapping>


UserManager class
Code:
    public User retrieveUser(String userName)
    {
       Transaction trx = null;
       User user = null;
        try
        {
            //returns a Hibernate session object. See Using hibernate with
            //Tomcat and JDNI Page.... www.hibernate.org/114/html
            Session dbSession = HiberUtil.currentSession();
            user = (User) dbSession.load(User.class, new Long(1));
           
             return user;
        } catch (ClassCastException cce)
        {
            cce.printStackTrace();
        } catch (HibernateException e)
        {
            e.printStackTrace();
        }
        return null;
    }


Console
Code:
Sep 24, 2003 12:41:29 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: Hibernate 2.0.3
Sep 24, 2003 12:41:29 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: loaded properties from resource hibernate.properties: {hibernate.connection.username=linda, hibernate.connection.password=mysql, hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect, hibernate.show_sql=true, hibernate.connection.url=jdbc:mysql://localhost/review, hibernate.connection.driver_class=org.gjt.mm.mysql.Driver}
Sep 24, 2003 12:41:29 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
Sep 24, 2003 12:41:29 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: JVM proxy support: true
Sep 24, 2003 12:41:29 PM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing one-to-many association mappings
Sep 24, 2003 12:41:29 PM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing foreign key constraints
Sep 24, 2003 12:41:31 PM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
Sep 24, 2003 12:41:31 PM net.sf.hibernate.dialect.Dialect <init>
INFO: Using dialect: net.sf.hibernate.dialect.MySQLDialect
Sep 24, 2003 12:41:31 PM net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 20
Sep 24, 2003 12:41:31 PM net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: org.gjt.mm.mysql.Driver at URL: jdbc:mysql://localhost/review
Sep 24, 2003 12:41:31 PM net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=linda, password=mysql}
Sep 24, 2003 12:41:31 PM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: Use outer join fetching: true
Sep 24, 2003 12:41:32 PM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: Use scrollable result sets: true
Sep 24, 2003 12:41:32 PM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: JDBC 2 max batch size: 15
Sep 24, 2003 12:41:32 PM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: echoing all SQL to stdout
Sep 24, 2003 12:41:32 PM net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: no JDNI name configured
Sep 24, 2003 12:41:32 PM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: Query language substitutions: {}
trying to load User1
net.sf.hibernate.MappingException: No persister for: com.review.data.User
   at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryImpl.java:420)
   at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2302)
   at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1754)
   at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1688)
   at com.review.data.UserManager.retrieveUser(UserManager.java:67)
   at com.review.data.UserManager.getUser(UserManager.java:50)
   at com.review.test.TestUser.testUserExists(TestUser.java:49)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:324)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:392)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:276)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:167)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2003 4:44 pm 
Beginner
Beginner

Joined: Wed Sep 10, 2003 5:34 pm
Posts: 36
Location: New York, NY
Hi Linda,

I'm guessing that the path you specified in your Hibernate.cfg.xml file for the User.hbm.xml is not working. Best practices in this regard are to copy your hbm.xml files into your classpath somewhere (I usually put them at the root of the classes dir in my build directory). Then just refer to them in your config without a path like:

Code:
    <!-- mapping files -->
    <mapping resource="User.hbm.xml"/>


Hibernate will load them as a resource from the classpath.

Hope that helps,

Matt[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2003 5:06 pm 
Newbie

Joined: Wed Sep 24, 2003 3:30 pm
Posts: 7
I tried that & it didn't make any difference.
The reason I didn't put them in the classpath is I'm using Eclipse and when I make a change to the project properties it very thoughtfully cleans out the classpath and recompile the project. Which deletes all my mapping & properties files. :( Took me a while to figure that one out.

But anyway, I did make the change as you suggested & I'm still getting the same error:
Code:
Sep 24, 2003 2:02:45 PM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: Query language substitutions: {}
trying to load User1
net.sf.hibernate.MappingException: No persister for: com.review.data.User
   at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryImpl.java:420)
   at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2302)
   at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1754)
   at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1688)
   at com.review.data.UserManager.retrieveUser(UserManager.java:68)
   at com.review.data.UserManager.getUser(UserManager.java:50)
   at com.review.test.TestUser.testUserExists(TestUser.java:49)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:324)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2003 5:26 pm 
Newbie

Joined: Wed Sep 24, 2003 3:30 pm
Posts: 7
My apologies, you were definitely on the right track.

I added code to manually add the mappings and I was able to connect. I still have some problems with the mapping file but at least hibernate going to the DB. :)

Any idea why the mappings defined in the hibernage.cfg.xml file aren't loaded automatically??

Code:
public static final String USER_MAP    = "User.hbm.xml";

public static Session currentSession() throws HibernateException
   {
      Session s = (Session) session.get();
      if(s==null)
      {
         //Don't get from JNDI, use a static SessionFactory
         if(sessionFactory == null)
         {         
            // use default location for configuration file:
            //   WEB-INF/classes/hibernate.cfg.xml
            config = new Configuration();
            InputStream userMap =    ClassLoader.getSystemResourceAsStream(USER_MAP);
            config.addInputStream(userMap);
            sessionFactory = config.buildSessionFactory();            
         }
         s = sessionFactory.openSession();
         session.set(s);   
      }
      return s;
   }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2003 5:56 pm 
Beginner
Beginner

Joined: Wed Sep 10, 2003 5:34 pm
Posts: 36
Location: New York, NY
How about this, add the directory that contains your hbm.xml files to the classpath, so then Eclipse won't erase them when you rebuild. I definately don't specify a path to the mapping file in my cfg.xml, I just put them in the classpath.

Matt


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.