Hi
I am struggling to get Hibernate Annotations to work. I have not been able to get it working via external class mapping in the hibernate.cfg.xml file (got an unknown entity exception with the first entity class that was encountered) and now I am getting this exception when I map the entity classes in the SessionFactory method.
I enclose the following:
1. the "entity" class declaration as well as the save method
2. util SessionFactory class that declares package and entity classes
3. the run time error
4. hibernate.cfg.xml file
Please can someone guide a very frustrated new comer to Annotations(I have got the same example to work with the mapping data contained in an xml file)
Fernando
##### hibernate version 3.1.2
##### hibernate Annotations version 3.1.8
##### data base being used HSQLDB
###### class declaration
package PeacTest_Annotations;
/** @author Fernando de Nobrega 22 January 2006
*/
//hibernate
import java.io.Serializable;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.hql.*;
import javax.persistence.*;
import java.util.*;
import java.util.Date;
import java.io.*;
/**
*
*/
@Entity()
@Table(name="passwordObj")
public class Password {
///////////////////////////////////////
// attributes
@Id @GeneratedValue
private Long id;
.
.
.
.
.
####### save method of above class
public Long savePasswordObject(Password passwordObject) {
Session sess = null;
Transaction tx = null;
try {
sess = HibernateUtil.getSessionFactory().openSession();
tx = sess.beginTransaction();
setPasswordObjIntoMemory(passwordObject);
sess.saveOrUpdate(passwordObject);
sess.flush();
tx.commit();
} catch (Exception ex) {
tx.rollback();
ex.printStackTrace();
}
finally {
sess.close();
}
//System.out.println ("IN PASSOWRD Serializabl id = " + id);
return id;
} // end savePasswordObject
package PeacTest_Annotations;
//hibernate
import java.io.Serializable;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.hql.*;
import javax.persistence.*;
import PeacTest_Annotations.*;
######## util SessionFactory class that declares package:
public class HibernateUtil implements Serializable {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new AnnotationConfiguration()
.addPackage("PeacTest_Annotations") //the fully qualified package name
.addAnnotatedClass(Password.class)
.buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("XXXXX FDN XXXInitial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
//public static Session getSession()
// throws HibernateException {
// return sessionFactory.openSession();
//}
}
####### run time error
18:03:48,502 WARN AnnotationBinder:155 - Package not found or wo package-info.java: PeacTest_Annotations
XXXXX FDN XXXInitial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/hibernate/cfg/QuerySecondPass
Exception in thread "main" java.lang.NullPointerException
at PeacTest_Annotations.Password.savePasswordObject(Password.java:710)
at PeacTest_Annotations.Password.main(Password.java:1088)
Process completed.
######### hibernate.cfg.xml file
?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">
<!-- hibernate.cfg.xml file -->
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- For external mapping -->
<!-- <mapping resource="UserAndAccessPackage.hbm.xml"/> -->
<!-- Hibernate Annotation class mapping -->
<mapping package="PeacTest_Annotations"/>
<mapping class="PeacTest_Annotations.Password"/>
<!-- <mapping class="peacBudget."/>
<mapping class="peacBudget."/>
<mapping class="peacBudget."/>
-->
<!--
TO GET HSQL SERVER RUNNING
java -classpath lib/hsqldb.jar org.hsqldb.Server
TO RUN ANT
ant run -Daction=store
-->
</session-factory>
</hibernate-configuration>
|