Hibernate SaveOrUpdate什么都不做

时间:2015-07-29 13:24:47

标签: spring hibernate spring-mvc

我使用Spring 3.2和Hibernate 3.6.7。

我有一个显示实体的表单,表单被提交并保存实体。使用saveOrUpdate方法。

直到我创建了我的实体类implements FieldHandled。 现在saveOrUpdate()什么都不做。

我阅读了Hibernate源代码,它说(快捷方式)如果实体实现了FieldHandled,并且FieldHandler没有脏,那么什么都不做。

这是我的代码:

@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
@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;
    }   
}

这是hibernate代码源

hibernate

!FieldInterceptionHelper.isInstrumented( entity ) = false,因为我的实体已被修改

FieldInterceptionHelper.extractFieldInterceptor( entity)返回我的实体的fieldHandler,这是脏的== false

true&& (false || false || false)= false

myfield handler

hibernate源代码:

as mightBeDirty == false对他来说更新不是必要的,也没有安排更新。

hibernate

我的问题: 当我在已经检测的分离实体上使用它时,我怎么能让saveOrUpdate()在数据库中真正更新?

由于

数据库配置:

<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>

1 个答案:

答案 0 :(得分:0)

我在保存前就这样做了: customerAddress.getFieldHandler().writeObject(customerAddress, "name", customerAddress.getName(), customerAddress.getName());

它会迫使脏到真。  我觉得当Entity实现FieldHandled时,它会阻止任何类型的自动运行时检测。我想我应该像这样手工实现所有的setter:

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

在每个setter中手动调用writeObject。