Dear folks,
I was trying to display data on a jsp page. But the building of SessionFactories was always failed. My application needs to access two different database as three different roles. So there are three hibernate configuration files. It seems that the JBoss did find the datasource because the mappings were partially done when the SessionFactories failed to be built.
Hibernate version: 3.2 CR 2
JBoss app. server version: 4.0.4 CR 2
Mapping documents:
All the mappings are correct (Tested without any app server, using mere JDBC). Here is one of my three hibernate configuration files. The other two are very similar.
Code:
<hibernate-configuration>
<!-- Die Konfiguration der SessionFactory fuer die Umfrage DB -->
<session-factory name="java:/hibernate/UmfrageSessionFactory">
<!-- Der JNDI Name der datasource für die Umfragen DB ist
der maxdb-umfrage-ds.xml von JBoss zu entnehmen -->
<property name="hibernate.connection.datasource">
java:UMFRAGEDS
</property>
<!-- SQL Dialekt -->
<property name="hibernate.dialect">
org.hibernate.dialect.SAPDBDialect
</property>
<property name="max_fetch_depth">3</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<!-- JBoss TransactionManager Factory Klasse -->
<property name="hibernate.transaction.factory_class">
org.hibernate.transaction.CMTTransactionFactory
</property>
<property
name="hibernate.transaction.flush_before_completion">
true
</property>
<property name="hibernate.transaction.auto_close_session">
true
</property>
<!-- JBoss TransactionManager Lookup Klasse -->
<property name="hibernate.transaction.manager_lookup_class">
org.hibernate.transaction.JBossTransactionManagerLookup
</property>
<property name="hibernate.connection.release_mode">
auto
</property>
<!-- Die Pfade zu den Mapping Dateien -->
<mapping resource="..." />
...
</session-factory>
</hibernate-configuration>
UmfrageDAO.javaCode:
public class UmfrageDAO extends GenericUmfrageDAO<Umfrage, Integer> {
protected UmfrageDAO() {
super(Umfrage.class);
}
public UmfrageDAO(Session session) {
super(Umfrage.class, session);
}
@Override
protected void setSession(Session s) {
session = s;
}
}
HibernateUtil.javaCode:
public class HibernateUtil {
public static final HibernateUtil INSTANCE = new HibernateUtil();
private static final String HIBERNATE_UMFRAGE_CFG = "hibernate_umfrage.cfg.xml";
private static final String HIBERNATE_PERSONAL_CFG = "hibernate_personal.cfg.xml";
private static final String HIBERNATE_STUDINFO_CFG = "hibernate_studinfo.cfg.xml";
private static SessionFactory umfrageSessionFactory;
private static SessionFactory personalSessionFactory;
private static SessionFactory studinfoSessionFactory;
static {
try {
umfrageSessionFactory = new Configuration().configure(
HIBERNATE_UMFRAGE_CFG).buildSessionFactory();
personalSessionFactory = new Configuration().configure(
HIBERNATE_PERSONAL_CFG).buildSessionFactory();
studinfoSessionFactory = new Configuration().configure(
HIBERNATE_STUDINFO_CFG).buildSessionFactory();
} catch (Throwable ex) {
log.error("Building SessionFactories failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getUmfrageSessionFactory(){
return umfrageSessionFactory;
}
public static SessionFactory getPersonalSessionFactory(){
return personalSessionFactory;
}
public static SessionFactory getStudinfoSessionFactory(){
return studinfoSessionFactory;
}
public static Session getUmfrageSession() throws InfrastructureException {
log.info("entering getUmfrageSession()");
try {
Session session = umfrageSessionFactory.getCurrentSession();
log.info("leaving getUmfrageSession()");
return session;
} catch (HibernateException he) {
log.error("Retrieving current Umfrage session failed.", he);
throw new InfrastructureException(he);
}
}
public static Session getPersonalSession() throws InfrastructureException {
log.info("entering getPersonalSession()");
try {
Session session = personalSessionFactory.getCurrentSession();
// wird bei read-only Sessions empfohlen, um die Performanz zu
// verbessern
// (siehe Hibernate 3 API Doc)
session.setFlushMode(FlushMode.NEVER);
log.info("leaving getPersonalSession()");
return session;
} catch (HibernateException he) {
log.error("Retrieving current Personal session failed.", he);
throw new InfrastructureException(he);
}
}
public static Session getStudinfoSession() throws InfrastructureException {
log.info("entering getStudinfoSession()");
try {
Session session = studinfoSessionFactory.getCurrentSession();
// wird bei read-only Sessions empfohlen, um die Performanz zu
// verbessern
// (siehe Hibernate 3 API Doc)
session.setFlushMode(FlushMode.NEVER);
log.info("leaving getStudinfoSession()");
return session;
} catch (HibernateException he) {
log.error("Retrieving current Studinfo session failed.", he);
throw new InfrastructureException(he);
}
}
}
UmfrageDAOFactory.javaCode:
public class UmfrageDAOFactory {
public static final UmfrageDAOFactory INSTANCE = new UmfrageDAOFactory();
public DurchfuehrungDAO getDurchfuehrungDAO() {
return new DurchfuehrungDAO(getCurrentSession());
}
public UmfrageDAO getUmfrageDAO() {
return new UmfrageDAO(getCurrentSession());
}
...
protected Session getCurrentSession() {
return HibernateUtil.getUmfrageSessionFactory().getCurrentSession();
}
private UmfrageDAOFactory() {
}
}
UmfrageTableData.javathis is an extremely rudimentary class for testing the JSF UI.
Code:
public class UmfrageTableData {
private static UmfrageDAO uDAO = UmfrageDAOFactory.INSTANCE.getUmfrageDAO();
private static final List<Umfrage> umfragen = uDAO.findAll();
public List<Umfrage> getAllUmfragen() {
return umfragen;
}
}
index.jspCode:
<html>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<f:view>
<body>
<h:form>
<h:dataTable value="#{umfrageTableData.umfragen}" var="umfrage">
<h:column>
<h:outputText value="#{umfrage.id}" />
<f:verbatim>,</f:verbatim>
</h:column>
<h:column>
<h:outputText value="#{umfrage.umfrTyp}" />
</h:column>
</h:dataTable>
</h:form>
</body>
</f:view>
</html>
Full stack trace of any exception that occurs:00:24:05,181 INFO [SessionFactoryImpl] building session factory
00:24:05,211 WARN [ConfigurationFactory] No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/E:/Programme/jboss-4.0.4.CR2/server/default/tmp/deploy/tmp27542umfrage-exp.war/WEB-INF/lib/ehcache-1.2.jar!/ehcache-failsafe.xml
00:24:07,244 INFO [SessionFactoryObjectFactory] Factory name: java:/hibernate/UmfrageSessionFactory
00:24:07,244 INFO [NamingHelper] JNDI InitialContext properties:{}
00:24:07,244 INFO [NamingHelper] Creating subcontext: hibernate
00:24:07,244 INFO [SessionFactoryObjectFactory] Bound factory to JNDI name: java:/hibernate/UmfrageSessionFactory
00:24:07,244 WARN [SessionFactoryObjectFactory] InitialContext did not implement EventContext
00:24:07,244 INFO [NamingHelper] JNDI InitialContext properties:{}
00:24:07,264 INFO [STDOUT] 00:24:07,254 ERROR [HibernateUtil] Building SessionFactories failed.
java.lang.ClassCastException: org.jboss.tm.TxManager
at org.hibernate.transaction.JNDITransactionManagerLookup.getTransactionManager(JNDITransactionManagerLookup.java:23)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:322)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1213)
at de.fhzw.portal.umfragesystem.model.utils.HibernateUtil.<clinit>(HibernateUtil.java:71)
at de.fhzw.portal.umfragesystem.model.dao.umfrage.UmfrageDAOFactory.getCurrentSession(UmfrageDAOFactory.java:43)
at de.fhzw.portal.umfragesystem.model.dao.umfrage.UmfrageDAOFactory.getUmfrageDAO(UmfrageDAOFactory.java:38)
at test.UmfrageTableData.<clinit>(UmfrageTableData.java:12)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
....
I've tracked to the source code in the Configuration.java. It went wrong in this method:
Code:
/**
* Instantiate a new <tt>SessionFactory</tt>, using the properties and
* mappings in this configuration. The <tt>SessionFactory</tt> will be
* immutable, so changes made to the <tt>Configuration</tt> after
* building the <tt>SessionFactory</tt> will not affect it.
*
* @return a new factory for <tt>Session</tt>s
* @see org.hibernate.SessionFactory
*/
public SessionFactory buildSessionFactory() throws HibernateException {
log.debug( "Preparing to build session factory with filters : " + filterDefinitions );
secondPassCompile();
validate();
Environment.verifyProperties( properties );
Properties copy = new Properties();
copy.putAll( properties );
PropertiesHelper.resolvePlaceHolders( copy );
Settings settings = buildSettings( copy );
return new SessionFactoryImpl(
this,
mapping,
settings,
getInitializedEventListeners()
);
}
and the line 1213 is exactly here: return new SessionFactoryImpl....
Name and version of the database you are using:
SAP DB. I guess it's irrelevant here. Since all the DB stuff "an sich" were tested successfully under the JDBC version of the hibernate configuration
Debug level Hibernate log excerpt:
INFO I guess?
Any help/hint/enlightenment would be highly appreciated! It's 00:30 am now and I'm still debugging in my office....
Regards,
Ellen Zhao