Common Hibernate Errors and Causes

written by aext on October 20, 2008 in Tutorials with no comments

This entry deals with some problems when do a work with Hibernate, you will get some errors which you can’t understand or dont know causes of that errors. Along with the exception or error messages themselves, potential causes of these errors are often listed along with links to additional resources.

To find the error you’re looking for, use your browser’s Find or Search capability and input a few words that you are seeing in your error message.

1. org.hibernate.hql.ast.QuerySyntaxException: unexpected end of subtree HQL query…

A complete exception would look like:

2007-06-06 10:14:02,953 [DefaultQuartzScheduler_Worker-4]
  ERROR PARSER::reportError - <AST>:0:0: unexpected end of subtree
  org.hibernate.hql.ast.QuerySyntaxException: unexpected end of subtree [QUERY]
    at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:225)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:158)
...

Probable cause: In the HQL query you have used a collection for a binding (maybe something like an in (:variable), and you have given a collection by using the setParameterList() method. But the collection that was binded is empty.

2. org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list…

A complete exception would look like:

Exception in thread "main" org.hibernate.QueryException:
query specified join fetching, but the owner of the fetched
association was not present in the select list
[FromElement{explicit,not a collection join,fetch join,fetch non-lazy
properties,classAlias=j_addressParent3,role=null,tableName=t_address,
tableAlias=address4_,origin=t_person_insurance
insurance3_,colums={insurance3_.ADDRESS_ID ,className=nl.sodeso.demo.impl.Address}}] [QUERY]

Probable cause: You have a HQL query that contains a join with a fetch option but that join has an owner that does not specify the fetch option, make sure that the owner also has the fetch option.

The following is an example of the problem:

01: select person from Person as person
02: left join person.address as address
03: left join fetch address.country as country

Here you can see that the join on line 03 is a child join of the join on line 02,

the join on line 03 specifies the fetch option but the join on line 02 doesn’t.

In this case the join on line 02 should be changed to:

02: left join fetch person.address as address
3. exception: org.hibernate.exception.SQLGrammarException: could not execute native bulk manipulation query

Something in your code will be:

String del = "DELETE VotableNode vn WHERE vn.votingSetBucketId = " + Integer.toString(bucketId);

session.createSQLQuery(del).executeUpdate();

Probable cause: It’s saying that you can’t create a bulk delete on a sql query by using hibernate.

The error in this code is that you were executing a HQL script using createSQLQuery method which is completely wrong. You should use createQuery:

String del = "DELETE VotableNode vn WHERE vn.votingSetBucketId = " + Integer.toString(bucketId);

session.createQuery(del).executeUpdate();

So there’s nothing much to say, If you want to make a bulk task like an update of multiple records or a deletion of multiple rows using a simple query, you MUST use a HQL query.

4. Initial SessionFactory creation failed.org.hibernate.MappingException: Error reading resource:app1/contact.hbm.xml

Probable causes: Your file xml config is missing in your classpath.

5. org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions.

from imma
Probable causes: It showed up when I attempt to update an element in a collection; adding to the collection was not a problem, hibernate mapping files are ok and everything else seemed to be in order but the error keeps turning up.

Solution: it turned out that I was preserving a value across requests whose presence cause a select statement to be executed before the update hence the exception.

A little context about the exception mentioned: I was using Hibernate, Spring and JSF. I was trying to update the collection using a JSF managed bean that has a request scope. Note that the hibernate data access object is managed by Spring hence is not (in my setup) part of JSF life cycle.

So with the above scenario, the first request attaches the collection to a hibernate session, subsequent attempts to modify the collection threw up the mentioned exception.

Changing the scope of the JSF managed bean seems to solve the issue.

Updated at 10/28/08 by Nguyen, Lam D