Joseph
Joseph

Reputation: 145

JPA Postgres insert Bit(4) with default value

I've got Postgres DB, Java with JPA 2.0 and eclipseLink.

In my To object:

     @Column(name = "mask")
     private Boolean mask;

IN DB: mask bit(4) DEFAULT B'1000'::"bit",

When I want to presist the object:

Caused by: org.postgresql.util.PSQLException: ERROR: column "mask" is of type bit but expression is of type boolean Hint: You will need to rewrite or cast the expression.

I try in TO class: - Boolean - BitSet(4) - String - Integer - Char

With BitSet I try, these: TO object: @Column(name = "mask") private BitSet mask;

     public BitSet getMask() {
         BitSet work = new BitSet(4);        
         work.set(0);
         if (mask == null){
           return work;
         }
        return mask;
    }

    public void setMask(BitSet mask) {
      BitSet work = new BitSet(4);        
      work.set(0);
      if (mask== null) {
        this.mask= work;
      } else {
        this.mask= mask;
      }
    }

How can i make it work?

The boolean method is not good, store 1 bit. I search in google, lost of forums.

After 5 hour of google search i found that: http://archives.postgresql.org/pgsql-bugs/2005-05/msg00014.php

Its about a postgres BUG:

"Having a JDBC type called "BIT" which actually maps to a single boolean type is very confusing. If you assume that JDBC's BIT has nothing to do with the server type called "bit", and that it's just a coincidence that they have the same name, then things should be clearer."

Upvotes: 1

Views: 1899

Answers (1)

James
James

Reputation: 18379

If you access your field through raw JDBC what type is returned?

Did you try mapping it as a String, what error occurs? You may need to use a @Converter to convert the type.

See, http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters/Converter

Upvotes: 1

Related Questions