Is there any way to use OrmLite with Postgres hstores?

后端 未结 3 576
执笔经年
执笔经年 2021-01-06 07:53

We\'re currently using a PostgreSQL database and OrmLite. We now have a use case for using an Postgres hstore, but can\'t find any way of accessing that table through OrmLit

3条回答
  •  花落未央
    2021-01-06 08:38

    @Gray I submitted an ORMLite patch on SourceForge that can enables the "Other" data type. The patch ID is 3566779. With this patch, it's possible to support hstores.

    Users will need to add the PGHStore class to their projects. The code for this class is here.

    Users will also need to add a persister class as shown here:

    package com.mydomain.db.persister;
    
    import com.mydomain.db.PGHStore;
    import com.j256.ormlite.field.FieldType;
    import com.j256.ormlite.field.SqlType;
    import com.j256.ormlite.field.types.BaseDataType;
    import com.j256.ormlite.support.DatabaseResults;
    import java.sql.SQLException;
    
    public class PGHStorePersister extends BaseDataType {
    
        private static final PGHStorePersister singleton = new PGHStorePersister();
    
        public static PGHStorePersister getSingleton() {
            return singleton;
        }
    
        protected PGHStorePersister() {
            super(SqlType.OTHER, new Class[] { PGHStore.class });
        }
    
        protected PGHStorePersister(SqlType sqlType, Class[] classes) {
            super(sqlType, classes);
        }
    
        @Override
        public Object parseDefaultString(FieldType ft, String string) throws SQLException {
            return new PGHStore(string);
        }
    
        @Override
        public Object resultToSqlArg(FieldType fieldType, DatabaseResults results, int columnPos) throws SQLException {
            return results.getString(columnPos);
        }
    
        @Override
        public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) throws SQLException {
            return new PGHStore((String) sqlArg);
        }
    
        @Override
        public boolean isAppropriateId() {
            return false;
        }
    }
    

    Lastly, users will need to annotate their data to use the persister.

    @DatabaseField(columnName = "myData", persisterClass=PGHStorePersister.class)
    

提交回复
热议问题