liferay 7 生成的代码:blob 未写入 postgresql

liferay 7 generated code: blob not written to postgresql

我按照 this tutorial 尝试在数据库中为 liferay portlet 实现文件存储(使用 liferay-ce-portal-tomcat-7.0-ga5 和 PostgreSQL 9.6)。 Service.xml:

<entity name="MyBlob" local-service="true" table="_MY_BLOB_IMP" human-name="My blobs">
    <column name="myBlobId" type="long" primary="true"
        id-type="increment">
    </column>
    <column name="data" type="Blob"></column>
</entity>

Portlet.java:

long blobId = CounterLocalServiceUtil.increment(MyBlob.class.getName());
MyBlob blob = _myBlobLocalService.createMyBlob(blobid);
blob.setData(blobData);
//_myBlobLocalService.addMyBlob(blob);
MyBlobLocalServiceUtil.addMyBlob(blob);

我收到错误:

org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
    at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:85)
    at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
    at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at com.liferay.portal.dao.orm.hibernate.event.NestableFlushEventListener.onFlush(NestableFlushEventListener.java:61)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at com.liferay.portal.dao.orm.hibernate.SessionImpl.flush(SessionImpl.java:173)
    at com.liferay.portal.kernel.dao.orm.ClassLoaderSession.flush(ClassLoaderSession.java:282)

在 "session.flush();" 行由服务构建器生成的代码中:

try {
            session = openSession();

            if (myBlob.isNew()) {
                session.save(myBlob);

                myBlob.setNew(false);
            }
            else {
                session.evict(myBlob);
                session.saveOrUpdate(myBlob);
            }

            session.flush();

            session.clear();
        }

这是 postgres 日志:

