Is there any way to use OrmLite with Postgres hstores?

后端 未结 3 575
执笔经年
执笔经年 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)
    
    0 讨论(0)
  • 2021-01-06 08:55

    At the very least I'd like a handle to the existing connection OrmLite is using so I can reuse it to build a prepared statement...

    Ok, that's pretty easy. As @jsight mentioned, the ORMLite ConnectionSource for JDBC is JdbcConnectionSource. When you get a connection from that class using connectionSource.getReadOnlyConnection(), you will get a DatabaseConnection that is really a JdbcDatabaseConnection and can be cast to it. There is a JdbcDatabaseConnection.getInternalConnection() method which returns the associated java.sql.Connection.

    I've tried to use updateRaw(), but that function only exists on an OrmLite dao that I don't have ...

    You really can use any DAO class to perform a raw function on any table. It is convenient to think of it as being an unstructured update to an DAO object's table. But if you have any DAO, you can perform a raw update on any other table.

    find a way to transfer data to and from the database using unsupported fields instead of just unsupported queries

    If you are using unsupported fields, then you are going to have to do it as a raw statement -- either SELECT or UPDATE. If you edit your post to show the raw statement you've tried, I can help more specifically.

    0 讨论(0)
  • 2021-01-06 08:55

    It sounds like ConnectionSource may actually be implemented by JdbcConnectionSource, and will likely return a JdbcDatabaseConnection. That object has a getInternalConnection method that looks like what you are looking for.

    0 讨论(0)
提交回复
热议问题