Liferay Service Builder没有更新BLOB



我试图让我的Liferay Service更新BLOB类型的列。持久化各自的实体可以正常工作,删除也可以。更新不会抛出异常,而只是返回旧值,即blob类型的字段。

我的设置:

  • Liferay 6.1 CE GA3捆绑Tomcat
  • Liferay插件SDK for 6.1 GA3
  • 5.6.19 MySQL社区服务器
  • hibernate3.jar (from https://issues.liferay.com/browse/LPS-42478)

我在Eclipse中的步骤:

-创建一个新的Liferay Portlet项目

-创建一个新的Liferay Service Builder。Test, namespace my_service)

编辑service . xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="com.test">
    <author>fe</author>
    <namespace>my_service</namespace>
    <entity name="Foo" local-service="true" remote-service="true" cache-enabled="false">
        <column name="fooId" type="long" primary="true" />
        <column name="companyId" type="long" />
        <column name="userId" type="long" />
        <column name="userName" type="String" />
        <column name="createDate" type="Date" />
        <column name="modifiedDate" type="Date" />
        <column name="field1" type="Blob" />
        <order by="asc">
            <order-column name="fooId" />
        </order>
    </entity>
</service-builder>

ant构建服务

编辑FooLocalServiceImpl.java:

public Foo addFoo(User user, String data, ServiceContext serviceContext) throws PortalException, SystemException {
    Date now = new Date();
    long fooId = counterLocalService.increment(Foo.class.getName());
    Foo foo = fooPersistence.create(fooId);
    foo.setCompanyId(user.getCompanyId());
    foo.setUserId(user.getUserId());
    foo.setCreateDate(serviceContext.getCreateDate(now));
    foo.setModifiedDate(serviceContext.getModifiedDate(now));
    Blob blob = null;
    try {
        blob = new SerialBlob(data.getBytes());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    foo.setField1(blob);
    super.addFoo(foo);
    return foo;
}
public Foo updateFoo(User user, long fooId, String data, ServiceContext serviceContext) throws PortalException, SystemException {
    Date now = new Date();
    Foo foo = FooLocalServiceUtil.fetchFoo(fooId);
    foo.setCachedModel(false);
    foo.setModifiedDate(serviceContext.getModifiedDate(now));
    Blob blob = null;
    try {
        blob = new SerialBlob(data.getBytes());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    foo.setField1(blob);
    super.updateFoo(foo);
    return foo;
}

ant构建服务

-从portlet-hbm.xml中删除重复条目

编辑view.jsp:

<%
    User user = PortalUtil.getUser(request);
    ServiceContext serviceContext = ServiceContextFactory.getInstance(request);
    Foo foo = FooLocalServiceUtil.addFoo(user, "This is some data", serviceContext);
    long fooId = foo.getFooId();
%>
We added a Foo (<%=foo.getModifiedDate().getTime() %>)<br>
<%
    Blob blob = FooLocalServiceUtil.fetchFoo(fooId).getField1();
    byte[] bdata = blob.getBytes(1, (int) blob.length());
    String data = new String(bdata);        
%>
It contains the data "<%=data%>"<br>
<%
    foo = FooLocalServiceUtil.updateFoo(user, fooId, "This is some updated data", serviceContext);
    fooId = foo.getFooId();
%>
Now we updated the Foo (<%=foo.getModifiedDate().getTime() %>)<br>
<%
    blob = FooLocalServiceUtil.fetchFoo(fooId).getField1();
    bdata = blob.getBytes(1, (int) blob.length());
    data = new String(bdata);       
%>
It contains the data "<%=data%>"<br>

-设置门户属性:

hibernate.jdbc.batch_size=0
hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider
hibernate.cache.use_query_cache=false
hibernate.cache.use_second_level_cache=false

-通过Eclipse部署portlet

不幸的是,BLOB列似乎没有得到更新,而modifiedDate得到了更新。

当我检查DB时,我得到:

mysql> select * from my_service_Foo;
+-------+-----------+--------+----------+---------------------+---------------------+-------------------+
| fooId | companyId | userId | userName | createDate          | modifiedDate        | field1            |
+-------+-----------+--------+----------+---------------------+---------------------+-------------------+
|     1 |     10153 |  10406 |          | 2014-06-24 07:06:37 | 2014-06-24 07:06:37 | This is some data |
+-------+-----------+--------+----------+---------------------+---------------------+-------------------+

我也有同样的问题。我在update方法调用中做了一些更改。

这是你可以在updateFoo方法

中做的更改

super.updateFoo (foo,假);

方法调用中的第二个参数将布尔合并设置为false,这将强制执行会话。saveOrUpdate语句,将在BatchSessionImpl.

的update()方法中跳过session.merge(foo)语句。

最新更新