Hibernate的SaveOrUpdate无所事事 [英] Hibernate SaveOrUpdate doing nothing

查看:149
本文介绍了Hibernate的SaveOrUpdate无所事事的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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对他来说更新不是必需的,也不会安排更新。




我的问题:
当我使用它时,我怎样才能让saveOrUpdate()真的在数据库中更新?数据库配置:

$ b

谢谢

数据库配置:

 < bean id =sessionFactoryclass =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean> 
< property name =packagesToScanvalue =com.XXXX.db.pojo/>
< property name =dataSourceref =dataSource/>
< property name =hibernateProperties>
<道具>
< 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>验证< / 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>
< /道具>
< / property>
< property name =eventListeners>
< map>
< entry key =save-updatevalue-ref =saveEventListener/>
< entry key =flush-entityvalue-ref =flushEntityEventListener/>
< entry key =post-loadvalue-ref =postLoadEventListener/>

< / map>
< / property>
< / bean>

< bean id =saveEventListenerparent =callbackHandlerEventListenerclass =org.hibernate.ejb.event.EJB3SaveOrUpdateEventListener/>

< bean id =flushEntityEventListenerparent =callbackHandlerEventListenerclass =org.hibernate.ejb.event.EJB3FlushEntityEventListener/>

< bean id =postLoadEventListenerparent =callbackHandlerEventListenerclass =org.hibernate.ejb.event.EJB3PostLoadEventListener/>

< bean id =entityCallbackHandlerclass =org.hibernate.ejb.event.EntityCallbackHandler/>

< bean id =callbackHandlerEventListenerabstract =trueclass =org.hibernate.ejb.event.CallbackHandlerConsumer>
< property name =callbackHandlerref =entityCallbackHandler/>
< / bean>

< bean id =dataSourceclass =org.springframework.jndi.JndiObjectFactoryBean>
< property name =jndiNamevalue =java:comp / env / jdbc / dataSourceAHEAD/>
< / bean>


$ b customerAddress.getFieldHandler()。writeObject(customerAddress,name,customerAddress.getName(),customerAddress.getName());

它会强制肮脏的事情变成现实。
我觉得当一个实体实现FieldHandled时,它会阻止任何类型的自动运行时检测。我想我应该像这样手动执行所有setter:

 public void setXXX(String xxx){
getFieldHandler()。writeObject(this,xxx,this.xxx,xxx);
this.xxx = xxx;
}

在每个设置器中手工调用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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