2017-11-27 14:55:35 CET [20752]: [4131-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 LOG:  execute S_6: select resourcepe0_.resourcePermissionId as resource1_269_, resourcepe0_.mvccVersion as mvccVers2_269_, resourcepe0_.companyId as companyId269_, resourcepe0_.name as name269_, resourcepe0_.scope as scope269_, resourcepe0_.primKey as primKey269_, resourcepe0_.primKeyId as primKeyId269_, resourcepe0_.roleId as roleId269_, resourcepe0_.ownerId as ownerId269_, resourcepe0_.actionIds as actionIds269_, resourcepe0_.viewActionId as viewAct11_269_ from ResourcePermission resourcepe0_ where (resourcepe0_.companyId= )AND(resourcepe0_.name= )AND(resourcepe0_.scope= )AND(resourcepe0_.primKey= )AND(resourcepe0_.roleId= )
2017-11-27 14:55:35 CET [20752]: [4132-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 DETAIL:  parameters:  = '20116',  = 'Zalacznik',  = '1',  = '20116',  = '20123'
2017-11-27 14:55:35 CET [20752]: [4133-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 LOG:  execute S_6: select resourcepe0_.resourcePermissionId as resource1_269_, resourcepe0_.mvccVersion as mvccVers2_269_, resourcepe0_.companyId as companyId269_, resourcepe0_.name as name269_, resourcepe0_.scope as scope269_, resourcepe0_.primKey as primKey269_, resourcepe0_.primKeyId as primKeyId269_, resourcepe0_.roleId as roleId269_, resourcepe0_.ownerId as ownerId269_, resourcepe0_.actionIds as actionIds269_, resourcepe0_.viewActionId as viewAct11_269_ from ResourcePermission resourcepe0_ where (resourcepe0_.companyId= )AND(resourcepe0_.name= )AND(resourcepe0_.scope= )AND(resourcepe0_.primKey= )AND(resourcepe0_.roleId= )
2017-11-27 14:55:35 CET [20752]: [4134-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 DETAIL:  parameters:  = '20116',  = 'Zalacznik',  = '2',  = '20143',  = '20123'
2017-11-27 14:55:35 CET [20752]: [4135-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 LOG:  execute S_6: select resourcepe0_.resourcePermissionId as resource1_269_, resourcepe0_.mvccVersion as mvccVers2_269_, resourcepe0_.companyId as companyId269_, resourcepe0_.name as name269_, resourcepe0_.scope as scope269_, resourcepe0_.primKey as primKey269_, resourcepe0_.primKeyId as primKeyId269_, resourcepe0_.roleId as roleId269_, resourcepe0_.ownerId as ownerId269_, resourcepe0_.actionIds as actionIds269_, resourcepe0_.viewActionId as viewAct11_269_ from ResourcePermission resourcepe0_ where (resourcepe0_.companyId= )AND(resourcepe0_.name= )AND(resourcepe0_.scope= )AND(resourcepe0_.primKey= )AND(resourcepe0_.roleId= )
2017-11-27 14:55:35 CET [20752]: [4136-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 DETAIL:  parameters:  = '20116',  = 'Zalacznik',  = '4',  = 'Zalacznik',  = '20123'
2017-11-27 14:55:35 CET [20752]: [4137-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 LOG:  execute S_10: BEGIN
2017-11-27 14:55:35 CET [20752]: [4138-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 LOG:  execute <unnamed>: insert into _ZALACZNIK_BLOB_IMP (zalacznikBlobId) values ()
2017-11-27 14:55:35 CET [20752]: [4139-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 DETAIL:  parameters:  = '4'
2017-11-27 14:55:35 CET [20752]: [4140-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 LOG:  fastpath function call: "lo_creat" (OID 957)
2017-11-27 14:55:35 CET [20752]: [4141-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 LOG:  fastpath function call: "lo_open" (OID 952)
2017-11-27 14:55:35 CET [20752]: [4142-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 LOG:  fastpath function call: "lowrite" (OID 955)
2017-11-27 14:55:35 CET [20752]: [4143-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 LOG:  fastpath function call: "lowrite" (OID 955)
2017-11-27 14:55:35 CET [20752]: [4144-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 LOG:  fastpath function call: "lowrite" (OID 955)
(  .  .  .  )
2017-11-27 14:55:35 CET [20752]: [4179-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 LOG:  fastpath function call: "lowrite" (OID 955)
2017-11-27 14:55:35 CET [20752]: [4180-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 LOG:  fastpath function call: "lo_close" (OID 953)
2017-11-27 14:55:35 CET [20752]: [4181-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 LOG:  execute <unnamed>: update _MY_BLOB_IMP set data= where myBlobId=
2017-11-27 14:55:35 CET [20752]: [4182-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 DETAIL:  parameters:  = '23373',  = '204'
2017-11-27 14:55:35 CET [20752]: [4183-1] user=liferay_umsw,db=umswportal,app=PostgreSQL JDBC Driver,client=127.0.0.1 LOG:  execute S_33: ROLLBACK

示例中的代码是否不再对 liferay 7 有效,我应该以其他方式为 postgre 实现 blob 还是我犯了其他错误?

谢谢,

更新: 删除 session.flush() 和 session.clear() 没有帮助,它将错误移至提交...

at com.liferay.portal.spring.hibernate.PortletTransactionManager$TransactionStatusWrapper.reset(PortletTransactionManager.java:260)
    at com.liferay.portal.spring.hibernate.PortletTransactionManager.commit(PortletTransactionManager.java:63)
    at com.liferay.portal.spring.transaction.DefaultTransactionExecutor._commit(DefaultTransactionExecutor.java:128)
    at com.liferay.portal.spring.transaction.DefaultTransactionExecutor.commit(DefaultTransactionExecutor.java:36)
    at com.liferay.portal.spring.transaction.DefaultTransactionExecutor.execute(DefaultTransactionExecutor.java:62)
    at com.liferay.portal.spring.transaction.TransactionInterceptor.invoke(TransactionInterceptor.java:58)
    at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:137)

您之前开始交易(我在日志中看到)

insert into _ZALACZNIK_BLOB_IMP (zalacznikBlobId) values ()

但稍后您在未提交的情况下刷新并关闭会话 - 这就是为什么您的更改未保存在数据库中的原因,请尝试在 session.close()

之前添加提交

例如,如果您有

Transaction tx = sess.beginTransaction();

添加

tx.commit();

also there some answers related

这看起来像是你的自动增量有问题。

你可以做两件事

1) 检查您的数据库 table (_MY_BLOB_IMP ) 是否设置了自动增量。

2) 更改您的 service.xml 并删除 id-type="increment" 并重新生成服务。

并更新您的代码..

long blobId = CounterLocalServiceUtil.increment(MyBlob.class.getName());
MyBlob blob = _myBlobLocalService.createMyBlob(blobid);
blob.setData(blobData);
//_myBlobLocalService.addMyBlob(blob);
MyBlobLocalServiceUtil.addMyBlob(blob);