Re: [BUGS] Mapping Hibernate boolean to smallint(Postgresql) - Mailing list pgsql-jdbc

From Radosław Smogura
Subject Re: [BUGS] Mapping Hibernate boolean to smallint(Postgresql)
Date
Msg-id 1904636.6EQgLgcKN2@radekdesktop
Whole thread Raw
In response to Re: [BUGS] Mapping Hibernate boolean to smallint(Postgresql)  (c_h_thakar <chirag.thakar@sas.com>)
List pgsql-jdbc
Dnia środa, 21 marca 2012 o 05:07:43 c_h_thakar napisał(a):
> stagirus,
>
> How did you solve this issue ? Did you use UserType ?
>
> Could you please send the details ?
>
>
> --
> View this message in context:
> http://postgresql.1045698.n5.nabble.com/Re-BUGS-Mapping-Hibernate-boolean-t
> o-smallint-Postgresql-tp2855367p5582702.html Sent from the PostgreSQL - jdbc
> mailing list archive at Nabble.com.
I may give you alternative solution, I use it for different situations, but
currently I write it "by hand". It's JPA aware, not bound to hibernate. Try
this:
public class entity {
    private int intAsBool;
    private boolean boolValue;

    @Column()
    protected int getIntAsBool() {...}
    protected void setIntAsBool(int val) {...}

    @Transient
    public boolean getBoolValue() {...}
    public boolean setBoolValue() {...}

    @PostLoad
    protected void postLoad() {
        setBoolValue(getIntAsBool() == 1);
    }

    @PrePersist
    @PreUpdate
    protected void preStore() {
        setIntAsBool(getBoolValue() ? 1 : 0);
    }
}

Bear in mind you need to choose only one way to mark JPA annotations field or
ancestors, 2nd one is better for many reasons.

Hope this help. I use this construct to store and map T/N char(1) values to
boolean.

Regards,
Radosław Smogura
http://softperience.eu/

pgsql-jdbc by date:

Previous
From: c_h_thakar
Date:
Subject: Re: [BUGS] Mapping Hibernate boolean to smallint(Postgresql)
Next
From: "Atri Sharma"
Date:
Subject: Regarding GSoc 2012 project idea