我有一个使用Hibernate 3.1和JPA注释的应用程序.它有几个具有byte[]属性的对象(大小为1k-200k).它使用JPA@Lob注释,并且Hibernate 3.1可以在所有主要数据库上很好地读取这些注释--它似乎隐藏了JDBC Blob供应商的特性(它应该这样做).
@Entity
public class ConfigAttribute {
@Lob
public byte[] getValueBuffer() {
return m_valueBuffer;
}
}
当我们发现PostgreSQL中的Hibernate 3.5breaks (and won't fix)这个注释组合(没有解决办法)时,我们不得不升级到3.5.到目前为止,我还没有找到明确的修复方法,但我确实注意到,如果只删除@Lob,它将使用PostgreSQL类型byteA(它可以工作,但只在Postgres上有效).
annotation postgres oracle works on
-------------------------------------------------------------
byte[] + @Lob oid blob oracle
byte[] bytea raw(255) postgresql
byte[] + @Type(PBA) oid blob oracle
byte[] + @Type(BT) bytea blob postgresql
once you use @Type, @Lob seems to not be relevant
note: oracle seems to have deprecated the "raw" type since 8i.
我正在寻找一种方法,有一个单一的注释类(具有BLOB属性),可以跨主要数据库移植.
- 注释byte[]属性的可移植方式是什么?
- 在某些最新版本的Hibernate中修复了这个问题吗?
Update: 在阅读了this blog之后,我终于明白了JIRA问题中最初的解决方法是什么:显然,您应该删除@Lob并将该属性注释为:
@Type(type="org.hibernate.type.PrimitiveByteArrayBlobType")
byte[] getValueBuffer() {...
然而,这不能for me起作用--我仍然得到OID,而不是BYTEA;但是,它确实对JIRA问题的作者有效,他似乎想要OID.
在A.Garcia给出答案后,我try 了这个组合,它实际上对postgresql有效,但对oracle无效.
@Type(type="org.hibernate.type.BinaryType")
byte[] getValueBuffer() {...
我真正需要做的是控制哪个@org.冬眠注释.键入组合(@Lob+byte[]被映射)到(在postgresql上).
下面是3.5.5中的代码片段,最终来自MaterializedBlobType(SQL类型Blob).根据Steve的博客,PostgreSQL希望您将Streams用于byTea(不要问我为什么),并将PostgreSQL的自定义Blob类型用于OID.还要注意,在JDBC上使用setBytes()也是为了byteA(根据过go 的经验).这就解释了为什么Use-Streams没有影响,它们都假定为"byTea".
public void set(PreparedStatement st, Object value, int index) {
byte[] internalValue = toInternalFormat( value );
if ( Environment.useStreamsForBinary() ) {
// use streams = true
st.setBinaryStream( index,
new ByteArrayInputStream( internalValue ), internalValue.length );
}
else {
// use streams = false
st.setBytes( index, internalValue );
}
}
这将导致:
ERROR: column "signature" is of type oid but expression is of type bytea
Update
ERROR: column "signature" is of type bytea but expression is of type oid
Hibernate中的类型系统目前是一个"正在进行中的工作"(根据3.5.5反对意见).事实上,3.5.5代码中的很多内容都被弃用了,以至于很难知道当子类化PostgreSQLDialect时要看什么).
阿法克,类型.postgresql上的BLOB/'oid'应该映射到使用oid样式JDBC访问的某个自定义类型(即PostgresqlBlobType对象和非materialized BlobType).我从来没有在postgresql中成功地使用过Blob,但我知道bytea只是像我预期的那样工作.
我目前正在研究BatchUpdateException--驱动程序可能不支持批处理.
2004年的名言:
References:
- https://forum.hibernate.org/viewtopic.php?p=2393203个
- https://forum.hibernate.org/viewtopic.php?p=2435174个
- http://hibernate.atlassian.net/browse/HHH-4617
- http://postgresql.1045698.n5.nabble.com/Migration-to-Hibernate-3-5-final-td2175339.html个
- https://jira.springframework.org/browse/SPR-2318个
- https://forums.hibernate.org/viewtopic.php?p=2203382&sid=b526a17d9cf60a80f13d40cf8082aafd个
- http://virgo47.wordpress.com/2008/06/13/jpa-postgresql-and-bytea-vs-oid-type/个