我试图让我的Liferay服务更新BLOB类型的列。保持相应实体的工作正常,删除也是如此。更新不会抛出异常,但只需返回旧值就是BLOB类型的字段。
我的设置:
到目前为止我在Eclipse中的步骤:
- 创建一个新的Liferay Portlet项目
- 创建一个新的Liferay Service Builder(包com.test,命名空间my_service)
-edit 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 build-service
-edit 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 build-service
- 从portlet-hbm.xml中删除重复条目
-edit 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>
-set portal properties:
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
-Deploy portlet via Eclipse
不幸的是,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 |
+-------+-----------+--------+----------+---------------------+---------------------+-------------------+
答案 0 :(得分:0)
我也有同样的问题。我在更新方法调用中做了一些更改。
以下是您在 updateFoo 方法
中可以执行的更改super.updateFoo(FOO,FALSE);
方法调用中的第二个参数将布尔合并设置为false,这将强制执行 session.saveOrUpdate 语句,并将在的upadate()方法中跳过session.merge(foo)语句BatchSessionImpl 强>