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>