2

The following code works well with MySQL.

But the same code fails when switching HSQLDB in-memory database (for unit test),

Query query = entityManager.createQuery("SELECT c FROM CartInvoiceEntity c WHERE c.invoiceId = :invoiceId");

query.setParameter("invoiceId", cartInvoiceEntity.getInvoiceId());

with the following error message:

org.hibernate.QueryParameterException: could not locate named parameter [invoiceId]

Inspected on query object when debugging, I found the query object has a parameters field. When using MySQL, this parameters contains "invoiceId" in its HashMap. But when switching to HSQLDB, this HashMap is empty -- this is why the exception thrown. The question is why the HashMap is empty with HSQLDB being used.

Following is the relevant Maven dependencies

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.1.9.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.1.9.Final</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.16</version>
    </dependency>
    <dependency>
        <groupId>hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>1.8.0.10</version>
        <scope>test</scope>
    </dependency>

From the dependency hierarchy, I can see hibernate-jpa-2.0.api:1.0.1.Final is used and it is indirectly referenced from both hibernate-core and hibernate-entitymanager JARs.

Any help is highly appreciated!

1
  • Why not try a more recent version of HSQLDB, such as 2.2.9. Commented Mar 7, 2013 at 21:34

1 Answer 1

0

Just found the root cause. When doing unit test (not necessarily with or without in-memory database such as HSQLDB), somehow, Spring component scan mechanism does not work properly for the JPA persistence setting. This causes the Hibernate failure in looking for parameter. Hibernate only looks for parameter if it knows entity classes.

My fix is to add class entries (for entity classes) into persistence.xml directly (don't know how to do that in org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean when create EntityManagerFactory).

<persistence-unit name="my_unit">
    ...
    <class>my.entity.CartInvoiceEntity</class>
    ...
</persistence-unit>

See:

http://www.springbyexample.org/examples/one-to-many-jpa-hibernate-config-jpa-config.html http://www.aoiblog.com/wp-content/uploads/2010/02/two.jpg

But I did encounter some other issue which is due to HSQLDB itself as follows.

Caused by: org.hibernate.exception.GenericJDBCException: This function is not supported

This issue has been resolved by upgrade HSQLDB's version. See the following stackoverflow link:

Function not supported from hibernate

Thanks.

Sign up to request clarification or add additional context in comments.

1 Comment

You can configure a LocalContainerEntityManagerFactoryBean using Java to scan the package where your entity classes are if you want to avoid the persistence.xml: bean.setPersistenceUnitName("my_unit"); bean.setPackagesToScan("my.entity");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.