Hello,
I am testing the Hibernate3.1-beta2 annotations.
Hibernate-3.1-beta2 + hibernate-annotations-3.1-beta4.jar
in Eclipse 3.1.
But when I create a simple JUnit Testcase, it raised the following exception:
Code:
2005-9-5 15:54:04 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.1 beta 2
2005-9-5 15:54:04 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
2005-9-5 15:54:04 org.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
2005-9-5 15:54:04 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
2005-9-5 15:54:04 org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
2005-9-5 15:54:04 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
2005-9-5 15:54:04 org.hibernate.cfg.AnnotationConfiguration addPackage
INFO: Mapping package mo.eric.sinoalbum.bean
2005-9-5 15:54:05 mo.eric.sinoalbum.util.HibernateUtil <clinit>
SEVERE: Building SessionFactory failed.
java.lang.NoSuchMethodError: org.hibernate.cfg.Mappings.<init>(Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/List;Ljava/util/List;Lorg/hibernate/cfg/NamingStrategy;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;)V
at org.hibernate.cfg.ExtendedMappings.<init>(ExtendedMappings.java:48)
at org.hibernate.cfg.AnnotationConfiguration.createExtendedMappings(AnnotationConfiguration.java:131)
at org.hibernate.cfg.AnnotationConfiguration.addPackage(AnnotationConfiguration.java:121)
at org.hibernate.cfg.AnnotationConfiguration.parseMappingElement(AnnotationConfiguration.java:316)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1336)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1318)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1294)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1214)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1200)
at mo.eric.sinoalbum.util.HibernateUtil.<clinit>(HibernateUtil.java:50)
at mo.eric.sinoalbum.test.CategoryDAOTest.testInsert(CategoryDAOTest.java:59)
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:585)
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 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:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
The following is my configuration and codes:
Code:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Settings for a remote MySQL database. -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/sinoalbum</property>
<property name="connection.username">sinoalbum</property>
<property name="connection.password">123456</property>
<!-- Use the C3P0 connection pool. -->
<property name="c3p0.min_size">1</property>
<property name="c3p0.max_size">5</property>
<property name="c3p0.timeout">1800</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="cache.use_query_cache">false</property>
<property name="cache.use_minimal_puts">false</property>
<property name="max_fetch_depth">3</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create-drop</property>
<mapping package="mo.eric.sinoalbum.bean"/>
<mapping class="mo.eric.sinoalbum.bean.AbstractBaseObject"/>
<mapping class="mo.eric.sinoalbum.bean.Category"/>
</session-factory>
</hibernate-configuration>
Code:
/**
* CategoryDAOTest.java
*
* Copyright (C) 2005 DSE. All right Reserved.
*
* This software is the confidential and proprietary information of DSE.
*/
package mo.eric.sinoalbum.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import mo.eric.sinoalbum.bean.Category;
import mo.eric.sinoalbum.util.HibernateUtil;
import mo.eric.sinoalbum.util.UUID;
import junit.framework.TestCase;
/**
* CategoryDAOTest.java
*
* @author Chao Hoi Ka, Eric (HKC)
*
*/
public class CategoryDAOTest extends TestCase {
public static void main(String[] args) {
junit.textui.TestRunner.run(CategoryDAOTest.class);
}
/**
* Constructor for CategoryDAOTest.
* @param name
*/
public CategoryDAOTest(String name) {
super(name);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}
public void testInsert() throws Exception {
Session ses = null;
Transaction tx = null;
try {
ses = HibernateUtil.getSession();
tx = ses.beginTransaction();
Category cat = new Category();
cat.setId(UUID.getId());
cat.setName("Book");
ses.save(cat);
tx.commit();
} catch(Exception e) {
tx.rollback();
ses.close();
}
}
}
Code:
/**
* HibernateUtil.java
*
* Copyright (C) 2005 DSE. All right Reserved.
*
* This software is the confidential and proprietary information of DSE.
*/
package mo.eric.sinoalbum.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
* HibernateUtil.java
*
* @author Chao Hoi Ka, Eric (HKC)
*
*/
public class HibernateUtil {
/**
*
*/
public HibernateUtil() {
super();
// TODO Auto-generated constructor stub
}
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static Configuration configuration;
private static SessionFactory sessionFactory;
private static final ThreadLocal threadSession = new ThreadLocal();
private static final ThreadLocal threadTransaction = new ThreadLocal();
private static final ThreadLocal threadInterceptor = new ThreadLocal();
// Create the initial SessionFactory from the default configuration files
static {
try {
configuration = new AnnotationConfiguration();
// configuration = new Configuration();
sessionFactory = configuration.configure().buildSessionFactory();
// We could also let Hibernate bind it to JNDI:
// configuration.configure().buildSessionFactory()
} catch (Throwable ex) {
// We have to catch Throwable, otherwise we will miss
// NoClassDefFoundError and other subclasses of Error
log.error("Building SessionFactory failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
/**
* Returns the SessionFactory used for this static class.
*
* @return SessionFactory
*/
public static SessionFactory getSessionFactory() {
/* Instead of a static variable, use JNDI:
SessionFactory sessions = null;
try {
Context ctx = new InitialContext();
String jndiName = "java:hibernate/HibernateFactory";
sessions = (SessionFactory)ctx.lookup(jndiName);
} catch (NamingException ex) {
throw new InfrastructureException(ex);
}
return sessions;
*/
return sessionFactory;
}
/**
* Returns the original Hibernate configuration.
*
* @return Configuration
*/
public static Configuration getConfiguration() {
return configuration;
}
/**
* Rebuild the SessionFactory with the static Configuration.
*
*/
public static void rebuildSessionFactory() throws Exception {
synchronized(sessionFactory) {
try {
sessionFactory = getConfiguration().buildSessionFactory();
} catch (Exception ex) {
log.error(ex);
throw new Exception(ex);
}
}
}
/**
* Rebuild the SessionFactory with the given Hibernate Configuration.
*
* @param cfg
*/
public static void rebuildSessionFactory(Configuration cfg)
throws Exception {
synchronized(sessionFactory) {
try {
sessionFactory = cfg.buildSessionFactory();
configuration = cfg;
} catch (Exception ex) {
log.error(ex);
throw new Exception(ex);
}
}
}
/**
* Retrieves the current Session local to the thread.
* <p/>
* If no Session is open, opens a new Session for the running thread.
*
* @return Session
*/
public static Session getSession()
throws Exception {
Session s = (Session) threadSession.get();
try {
if (s == null) {
log.debug("Opening new Session for this thread.");
if (getInterceptor() != null) {
log.debug("Using interceptor: " + getInterceptor().getClass());
s = getSessionFactory().openSession(getInterceptor());
} else {
s = getSessionFactory().openSession();
}
threadSession.set(s);
}
} catch (HibernateException ex) {
log.error(ex);
throw new Exception(ex);
}
return s;
}
/**
* Closes the Session local to the thread.
*/
public static void closeSession()
throws Exception {
try {
Session s = (Session) threadSession.get();
threadSession.set(null);
if (s != null && s.isOpen()) {
log.debug("Closing Session of this thread.");
s.close();
}
} catch (HibernateException ex) {
throw new Exception(ex);
}
}
/**
* Start a new database transaction.
*/
public static void beginTransaction()
throws Exception {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx == null) {
log.debug("Starting new database transaction in this thread.");
tx = getSession().beginTransaction();
threadTransaction.set(tx);
}
} catch (HibernateException ex) {
throw new Exception(ex);
}
}
/**
* Commit the database transaction.
*/
public static void commitTransaction()
throws Exception {
Transaction tx = (Transaction) threadTransaction.get();
try {
if ( tx != null && !tx.wasCommitted()
&& !tx.wasRolledBack() ) {
log.debug("Committing database transaction of this thread.");
tx.commit();
}
threadTransaction.set(null);
} catch (HibernateException ex) {
rollbackTransaction();
throw new Exception(ex);
}
}
/**
* Rollback the database transaction.
*/
public static void rollbackTransaction()
throws Exception {
Transaction tx = (Transaction) threadTransaction.get();
try {
threadTransaction.set(null);
if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ) {
log.debug("Tyring to rollback database transaction of this thread.");
tx.rollback();
}
} catch (HibernateException ex) {
throw new Exception(ex);
} finally {
closeSession();
}
}
/**
* Reconnects a Hibernate Session to the current Thread.
*
* @param session The Hibernate Session to be reconnected.
*/
public static void reconnect(Session session)
throws Exception {
try {
session.reconnect();
threadSession.set(session);
} catch (HibernateException ex) {
throw new Exception(ex);
}
}
/**
* Disconnect and return Session from current Thread.
*
* @return Session the disconnected Session
*/
public static Session disconnectSession()
throws Exception {
Session session = getSession();
try {
threadSession.set(null);
if (session.isConnected() && session.isOpen())
session.disconnect();
} catch (HibernateException ex) {
throw new Exception(ex);
}
return session;
}
/**
* Register a Hibernate interceptor with the current thread.
* <p>
* Every Session opened is opened with this interceptor after
* registration. Has no effect if the current Session of the
* thread is already open, effective on next close()/getSession().
*/
public static void registerInterceptor(Interceptor interceptor) {
threadInterceptor.set(interceptor);
}
private static Interceptor getInterceptor() {
Interceptor interceptor =
(Interceptor) threadInterceptor.get();
return interceptor;
}
}
Code:
/**
* AbstractBaseObject.java
*
* Copyright (C) 2005 DSE. All right Reserved.
*
* This software is the confidential and proprietary information of DSE.
*/
package mo.eric.sinoalbum.bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratorType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
/**
* AbstractBaseObject.java
*
* @author Chao Hoi Ka, Eric (HKC)
*
*/
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name="BASE_OBJECT")
public abstract class AbstractBaseObject {
protected String id;
protected String name;
/**
*
*/
public AbstractBaseObject() {
super();
// TODO Auto-generated constructor stub
}
public abstract String getIndexText();
/**
* @return Returns the id.
*/
@Id(generate=GeneratorType.NONE)
@Column(updatable = false, name = "ID", nullable = false, length = 50)
public String getId() {
return this.id;
}
/**
* @param id The id to set.
*/
public void setId(String id) {
this.id = id;
}
/**
* @return Returns the name.
*/
@Column
public String getName() {
return this.name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
}
Code:
/**
* Category.java
*
* Copyright (C) 2005 DSE. All right Reserved.
*
* This software is the confidential and proprietary information of DSE.
*/
package mo.eric.sinoalbum.bean;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
* Category.java
*
* @author Chao Hoi Ka, Eric (HKC)
*
*/
@Entity
@Table(name = "CATEGORY")
public class Category extends AbstractBaseObject {
/**
*
*/
public Category() {
super();
// TODO Auto-generated constructor stub
}
public String getIndexText() {
return super.name;
}
}