从JPA 1.0迁移到JPA 2.0后存储映像BLOB

时间:2018-03-13 10:53:12

标签: java jpa websphere jpa-2.0 openjpa

我们正在迁移过程中将一些应用程序迁移到WAS自由配置文件,应用程序使用的是开放的jpa版本1和java 6,并且代码工作得很好,我们决定迁移到JPA 2.0和java版本8,因为它是在apache open jpa中实现的,以避免迁移问题,现在应用程序无法在oracle DB中保存图片,它存储为表中的blob数据类型,我们能够正确地从数据库中检索图像但是在提交存储图像查询时,由于以下错误,无法在数据库中存储任何内容。我注意到它无法为blob数据类型字段获取占位符。

现场声明:

@Lob
@Column(name="FORM" ,columnDefinition="BLOB" ,  nullable=true )
private byte[] form;

存储交易

  @Action(Action.ACTION_TYPE.CREATE)
public void createTransaction(final Transaction transaction) throws Exception {
    EntityManager em = getEntityManager();
    try {
        em.getTransaction().begin();
        em.persist(transaction);
        em.getTransaction().commit();
    } catch (Exception ex) {
        try {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
        } catch (Exception e) {
            ex.printStackTrace();
            throw e;
        }
        throw ex;
    } finally {
        em.close();
        this.emf.close();
        this.emf = null;
    }
}

JPA项目设置: 平台:Generic 2.0,JPA实现:目标运行时提供的库。

异常堆栈跟踪:

       [err] <openjpa-2.2.3-SNAPSHOT-r422266:1802153 fatal store error> org.apache.openjpa.persistence.RollbackException: The transaction has been rolled back.  See the nested exceptions for details on the errors that occurred.
[err]   at org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:594)
[err]   at com.test.hace.sfp.dao.entities.controller.TransactionManager.createTransaction(TransactionManager.java:92)
[err]   at com.test.hace.sfp.services.process.impl.TransactionServiceImpl.submitNewTransactionToApproval(TransactionServiceImpl.java:115)
[err]   at pagecode.pages.branch.initiator.NewTransactionStep2.doSubmitAction(NewTransactionStep2.java:229)
[err]   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[err]   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[err]   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[err]   at java.lang.reflect.Method.invoke(Method.java:497)
[err]   at org.apache.el.parser.AstValue.invoke(AstValue.java:245)
[err]   at [internal classes]
[err]   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
[err]   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
[err]   at java.lang.Thread.run(Thread.java:745)
[err] Caused by: <openjpa-2.2.3-SNAPSHOT-r422266:1802153 fatal general error> org.apache.openjpa.persistence.PersistenceException: The transaction has been rolled back.  See the nested exceptions for details on the errors that occurred.
[err]   at org.apache.openjpa.kernel.BrokerImpl.newFlushException(BrokerImpl.java:2352)
[err]   at [internal classes]
[err]   ... 45 more
[err] Caused by: <openjpa-2.2.3-SNAPSHOT-r422266:1802153 fatal general error> org.apache.openjpa.persistence.PersistenceException: oracle.sql.BLOB
[err]   at org.apache.openjpa.jdbc.sql.DBDictionary.narrow(DBDictionary.java:4998)
[err]   at [internal classes]
[err]   ... 52 more
[err] Caused by: java.sql.SQLException: oracle.sql.BLOB
[err]   at org.apache.openjpa.jdbc.sql.OracleDictionary.getEmptyBlob(OracleDictionary.java:1249)
[err]   at [internal classes]
[err]   ... 59 more

1 个答案:

答案 0 :(得分:0)

对于某些操作(特别是涉及LOB),JPA Providers(OpenJPA,Eclipselink)将要求应用程序的类加载器(更具体地说,线程上下文类加载器,即app类加载器)可以解析JDBC类。

问题是JNDI查找解析的javax.sql.DataSource没有自动将这些JDBC类添加到应用程序类加载器(你真的不想污染应用程序&#39; ClassLoader with毕竟不必要的图书馆。)

但是,如果您将应用程序的JDBC库定义为共享库,并将其作为应用程序在server.xml中的条目定义,那么它将起作用。

例如:

    <jdbcDriver id="Derby" libraryRef="JDBCLib"/>
    <library filesetRef="DerbyFileset" id="JDBCLib"/>
    <fileset dir="/derby/lib" id="DerbyFileset"/>

    <dataSource id="jdbc/JPA_DS"
                    jndiName="jdbc/JPA_DS"
                    jdbcDriverRef="Derby"
                    isolationLevel="TRANSACTION_READ_COMMITTED">
                    <properties.derby.client 
                        databaseName="TestDB"
                        serverName="localhost" 
                        portNumber="1527"/>
    </dataSource>

    <application type="ear" id="entity10"         name="Entity10"
       location="${fvt.home}/testbuckets/jpaspec-1.0/Entity/Entity10.ear">
       <classloader commonLibraryRef="JDBCLib" />
    </application>