Rene-G
Rene-G

Reputation: 21

JPA, Hibernate, DB2: JPQL-Query and char column

I'm using JPA with hibernate and have some trouble with the following JPQL query :

final Query query = getEntityManager().createQuery(
            "select u from User u where u.username = :username")
query.setParameter("username", "a"); 

When using a DB2 V9.7 database the query leads to a NoResultException even though a user with username 'a' exists. This problem only occurs if the column with the username has the type char. If i change the column type to varchar, like hibernate would generate it by himself, the query works fine.

Any ideas?

Enclosed some configuration information :

     @Column(name = "USER_NAME", length = 50)
     @NotNull
     @Size(max = 50)
     private String username;
    

Upvotes: 1

Views: 1805

Answers (1)

Vipin Thomas
Vipin Thomas

Reputation: 776

Hibernate maps String to VARCHAR type. This is explained here

http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/types.html

To make sure it maps to char you need to specify in your custom db2 dialect as mentioned here.

JPA/Hibernate DDL generation; CHAR vs. VARCHAR

Upvotes: 0

Related Questions