I’m having a little trouble using Hibernate with a char(6) column in Oracle. Here’s the structure of the table:
CREATE TABLE ACCEPTANCE
(
USER_ID char(6) PRIMARY KEY NOT NULL,
ACCEPT_DATE date
);
For records whose user id has less than 6 characters, I can select them without padding the user id when running queries using SQuirreL. I.E. the following returns a record if there’s a record with a user id of “abc”.
select * from acceptance where user_id = "abc"
Unfortunately, when doing the select via Hibernate (JPA), the following returns null:
em.find(Acceptance.class, "abc");
If I pad the value though, it returns the correct record:
em.find(Acceptance.class, "abc ");
The module that I’m working on gets the user id unpadded from other parts of the system. Is there a better way to get Hibernate working other than putting in code to adapt the user id to a certain length before giving it to Hibernate? (which could present maintenance issues down the road if the length ever changes)
That’s God’s way of telling you to never use CHAR() for primary key 🙂
Seriously, however, since your
user_idis mapped as String in your entity Hibernate’s Oracle dialect translates that intovarchar. Since Hibernate uses prepared statements for all its queries, that semantics carries over (unlike SQuirreL, where the value is specified as literal and thus is converted differently).Based on Oracle type conversion rules column value is then promoted to
varchar2and compared as such; thus you get back no records.If you can’t change the underlying column type, your best option is probably to use HQL query and
rtrim()function which is supported by Oracle dialect.