Archive

Posts Tagged ‘Hibernate’

java.lang.StackOverflowError and Hibernate

30 December 2010 Leave a comment

If you have set up your relationships correctly in your @Entity bean but you are still getting this error, I have found out that by explicitly setting the Query‘s flush mode type to COMMIT you can avoid recursive calls that result in a StackOverflowError

query.setFlushMode(FlushModeType.COMMIT);	// Avoid recursive call and StackOverflowError
Categories: Hibernate Tags: , ,

ClassNotFoundException: org.hibernate.hql.ast.HqlToken

2 December 2010 15 comments

Another exception under WebLogic server. This exception occurs because the default query factory class in persistence.xml is the org.hibernate.hql.ast.ASTQueryTranslatorFactory. As the name suggests this is a classpath issue. You might be wondering why you get it since you have the hibernate jar file (the file that contains the HqlToken class) in your classpath. The reason is that the weblogic.jar contains a version of antlr.jar which is loaded by a classloader, while the hibernate stuff are loaded by another classloader. So when the server runs it’s using the version of antlr bundled with weblogic which cannot see the hibernate classes. There are two solutions to this problem

1) Use a

<property name="hibernate.query.factory_class" value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory"/>

instead of a

<property name="hibernate.query.factory_class" value="org.hibernate.hql.ast.ASTQueryTranslatorFactory"/>

in your persistence.xml. This will solve the problem but you will have additional issues if you use Enums as parameters to queries (like I do) since I found out the the ClassicQueryTranslatorFactory has problems translating Enum data types to parameters (instead it translates them as bytes, unless you pass the actual value itself and not the Enum object).

2)

Add Hibernate’s antlr jar file as first entry in the classpath (in your startWeblogic script of your domain’s bin folder) and force weblogic to use this instead. I copied it into the common/lib folder and used

set CLASSPATH=%WL_HOME%\common\lib\antlr-2.7.6.jar;%SAVE_CLASSPATH%

There is another solution suggested, to add the following

    <prefer-application-packages>
        <package-name>antlr.*</package-name>
    </prefer-application-packages>

in your weblogic-application.xml, but for some reason this did not work for me.

Error loading element EntityTransaction with component name null and component class null

30 November 2010 8 comments

The exception java.lang.RuntimeException: Error loading element EntityTransaction with component name null and component class null is caused by this line

<transaction:entity-transaction entity-manager="#{entityManager}"/>

in the components.xml file. In order to fix the error you have to replace the

   <persistence:managed-persistence-context name="entityManager" auto-create="true" persistence-unit-jndi-name="java:/myManagerFactory"/>

with

   <persistence:entity-manager-factory name="entityManagerFactory" persistence-unit-name="myPersistenceUnit"/>
   <persistence:managed-persistence-context name="entityManager" auto-create="true" entity-manager-factory="#{entityManagerFactory}" />

and remove the

<transaction:entity-transaction entity-manager="#{entityManager}"/>

line.

The myPersistenceUnit is the name of the persistence unit in persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
             version="1.0">
             
   <persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>

      <jta-data-source>myDatasource</jta-data-source>

      <properties>
         <property name="hibernate.connection.provider_class" value="org.hibernate.connection.DatasourceConnectionProvider"/>
	   <property name="hibernate.connection.datasource" value="myDatasource"/>
	   <property name="transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
	   <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
	   <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WeblogicTransactionManagerLookup"/>
         <property name="hibernate.dialect" value="gr.my.company.util.MyAS400Dialect"/>
         <property name="hibernate.hbm2ddl.auto" value="validate"/>
         <property name="hibernate.show_sql" value="true"/>
         <property name="hibernate.format_sql" value="true"/>
         <property name="hibernate.default_schema" value="MYSCHEMA"/>
      </properties>
   </persistence-unit>  
    
</persistence>

No EJBs found in the ejb-jar file ‘MyApplication.jar’. Please ensure the ejb-jar contains EJB declarations via an ejb-jar.xml deployment descriptor or at least one class annotated with the @Stateless, @Stateful or @MessageDriven EJB annotation

30 November 2010 4 comments

Another migration error. In my application.xml I have defined an <ejb> module but my ejb project only contains EJB 3.0 @Entity annotated beans. WebLogic (10.3) doesn’t like this and expects a @Stateless, @Stateful or @MessageDriven annotated bean. It doesn’t find any and throws a

No EJBs found in the ejb-jar file ‘MyApplication.jar’. Please ensure the ejb-jar contains EJB declarations via an ejb-jar.xml deployment descriptor or at least one class annotated with the @Stateless, @Stateful or @MessageDriven EJB annotation

