Hibernate的SaveOrUpdate无所事事 [英] Hibernate SaveOrUpdate doing nothing
问题描述
我使用Spring 3.2和Hibernate 3.6.7。
我有一个表单显示一个实体,表单被submited和实体保存。使用saveOrUpdate方法。
直到我让我的实体类实现FieldHandled
为止。
现在saveOrUpdate()什么也不做。
我读了Hibernate源代码,它说(快捷方式)如果实体实现FieldHandled并且FieldHandler不是
这里是我的代码:
@ Controller
@Transactional
public class CustomerContactController {
$ b $ ...
@ModelAttribute
public CustomerContactAddressDTO getCustomerContactAddress(final HttpServletRequest request){
CustomerContactAddressDTO customerContactAddressDTO = new CustomerContactAddressDTO();
customerAddress = customerAddressDao.findById(customerAddressUid);
customerContactAddressDTO.setCustomerContact(customerAddress.getCustomerContact());
返回customerContactAddressDTO;
$ b @Secured(UserService.ROLE_PREFIX +right.customer.contact.update)
@RequestMapping(value =/ customer / contact / formedit / {customerContactAddressDTOUid} ,method = RequestMethod.POST)
public @ResponseBody
ReturnFormResult formEditSubmit(@ModelAttribute CustomerContactAddressDTO customerContactAddressDTO,
@PathVariable final String customerContactAddressDTOUid){
CustomerAddress customerAddress = customerContactAddressDTO.getCustomerAddress();
ReturnFormResult rfr = new ReturnFormResult();
rfr.setMode(update);
rfr.setStatus(Boolean.TRUE);
customerAddressDao.saveOrUpdate(customerAddress);
return rfr;
code
$ p $实体:
@Entity
@Table(name =t_customer_contact_address)
公用类CustomerAddress implements FieldHandled {
@Id
@GeneratedValue(generator =uuid)
@GenericGenerator(name =uuid,strategy =uuid2)
@Column(name =customer_contact_address_uid,updatable = false)
私人字符串ID;
$ b $ ...
@Basic(fetch = FetchType.LAZY)
@Formula((some native sql query))
private String groupTransactionCodes;
私人FieldHandler fieldHandler;
@Override
public void setFieldHandler(FieldHandler handler){
this.fieldHandler = handler;
@Override
public FieldHandler getFieldHandler(){
return fieldHandler;
}
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
$ b $ public String getGroupTransactionCodes(){
if(fieldHandler!= null){
return(String)fieldHandler.readObject(this,groupTransactionCodes,groupTransactionCodes );
}
return groupTransactionCodes;
}
public void setGroupTransactionCodes(String groupTransactionCodes){
this.groupTransactionCodes = groupTransactionCodes;
这是hibernate代码源
!FieldInterceptionHelper.isInstrumented(entity)
= false因为我的实体被入侵
FieldInterceptionHelper.extractFieldInterceptor(entity)
返回我实体的fieldHandler这是脏==假
true&&& (false || false || false)= false
hibernate源代码:
as mightBeDirty == false对他来说更新不是必需的,也不会安排更新。
在每个设置器中手工调用writeObject。
I work with Spring 3.2 and Hibernate 3.6.7.
I have a form that display an entity, the form is submited and the entity is saved. Using the saveOrUpdate method.
It worked until I made my entity class implements FieldHandled
.
Now saveOrUpdate() does nothing.
I read the Hibernate source code and it says (shortcut) that if the Entity implements FieldHandled and if the FieldHandler is not dirty then does nothing.
here is my code :
@Controller
@Transactional
public class CustomerContactController {
....
@ModelAttribute
public CustomerContactAddressDTO getCustomerContactAddress(final HttpServletRequest request) {
CustomerContactAddressDTO customerContactAddressDTO = new CustomerContactAddressDTO();
customerAddress = customerAddressDao.findById(customerAddressUid);
customerContactAddressDTO.setCustomerContact(customerAddress.getCustomerContact());
return customerContactAddressDTO;
}
@Secured(UserService.ROLE_PREFIX + "right.customer.contact.update")
@RequestMapping(value = "/customer/contact/formedit/{customerContactAddressDTOUid}", method = RequestMethod.POST)
public @ResponseBody
ReturnFormResult formEditSubmit(@ModelAttribute CustomerContactAddressDTO customerContactAddressDTO,
@PathVariable final String customerContactAddressDTOUid) {
final CustomerAddress customerAddress = customerContactAddressDTO.getCustomerAddress();
ReturnFormResult rfr = new ReturnFormResult();
rfr.setMode("update");
rfr.setStatus(Boolean.TRUE);
customerAddressDao.saveOrUpdate(customerAddress);
return rfr;
}
}
Entity :
@Entity
@Table(name = "t_customer_contact_address")
public class CustomerAddress implements FieldHandled{
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "customer_contact_address_uid", updatable = false)
private String id;
....
@Basic(fetch=FetchType.LAZY)
@Formula("(some native sql query)")
private String groupTransactionCodes;
private FieldHandler fieldHandler;
@Override
public void setFieldHandler(FieldHandler handler) {
this.fieldHandler = handler;
}
@Override
public FieldHandler getFieldHandler() {
return fieldHandler;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGroupTransactionCodes() {
if(fieldHandler!=null){
return (String)fieldHandler.readObject(this, "groupTransactionCodes", groupTransactionCodes);
}
return groupTransactionCodes;
}
public void setGroupTransactionCodes(String groupTransactionCodes) {
this.groupTransactionCodes = groupTransactionCodes;
}
}
this is hibernate code source
!FieldInterceptionHelper.isInstrumented( entity )
= false because my entity is intrumented
FieldInterceptionHelper.extractFieldInterceptor( entity)
return the fieldHandler of my Entity which is dirty == false
true && ( false || false || false) = false
hibernate source code :
as mightBeDirty == false for him update is notNecessary and doesn't schedule update.
My question : How could I have make the saveOrUpdate() really update in database when I use it on detached entity wich are instrumented ?
Thanks
database configuration :
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan" value="com.XXXX.db.pojo"/>
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">com.XXXX.application.utils.MSSQLAHEADDialect</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.use_sql_comments">false</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
<property name="eventListeners">
<map>
<entry key="save-update" value-ref="saveEventListener" />
<entry key="flush-entity" value-ref="flushEntityEventListener" />
<entry key="post-load" value-ref="postLoadEventListener" />
</map>
</property>
</bean>
<bean id="saveEventListener" parent="callbackHandlerEventListener" class="org.hibernate.ejb.event.EJB3SaveOrUpdateEventListener" />
<bean id="flushEntityEventListener" parent="callbackHandlerEventListener" class="org.hibernate.ejb.event.EJB3FlushEntityEventListener" />
<bean id="postLoadEventListener" parent="callbackHandlerEventListener" class="org.hibernate.ejb.event.EJB3PostLoadEventListener" />
<bean id="entityCallbackHandler" class="org.hibernate.ejb.event.EntityCallbackHandler" />
<bean id="callbackHandlerEventListener" abstract="true" class="org.hibernate.ejb.event.CallbackHandlerConsumer">
<property name="callbackHandler" ref="entityCallbackHandler" />
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/dataSourceAHEAD"/>
</bean>
I hacked by just doing this before saving :
customerAddress.getFieldHandler().writeObject(customerAddress, "name", customerAddress.getName(), customerAddress.getName());
it will force dirty to becode true. I feel like when a Entity implements FieldHandled, It prevent any kind of automatic runtime instrumentation.I think I should implement all the setter by hand like this :
public void setXXX(String xxx) {
getFieldHandler().writeObject(this, "xxx", this.xxx, xxx);
this.xxx = xxx;
}
calling writeObject by hand in each setter.
这篇关于Hibernate的SaveOrUpdate无所事事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!