I have a simple heirarcy that won't work. It is pretty much the Category of caveat emptor:
It prints the heirarchy right, so my getters and setters work.
I get the category, but it only contains one child, it self.
Eternal loop.
/*
* Created on Apr 21, 2005
*/
package se.snigel.testcase;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.Comparator;
//*/
/**
* Basic Hibernate helper class, handles SessionFactory, Session and
* Transaction.
* <p/>
* Uses a static initializer for the initial SessionFactory creation and holds
* Session and Transactions in thread local variables. All exceptions are
* wrapped in an unchecked RuntimeException.
*
* @author
[email protected] * @author Karl Wettin <
[email protected]>
*/
public class HibernateUtil {
/**
* The MyEchelon instance place holder.
*/
private static Long myEchelonPrimaryKey;
public static String jdbcDriver = "org.hsqldb.jdbcDriver";
public static String jdbcURL = "jdbc:hsqldb:hsql://localhost/xdb";
public static String jdbcUser = "sa";
public static String jdbcPassword = "";
public static String hibernateSQLDialect = "org.hibernate.dialect.HSQLDialect";
/*
public static String jdbcDriver = "com.mysql.jdbc.Driver";
public static String jdbcURL = "jdbc:mysql://localhost:3306/myechelon?autoReconnect=true";
public static String jdbcUser = "root";
public static String jdbcPassword = "";
public static String hibernateSQLDialect = "org.hibernate.dialect.MySQLDialect";
*/
private static Logger log = LogManager.getLogger(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 Configuration()
.addClass(Category.class);
try {
Class.forName(jdbcDriver).newInstance();
} catch (Exception ex) {
ex.printStackTrace();
}
configuration.setProperty("hibernate.connection.url", jdbcURL);
if (jdbcUser != null) {
configuration.setProperty("hibernate.connection.user", jdbcUser);
}
if (jdbcPassword != null) {
configuration.setProperty("hibernate.connection.password", jdbcPassword);
}
if (hibernateSQLDialect != null) {
configuration.setProperty("hibernate.dialect", hibernateSQLDialect);
}
configuration.setProperty("hibernate.show_sql", "false");
configuration.setProperty("hibernate.connection.autocommit", "false");
configuration.setProperty("hibernate.query.substitutions", "true 1, false 0, yes 'Y', no 'N'");
configuration.setProperty("hibernate.connection.pool_size", "1");
configuration.setProperty("hibernate.proxool.pool_alias", "pool1");
configuration.setProperty("hibernate.jdbc.batch_size", "0");
configuration.setProperty("hibernate.jdbc.batch_versioned_data", "true");
configuration.setProperty("hibernate.jdbc.use_streams_for_binary", "true");
configuration.setProperty("hibernate.max_fetch_depth", "1");
configuration.setProperty("hibernate.cache.region_prefix", "hibernate.test");
configuration.setProperty("hibernate.cache.use_query_cache", "true");
configuration.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");
configuration.setProperty("hibernate.proxool.pool_alias", "pool1");
configuration.setProperty("hibernate.proxool.pool_alias", "pool1");
sessionFactory = configuration.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 RuntimeException(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 RuntimeException {
synchronized (sessionFactory) {
try {
sessionFactory = getConfiguration().buildSessionFactory();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
/**
* Rebuild the SessionFactory with the given Hibernate Configuration.
*
* @param cfg
*/
public static void rebuildSessionFactory(Configuration cfg) throws RuntimeException {
synchronized (sessionFactory) {
try {
sessionFactory = cfg.buildSessionFactory();
configuration = cfg;
} catch (Exception ex) {
throw new RuntimeException(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 RuntimeException {
Session s = (Session) threadSession.get();
try {
if (s == null) {
if (log.isDebugEnabled()) {
log.debug("Opening new Session for this thread.");
}
if (getInterceptor() != null) {
if (log.isDebugEnabled()) {
log.debug("Using interceptor: " + getInterceptor().getClass());
}
s = getSessionFactory().openSession(getInterceptor());
} else {
s = getSessionFactory().openSession();
}
threadSession.set(s);
}
} catch (HibernateException ex) {
throw new RuntimeException(ex);
}
return s;
}
/**
* Closes the Session local to the thread.
*/
public static void closeSession() throws RuntimeException {
try {
Session s = (Session) threadSession.get();
threadSession.set(null);
if (s != null && s.isOpen()) {
if (log.isDebugEnabled()) {
log.debug("Closing Session of this thread.");
}
s.close();
}
} catch (HibernateException ex) {
throw new RuntimeException(ex);
}
}
/**
* Start a new database transaction.
*/
public static void beginTransaction() throws RuntimeException {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx == null) {
if (log.isDebugEnabled()) {
log.debug("Starting new database transaction in this thread.");
}
tx = getSession().beginTransaction();
threadTransaction.set(tx);
}
} catch (HibernateException ex) {
throw new RuntimeException(ex);
}
}
/**
* Commit the database transaction.
*/
public static void commitTransaction() throws RuntimeException {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.commit();
if (log.isDebugEnabled()) {
log.debug("Committing database transaction of this thread.");
}
}
threadTransaction.set(null);
} catch (HibernateException ex) {
rollbackTransaction();
throw new RuntimeException(ex);
}
}
/**
* Commit the database transaction.
*/
public static void rollbackTransaction() throws RuntimeException {
Transaction tx = (Transaction) threadTransaction.get();
try {
threadTransaction.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
if (log.isDebugEnabled()) {
log.debug("Tyring to rollback database transaction of this thread.");
}
tx.rollback();
}
} catch (HibernateException ex) {
throw new RuntimeException(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 RuntimeException {
try {
session.reconnect();
threadSession.set(session);
} catch (HibernateException ex) {
throw new RuntimeException(ex);
}
}
/**
* Disconnect and return Session from current Thread.
*
* @return Session the disconnected Session
*/
public static Session disconnectSession() throws RuntimeException {
Session session = getSession();
try {
threadSession.set(null);
if (session.isConnected() && session.isOpen())
session.disconnect();
} catch (HibernateException ex) {
throw new RuntimeException(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;
}
}