exception. The exact same jar file works fine on JBoss and WebSphere though. Not sure which one is right but I had to add a dummy @Stateless EJB in the jar file order to make it work.

Categories: JBoss, Seam, WAS, WebLogic Tags: , , , ,

java.lang.UnsupportedOperationException: The user must supply a JDBC connection

26 November 2010 5 comments

If you get this with Hibernate it probably means that you have an error, or an incomplete persistence.xml file. I got this while migrating from JBoss to WebLogic server and had forgotten to migrate the configuration from JBoss’ *-ds.xml to persistence.xml.

It happens because of this bit of code (Hibernate’s ConnectionProviderFactory.java)

ConnectionProvider connections;
String providerClass = properties.getProperty(Environment.CONNECTION_PROVIDER);
if ( providerClass!=null ) {
	try {
		log.info("Initializing connection provider: " + providerClass);
		connections = (ConnectionProvider) ReflectHelper.classForName(providerClass).newInstance();
	}
	catch ( Exception e ) {
		log.error( "Could not instantiate connection provider", e );
		throw new HibernateException("Could not instantiate connection provider: " + providerClass);
	}
}
else if ( properties.getProperty(Environment.DATASOURCE)!=null ) {
	connections = new DatasourceConnectionProvider();
}
else if ( properties.getProperty(Environment.URL)!=null ) {
	connections = new DriverManagerConnectionProvider();
}
else {
	connections = new UserSuppliedConnectionProvider();
}

It tries to load several connection providers based on the Environment properties, it can’t find any connection providers (since their corresponding properties are not defined in the persistence.xml) and it just returns the default UserSuppliedConnectionProvider which, alas, it’s getConnection method is the following

public Connection getConnection() {
	throw new UnsupportedOperationException("The user must supply a JDBC connection");
}

After fixing the error, my persistence.xml is the following

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
             version="1.0">
             
   <persistence-unit name="myUnit" transaction-type="JTA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>

      <jta-data-source>myDatasource</jta-data-source>

      <properties>
		<property name="hibernate.connection.provider_class" value="org.hibernate.connection.DatasourceConnectionProvider"/>
	   	<property name="hibernate.connection.datasource" value="myDatasource"/>
	   	<property name="transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
	   	<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
	   	<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WeblogicTransactionManagerLookup"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
        <property name="hibernate.hbm2ddl.auto" value="validate"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.default_schema" value="MYSCHEMA"/>
	 <property name="hibernate.query.factory_class" value="org.hibernate.hql.ast.ASTQueryTranslatorFactory"/>
      </properties>
   </persistence-unit>  
    
</persistence>

Transaction is not active: tx=TransactionImple < ac, BasicAction

5 April 2010 27 comments

This is an exception I was getting while trying to do some hibernate stuff.

Caused by: org.hibernate.exception.GenericJDBCException: Cannot open connection
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
    at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
    at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1573)
    at org.hibernate.loader.Loader.doQuery(Loader.java:696)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    at org.hibernate.loader.Loader.doList(Loader.java:2228)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
    at org.hibernate.loader.Loader.list(Loader.java:2120)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:67)
    ... 97 more
Caused by: org.jboss.util.NestedSQLException: Transaction is not active: tx=TransactionImple < ac, BasicAction: -53effdb8:7d1:4bb313e3:7a status: ActionStatus.ABORT_ONLY >; - nested throwable: (javax.resource.ResourceException: Transaction is not active: tx=TransactionImple < ac, BasicAction: -53effdb8:7d1:4bb313e3:7a status: ActionStatus.ABORT_ONLY >)
    at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:95)
    at org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:46)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
    ... 111 more
Caused by: javax.resource.ResourceException: Transaction is not active: tx=TransactionImple < ac, BasicAction: -53effdb8:7d1:4bb313e3:7a status: ActionStatus.ABORT_ONLY >
    at org.jboss.resource.connectionmanager.TxConnectionManager.getManagedConnection(TxConnectionManager.java:370)
    at org.jboss.resource.connectionmanager.BaseConnectionManager2.allocateConnection(BaseConnectionManager2.java:496)
    at org.jboss.resource.connectionmanager.BaseConnectionManager2$ConnectionManagerProxy.allocateConnection(BaseConnectionManager2.java:941)
    at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:89)
    ... 113 more

It turned out that this is not a hibernate or jboss exception at all. The actual cause of this was a NullPointerException somewhere in the flow of the application. This caused the transaction to end and re-enter a loop when the transaction was not active and this was thrown. So if you ever get this exception I suggest you put some break points in your code and see where it fails. It’s likely that there is an unchecked exception thrown somewhere in your programme.

Categories: Hibernate Tags: ,