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

时间:2017-11-28 07:47:07

标签: java postgresql hibernate liferay liferay-service-builder

我跟着this tutorial尝试在DB中为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)

在服务构建器生成的代码中&#34; session.flush();&#34;:

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)

2 个答案:

答案 0 :(得分:0)

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

insert into _ZALACZNIK_BLOB_IMP (zalacznikBlobId) values ($1)

但稍后你刷新并关闭会话而不提交 - 这就是为什么你没有在db中进行更改,尝试在session.close()之前添加提交

例如,如果你有

Transaction tx = sess.beginTransaction();

添加

tx.commit();

also there some answers related

答案 1 :(得分: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);