首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

PostgreSQL + Hibernate 关于 Bytea 和 oid 的映射有关问题

2012-11-09 
PostgreSQL + Hibernate 关于 Bytea 和 oid 的映射问题问题起始,1. 我在pojo里面定义了一个 binaryData,想

PostgreSQL + Hibernate 关于 Bytea 和 oid 的映射问题

问题起始,

1. 我在pojo里面定义了一个 binaryData,想通过Blob类型映射,

3. Database 对应的Column 为 bytea?

4.Java 代码?

Hint: You will need to rewrite or cast the expression.
Position: 42
27 Jul 2010 17:12:13,828 ERROR AbstractFlushingEventListener - Could not synchronize database state with session

?

问题追溯,

1. PostgreSQL 用两种类型 bytea/oid 来?处理binary stream

http://jdbc.postgresql.org/documentation/80/binary-data.html

?

2. Hibernate BlobType 默认行为
转载自 http://blog.toadhead.net/index.php/2005/02/12/postgresql-blobs-with-hibernate/

My test JDBC code looks something like:

PreparedStatement stmt = conn.prepareStatement("INSERT INTO foo (data) VALUES (?)");stmt.setBlob(1, Hibernate.createBlob(myInputStream));stmt.executeUpdate();

The above example works fine. The Postgresql JDBC driver accepts any object that implements javax.sql.Blob.

I therefore assumed that the following code would work:

HibernateObject o = new HibernateObject();o.setBlob(Hibernate.createBlob(myInputStream));hibernate.save(o);

Unfortunately this doesn’t work. The problem lies in net.sf.hibernate.type.BlobType. Hibernate contains the following logic:

public void set(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {??final boolean useInputStream = session.getFactory().getDialect().useInputStreamToInsertBlob()????&& BlobImplementer.class.isInstance( blob );   if (value instanceof BlobImpl) {       BlobImpl blob = (BlobImpl) value;       st.setBinaryStream( index, blob.getBinaryStream(), (int) blob.length() );   }   else {       st.setBlob(index, (Blob) value);   }}

The problem with the above code is that Postgresql uses setBinaryStream for byte array (bytea) fields. Hibernate incorrectly assumes that all databases can use setBinaryStream on blob fields.

Steve Lustbader filed a bug HB-955 regarding this issue that has, unfortunately, been rejected. Fortunately, there is a fairly simple workaround. net.sf.hibernate.lob.BlobImpl is the Hibernate class that implements javax.sql.Blob. I copied the implementation of this class and put it in my own class. I then use this class to the set the blob field in my Hibernate object. Hibernate than passes this object onto the JDBC driver and everything works well.

I understand the logic behind what is happening in Hibernate. They are trying to prevent problems with JDBC drivers that cast the blob field to their own implementation of javax.sql.Blob. I’m of the opinion that any JDBC driver that does this is broken. Any JDBC driver that does not accept any implementation of javax.sql.Blob is in my opinion broken. The assumption that all blobs can be set using setBinaryStream() is also incorrect, in my opinion.

?

问题原因

1.?Postgres 保存 bytea 的 JDBC代码片段

????测试代码,

<prop key="hibernate.dialect">com.haodao.dialect.MyPostgreSQLDialect</prop>

?

??? 这样,无需改动原有配置,程序正常执行,也保留了 stream 的特性,易于移植同事也不会带来高并发下OutOfMemory的问题

?

?

?



?

热点排行