无法使用JPA将null保留到oracle BLOB中

时间:2013-08-29 21:39:19

标签: java jpa oracle11g blob openjpa

我正在尝试使用JPA(EclipseLink 2.3.x)将映像Byte[]和调度程序TimerHandle持久化到oracle 11g数据库

当属性的值不为null时,JPA能够将Byte [] / TimerHandle持久保存到数据库,但是当这些属性为null时,它会给出此异常。

Caused by: java.lang.ClassCastException: oracle.sql.BLOB incompatible with oracle.sql.BLOB
    at oracle.jdbc.driver.OraclePreparedStatement.setBlob(OraclePreparedStatement.java:6663)
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.setBlob(OraclePreparedStatementWrapper.java:128)
    at org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.setBlob(DelegatingPreparedStatement.java:387)
    at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator$LoggingConnection$LoggingPreparedStatement.setBlob(LoggingConnectionDecorator.java:1499)
    at org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.setBlob(DelegatingPreparedStatement.java:387)
    at org.apache.openjpa.jdbc.sql.OracleDictionary.setNull(OracleDictionary.java:633)
    at org.apache.openjpa.jdbc.sql.DBDictionary.setTyped(DBDictionary.java:1285)
    at org.apache.openjpa.jdbc.sql.RowImpl.flush(RowImpl.java:896)
    at org.apache.openjpa.jdbc.sql.RowImpl.flush(RowImpl.java:856)
    at org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flushAndUpdate(PreparedStatementManagerImpl.java:117)
    at org.apache.openjpa.jdbc.kernel.BatchingPreparedStatementManagerImpl.batchOrExecuteRow(BatchingPreparedStatementManagerImpl.java:99)
    at org.apache.openjpa.jdbc.kernel.BatchingPreparedStatementManagerImpl.flushAndUpdate(BatchingPreparedStatementManagerImpl.java:83)
    at com.ibm.ws.persistence.jdbc.kernel.PreparedStatementManagerImpl.flushAndUpdate(PreparedStatementManagerImpl.java:63)
    at org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flushInternal(PreparedStatementManagerImpl.java:100)
    at org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flush(PreparedStatementManagerImpl.java:88)
    at org.apache.openjpa.jdbc.kernel.ConstraintUpdateManager.flush(ConstraintUpdateManager.java:550)
    at org.apache.openjpa.jdbc.kernel.ConstraintUpdateManager.flush(ConstraintUpdateManager.java:106)
    at org.apache.openjpa.jdbc.kernel.BatchingConstraintUpdateManager.flush(BatchingConstraintUpdateManager.java:59)
    at org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:105)
    at org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:78)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.flush(JDBCStoreManager.java:737)
    at org.apache.openjpa.kernel.DelegatingStoreManager.flush(DelegatingStoreManager.java:131)
    at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:2178)
    at org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:2076)
    at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:1847)
    ... 70 more

如何解决此问题,以便null可以在数据库中作为(null)持久保存到数据库?

1 个答案:

答案 0 :(得分:0)

我找到了部分解决方案...... 遇到此问题时,请使用普通的oracle.sql.BLOB而不是映射到BLOB的任何其他类。

@Entity
public class FileStorage implements Serializable {
    @Id
    private Long fileId;
    @Lob
    //private byte[] fileData; replaced this line with the line below.
    private oracle.sql.BLOB fileData;
    //Getter and setter
}

public class WorkingClass{
    private EntityManager em;
    public foo(){
        FileStorage f = new FileStorage();
        f.setFileData(BLOB.getEmptyBLOB());
        //f.getFileData().setBytes(someByteArrayData);
        em.persist(f);
    }
}

public class ErrorClass{
    private EntityManager em;
    public foo(){
        FileStorage f = new FileStorage();
        f.setFileData();
        //fail when the line below is commented
        //f.setFileData(someByteArrayData);
        em.persist(f);
    }
}

但现在问题困扰TimerHandle。使用ObjectOutputStream / ObjectInputStream手动序列化和反序列化后,计时器似乎没有正确序列化,当我调用NoSuchObjectLocalException时,即使我确认计时器仍在运行,也会导致((TimerHandle) o).getTimer()(调用一个不断输出消息的@Timeout方法。