Hibernate version:2.1.2
Hello forum Members
I have typed prepared the following program to try an approach for linking 2 tables.
Table User has 2 columns Id and Key Table Name has 3 columns Id, firstName and lastName.
User.Id is primary key User.Key is integer with auto_increment.
The objective is :
When the value in the User.Id is saved, the Key is auto incremented. The Name table is not being used at present.
When trying to run the launcher program -> example, I get an error stating "No Persister for pac.User"
Database used is MySQL. This is a stand-alone application.
I will appreciate any pointer regarding the error/changes required in the code.
Thanks.
regards, Amitabh. =============================================================================================
The following is content of hibernte.cfg.xml.
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<!-- <session-factory name="java:comp/env/hibernate/SessionFactory"> --> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/example</property> <property name="hibernate.connection.username">user</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.connection.pool_size">4</property> <property name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <!-- Mapping files --> <mapping resource="pac/User.hbm.xml"/> <mapping resource="pac/Name.hbm.xml"/> </session-factory>
</hibernate-configuration>
The Java Source code is :
=======
pac.User.java -- package pac; public class User{ private String userId = null; private String key = null;
public User(){ }
public String getId(){ return userId; }
public void setId(String userId){ this.userId = userId; }
public String getKey(){ return key; } } ======
====== pac.Name.java --- package pac; public class Name{ private String keyId = null; private String firstName = null; private String lastName = null;
public Name(){ }
public String getKeyId(){ return keyId; }
public void setKeyId(String keyId){ this.keyId = keyId; }
public String getFirstName(){ return firstName; }
public void setFristName(String firstName){ this.firstName = firstName; }
public String getLastName(){ return lastName; }
public void setLastName(String lastName){ this.lastName = lastName; } } ======
The content of hbm files
====== pac/User.hbm.xml ---- <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping package="pac">
<class name="User" table="USER"> <id name="id" column="Id"> <generator class="native"/> </id> <property name="key" column="Key"/> </class>
</hibernate-mapping>
======
====== pac/Name.hbm.xml ---- <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping package="pac">
<class name="Name" table="name"> <id name="id"> <generator class="native"/> </id> <property name="firstName"/> <property name="lastName"/> <one-to-one name="user" class="User" property-ref="key"/> </class>
</hibernate-mapping> ======
The Source code of the Launcher class is given below.
====== example.java ---- import net.sf.hibernate.*; import net.sf.hibernate.cfg.*; import java.io.*; import java.sql.*;
import pac.User; import pac.Name;
public class example{ private static String firstName = null; private static String lastName = null; private static BufferedReader reader = null;
private static SessionFactory sessionFactory = null; Connection connection=null;
static{ try{ Class.forName("com.mysql.jdbc.Driver"); sessionFactory = new Configuration().buildSessionFactory(); }catch(Exception exception){ exception.printStackTrace(); } }
public static final ThreadLocal session = new ThreadLocal();
private void openJdbcConnection() throws SQLException{ connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/example", "krishan","krishan"); } private Session openSession() throws HibernateException { Session open = (Session) session.get(); if(open == null){ open = sessionFactory.openSession(); session.set(open); } return open; }
private void closeSession() throws HibernateException { Session close = (Session) session.get(); session.set(null); if(close != null){ close.close(); } }
private void persist() throws HibernateException{ Session session = openSession(); User user = new User(); session.save(user); session.flush(); closeSession(); } public static void main(String[] args) throws Exception{ new example().persist(); reader = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the First Name : "); firstName = reader.readLine(); System.out.print("Enter the Last Name : "); lastName = reader.readLine(); } } ======
The error message
===========
20:51:25,044 INFO Environment:462 - Hibernate 2.1.2 20:51:25,054 INFO Environment:491 - hibernate.properties not found 20:51:25,064 INFO Environment:519 - using CGLIB reflection optimizer 20:51:25,084 INFO Configuration:595 - processing one-to-many association mappings 20:51:25,084 INFO Configuration:604 - processing one-to-one association property references 20:51:25,084 INFO Configuration:629 - processing foreign key constraints 20:51:25,144 WARN SettingsFactory:50 - No dialect set - using GenericDialect: The dialect was not set. Set the property hibernate.dialect. 20:51:25,154 INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.GenericDialect 20:51:25,154 INFO SettingsFactory:62 - Use outer join fetching: false 20:51:25,164 WARN UserSuppliedConnectionProvider:25 - No connection properties specified - the user must supply JDBC connections 20:51:25,184 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended) 20:51:25,184 INFO SettingsFactory:102 - Use scrollable result sets: false 20:51:25,184 INFO SettingsFactory:105 - Use JDBC3 getGeneratedKeys(): false 20:51:25,184 INFO SettingsFactory:108 - Optimize cache for minimal puts: false 20:51:25,194 INFO SettingsFactory:117 - Query language substitutions: {} 20:51:25,194 INFO SettingsFactory:128 - cache provider: net.sf.ehcache.hibernate.Provider 20:51:25,254 INFO Configuration:1080 - instantiating and configuring caches 20:51:26,666 INFO SessionFactoryImpl:119 - building session factory 20:51:27,097 INFO SessionFactoryObjectFactory:82 - no JNDI name configured Exception in thread "main" net.sf.hibernate.MappingException: No persister for: pac.User at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryImpl.java:344) at net.sf.hibernate.impl.SessionImpl.getClassPersister(SessionImpl.java:2656) at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2663)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:745) at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:720) at example.persist(example.java:54) at example.main(example.java:60) ==========
|