我试图让我的 Liferay 服务更新 BLOB 类型的列。保留相应的实体可以正常工作,删除也是如此。更新不会引发异常,而只是返回旧值是 BLOB 类型的字段。
我的设置:
- Liferay 6.1 CE GA3 与 Tomcat 捆绑
- 适用于 6.1 GA3 的 Liferay 插件 SDK
- 5.6.19 MySQL 社区服务器
- hibernate3.jar(来自https://issues.liferay.com/browse/LPS-42478)
到目前为止,我在 Eclipse 中的步骤:
- 创建一个新的 Liferay Portlet 项目
- 创建一个新的 Liferay 服务构建器(包 com.test,命名空间 my_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 中删除重复条目
-编辑视图.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 得到了更新。
当我检查数据库时,我得到:
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 |
+-------+-----------+--------+----------+---------------------+---------------------+-------------------+