antohoho
antohoho

Reputation: 1020

Hibernate's ColumnTransformer stopped working after upgrate to hibernate 5.3.4 from 5.2.12

I have entity class with one of the field annotated with @ColumnTransformer

@NotNull
@Column(name = "CURRENCY_CODE_ID")
@ColumnTransformer(
   read = "(SELECT cc.code FROM currency_code AS cc WHERE cc.currency_code_id = currency_code_id)",
   write = "(COALESCE((SELECT cc.currency_code_id FROM currency_code AS cc WHERE cc.code = ?),0))")
public String currency;

In hibernate 5.2.12 it worked fine, however after upgrade to Hibernate 5.3.4.Final it suddenly broke and I am getting following query generated

campaign0_.campaign_group_id as campaign3_33_,
(SELECT cc.code FROM public.currency_code AS cc 
    WHERE cc.campaign0_.currency_code_id = campaign0_.currency_code_id) as currency4_33_,
campaign0_.date_created as date_cre5_33_,

So in where clause cc.currency_code_id = currency_code_id was replaced with cc.campaign0_.currency_code_id . So basically generated table name campaign0_ was injected between alias of the table and column name. Not sure how it happened and what fix for that. I did search but find nothing so far.

Nothing mentioned in migration guide either - https://github.com/hibernate/hibernate-orm/blob/5.3/migration-guide.adoc

Upvotes: 3

Views: 1594

Answers (1)

antohoho
antohoho

Reputation: 1020

finally, after lots of debugging and research I was able to find this bug reported for 5.* version series - https://hibernate.atlassian.net/browse/HHH-8944?page=com.atlassian.jira.plugin.system.issuetabpanels%3Aall-tabpanel

looks like i have to use forColumn configuration, I tried it and it worked however little strange. From multiple combination uppercased worked

 @NotNull
    @Column(name = "CURRENCY_CODE_ID")
    @ColumnTransformer(
            forColumn = "CURRENCY_CODE_ID",
            read = "(SELECT cc.code FROM currency_code AS cc WHERE cc.CURRENCY_CODE_ID = currency_code_id)",
            write = "(COALESCE((SELECT cc.currency_code_id FROM currency_code AS cc WHERE cc.code = ?),0))")
    public String currency;

As you can see I added forColumn = "CURRENCY_CODE_ID", as uppercase and referenced cc.CURRENCY_CODE_ID as uppercased so it skipped it. So behavior is still little funky m but at list works ,

generated result is

 (SELECT
            cc.code 
        FROM
            public.currency_code AS cc 
        WHERE
            cc.CURRENCY_CODE_ID = campaign0_.currency_code_id) as currency4_33_,

this worked as well, so looks like upper case matter. Witch saved me

@NotNull
    @Column(name = "currency_code_id")
    @ColumnTransformer(
            forColumn = "currency_code_id",
            read = "(SELECT cc.code FROM currency_code AS cc WHERE cc.CURRENCY_CODE_ID = currency_code_id)",
            write = "(COALESCE((SELECT cc.currency_code_id FROM currency_code AS cc WHERE cc.code = ?),0))")
    public String currency;

Upvotes: 3

Related Questions