How to map column with type bit(24) in postgresql with hibernate?

Member

by orpha , in category: MySQL , 4 months ago

How to map column with type bit(24) in postgresql with hibernate?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 4 months ago

@orpha 

In PostgreSQL, the bit(24) data type is used to store a fixed-length bit string of 24 bits. To map this column type with Hibernate, you can use the @Type annotation along with a custom BitStringType class that implements the org.hibernate.usertype.UserType interface.


First, create a custom BitStringType class that converts the 24-bit String to a byte array and vice versa:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

public class BitStringType implements UserType {

    @Override
    public int[] sqlTypes() {
        return new int[]{Types.VARBINARY};
    }

    @Override
    public Class returnedClass() {
        return String.class;
    }

    @Override
    public boolean equals(Object x, Object y) throws HibernateException {
        return x.equals(y);
    }

    @Override
    public int hashCode(Object x) throws HibernateException {
        return x.hashCode();
    }

    @Override
    public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
        byte[] bytes = rs.getBytes(names[0]);
        if (bytes == null) {
            return null;
        }
        return bytes.toString();
    }

    @Override
    public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
        if (value == null) {
            st.setNull(index, Types.VARBINARY);
        } else {
            byte[] bytes = ((String) value).getBytes();
            st.setBytes(index, bytes);
        }
    }

    @Override
    public Object deepCopy(Object value) throws HibernateException {
        return value;
    }

    @Override
    public boolean isMutable() {
        return false;
    }

    @Override
    public Serializable disassemble(Object value) throws HibernateException {
        return (Serializable) value;
    }

    @Override
    public Object assemble(Serializable cached, Object owner) throws HibernateException {
        return cached;
    }

    @Override
    public Object replace(Object original, Object target, Object owner) throws HibernateException {
        return original;
    }
}


Next, annotate the column in your entity class with @Type and specify the BitStringType class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import org.hibernate.annotations.Type;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class YourEntity {

    @Id
    @GeneratedValue
    private long id;

    @Column
    @Type(type = "com.example.BitStringType")
    private String yourBitStringColumn;

    // getters/setters
}


With this setup, Hibernate will use the custom BitStringType class to map the bit(24) column in PostgreSQL to a Java String in your entity class.