0

我按照本教程尝试在 DB 中为 liferay portlet 实现文件存储(使用 liferay-ce-portal-tomcat-7.0-ga5 和 PostgreSQL 9.6)。服务.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=$1 )AND(resourcepe0_.name=$2 )AND(resourcepe0_.scope=$3 )AND(resourcepe0_.primKey=$4 )AND(resourcepe0_.roleId=$5 )
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: $1 = '20116', $2 = 'Zalacznik', $3 = '1', $4 = '20116', $5 = '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=$1 )AND(resourcepe0_.name=$2 )AND(resourcepe0_.scope=$3 )AND(resourcepe0_.primKey=$4 )AND(resourcepe0_.roleId=$5 )
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: $1 = '20116', $2 = 'Zalacznik', $3 = '2', $4 = '20143', $5 = '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=$1 )AND(resourcepe0_.name=$2 )AND(resourcepe0_.scope=$3 )AND(resourcepe0_.primKey=$4 )AND(resourcepe0_.roleId=$5 )
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: $1 = '20116', $2 = 'Zalacznik', $3 = '4', $4 = 'Zalacznik', $5 = '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 ($1)
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: $1 = '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=$1 where myBlobId=$2
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: $1 = '23373', $2 = '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)
4

2 回答 2

0

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

insert into _ZALACZNIK_BLOB_IMP (zalacznikBlobId) values ($1)

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

例如,如果你有

Transaction tx = sess.beginTransaction();

添加

tx.commit();

还有一些相关的答案

于 2017-11-28T08:17:00.353 回答
0

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

你可以做两件事

1)检查您的数据库表(_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);
于 2017-11-28T08:36:12.627 回答