Archive

Archive for the ‘Hibernate’ Category

Caused by javax.persistence.TransactionRequiredException with message: “no transaction is in progress”

2 May 2011 2 comments

This is weird, I am not sure why I am getting it. I have an application that makes some calculations that take some considerable time, and I persist the results. I run my application with 100 records from the database and everything works fine. I run my application with 60.000 records from the database and I am getting the above error message. The exception happens when the flush() method is called on the EntityManager. If I am to have a wild guess I’d say that there is a transaction time out (it is set to 30 seconds in the weblogic console) and therefore when the flush() method is called there is no active transaction.

Anyway I managed to overcome this issue by explicitly defining a user transaction

import org.jboss.seam.transaction.Transaction;
import org.jboss.seam.transaction.UserTransaction;
...
...

UserTransaction ut = Transaction.instance();
ut.begin();
...
...
ut.commit();

If you have any idea why this is happening please leave a comment.

UPDATE: Now the first transaction (the one with 100 records) fails. It complains that there is already one transaction active when I try to start a new one. I guess I need to revert my code and to increase the transaction timeout on the weblogic console, this would solve both issues.

Categories: Hibernate, Seam, WebLogic

org.hibernate.MappingException: broken column mapping for

16 February 2011 Leave a comment

I got this error when I tried to map a child element by using its parent’s foreign key. I had a parent element with a region id (as foreign key) and I needed to get the child element whose primary key was this region id (and a language id). My child element (Region.hbm.xml) had a composite primary key

<composite-id>
  			<key-property name="regionCode" type="integer" column="RGNCDE"/>
  			<key-property name="languageCode" type="integer" column="LNGCDE"/>
  		</composite-id>

and in my parent I did

<property name="regionCode" column="REGCDE" type="integer"/>

<many-to-one name="region" class="my.package.hbm.Region">

but this resulted in “org.hibernate.MappingException: broken column mapping for” error.

I had to split the child’s composite key

<id name="regionCode" type="integer" column="RGNCDE">
			<generator class="assigned"/>
		</id>

		<property name="languageCode" column="LNGCDE" type="integer"/>

and add a formula property in my parent’s class mapping

<property name="regionCode" column="REGCDE" type="integer"/>

<many-to-one name="region" class="my.package.hbm.Region" formula="REGCDE"/>

so that hibernate could link the region id of the parent to the region id of the child. And since I needed a record based also on the language code (but my parent didn’t have any language code column) I had to manually add search criteria

crit.createCriteria("region").add(Restrictions.eq("languageCode", 1));
Categories: 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.

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: ,

org.hibernate.MappingException: Unknown entity: AFullyQualifiedClassName

21 May 2008 68 comments

This is actually very annoying. I spent two hours trying to find out why I was getting this error message while using annotations to declare my entity bean. I checked everything, the hibernate configuration file, I made sure my annotated class was declared and added to the AnnotationConfiguration I was using and I also made sure that I had the fully qualified class name in my classpath. I (thought that I) made sure everything was ok but I was still getting this error message.

At last I found out that I had accidentally used the wrong annotation with my entity class. When I declared my class to be an entity bean (a persistent POJO), by using the @Entity annotation, I had accidentally imported the org.hibernate.annotations.Entity class instead of the correct one javax.persistence.Entity. I used the auto-complete Eclipse feature and somehow I imported the wrong package.

Categories: Hibernate, Java