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)