I am writing Test cases for a webapp which requires me to mock controllers and verify results from URL hits. However, I am getting the error "detached entity passed to persist". The webapp however is live and working without errors meaning the problem is with my test.
I use the following test-context for hibernate
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="org.hibernate.envers.audit_table_prefix">history_</prop>
<prop key="org.hibernate.envers.audit_table_suffix"></prop>
<prop key="org.hibernate.envers.revision_type_field_name">_REV_TYPE</prop>
<prop key="org.hibernate.envers.revision_on_collection_change">false</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
</props>
</property>
The context doesn't work for a table in the database where it gives the above error on updating an object using persist. The table schema simplified is as follows
'id' int(11) NOT NULL AUTO_INCREMENT,
'code' varchar(64) NOT NULL,
'num_added' int(11) NOT NULL DEFAULT '0',
'created' datetime NOT NULL,
'updates' datetime NOT NULL,
PRIMARY KEY('id'),
UNIQUE KEY 'uq_code' ('code')
I have tried other answers on stackoverflow suggesting that id must not be present in the object being persisted as it leads hibernate to belive that object is in sync with database, however leaving id empty still gives same error. Using merge instead of persist gives an error that says
"Duplicate entry 'M923' for key 'uq_code' ; SQL [n/a]; constraint [null]"
I have tried refreshing the session after every URL hit like so
private void refresh(){
if(openSessionInViewFilter!=null){
openSessionInViewFilter.destroy();
openSessionInViewFilter = new OpenSessionInViewFilter();
openSessionInViewFilter.setServletContext(servletContext);
openSessionInViewFilter.setSessionFactoryBeanName("sessionFactory");
this.controller = MockMvcBuilders.standaloneSetup(Controller).addFilters(openSessionInViewFilter).build();
}
}
but the error remains the same. Similarly, turning session parameters singleSession and hibernate.transaction.auto_close_session to true/false also doesn't help. What is the problem here.