使用Informix中的Hibernate读取新类型

时间:2017-12-20 09:57:15

标签: java hibernate informix

我在Informix数据库中使用新的未记录类型 longlvarchar 定义了一个列。使用JDBC我可以通过获取二进制流来读取此列,但我找不到使用hibernate读取此列的方法。我已经定义了一个实现InformixDialect的类,我已经注册了这个clas,所以hibernate可以使用它。在本课程中,我尝试将 Longlvarchar 类型映射到 longvarchar (以及其他类型),但无法读取数据。 使用类似:testDao.findTestList().get(0).getLlvarchar()的dao仅打印 com.informix.jdbc.IfxUDT@2c708440 。如何将这种新类型注册为休眠,或者像JDBC一样将其读作二进制流?我正在使用hibernate 5.2.12 final。

JDBC:

private void getMetainf() {
    try (Statement stmt = con.createStatement(); ResultSet res = stmt.executeQuery("select * from verylongvarchar where id = 1");) {
        ResultSetMetaData rsmd = res.getMetaData();
        System.out.println(con.getMetaData());

        res.next();
        readStream(res.getBinaryStream(2));


    } catch (SQLException e) {
        System.out.println("Select Failed ...\n" + e.getMessage());
        logger.error(" Get failed ", e);

    }
}

private void readStream(final ResultSet res) {
    try {
        InputStream in = res.getBinaryStream(2);

        byte[] temp = new byte[100];
        int i = -1;
        while ((i = in.read(temp)) != -1) {

            // Create String from bytes
            String s = new String(temp, 0, i);
            System.out.println(s);
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

休眠: 实体:

@Entity
@Table(name = "verylongvarchar")

public class TestEntity extends QidbEntity implements Serializable {

private static final long serialVersionUID = 6249297171036357454L;

@Id
@Column(name = "id", unique = true, nullable = false)
private Integer id;

@Column(name = "llvarchar", columnDefinition = "longlvarchar")

private String llvarchar;

@Column(name = "longvarchar")
private String longvarchar;

@Column(name = "textcolumn")
private String textcolumn;

@Column(name = "normaltext")
private String normaltext;
...

我的道:

public class TestDao extends AbstractDAO {

public List<TestEntity> findTestList() {
    return findEntities(TestEntity.class);
}

public TestEntity findResult() {
    String query = "SELECT te FROM TestEntity te where te.id = 1";
    return createQuery(query, TestEntity.class).getSingleResult();
}

} 重写Informixdialect:

public class InformixDataType extends InformixDialect {

public InformixDataType() {
    super();

    registerColumnType(Types.LONGVARCHAR, "longlvarchar");
    registerHibernateType(Types.LONGVARCHAR, "longlvarchar");
}
}

0 个答案:

没有答案