我的JPA @OneToOne关系导致在目标实体上插入而不是更新

时间:2013-04-08 08:05:40

标签: jpa spring-data-jpa

我有一个与Member实体有@OneToOne关系的Address实体,如下所示:

Member实体中:

@OneToOne(cascade=CascadeType.ALL)
private Address address;

Address实体:

@RooJpaEntity
public class Address {

    private String formattedAddress;
    private double latitude;
    private double longitude;
}

我遇到的问题是每次更新会员的地址如下:

public void modifyAddress(Member member, Address address){
        member.setAddress(address);
        memberRepository.save(member);
    }

... 而不是更新地址表中的行,而是在地址表中插入一个新行并更新成员表中的FK ,如下面给出的sql所示:

生成的SQL:

Hibernate: 
    /* insert com.bignibou.domain.Address
        */ insert 
        into
            address
            (formatted_address, latitude, longitude, version) 
        values
            (?, ?, ?, ?)
Hibernate: 
    /* update
        com.bignibou.domain.Member */ update
            member 
        set
            address=?,
            version=? 
        where
            id=? 
            and version=?

我不确定如何解决问题(我现在不想在成员表中内联Adress数据)。我希望我的应用更新地址表,而不是在该表中插入新行。

任何人都可以提出建议吗?

编辑1

我修改了我的实体,如下所示:

以下是完整的Address实体:

@RooJpaEntity
public class Address {

    private String formattedAddress;
    private double latitude;
    private double longitude;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "address")
    private Member member;
}

以下是Member实体中的相关属性:

@OneToOne
private Address address;

我的方法:

@Override
public void modifyAddress(Member member, Address address){
    address.setMember(member);
    updateAddress(address);
}

我的应用的行为没有改变......

编辑2 :如果我按如下方式修改应用,则会收到StaleObjectStateException。

@Override
    public void modifyAddress(Member member, Address address){
        long addressId = member.getAddress().getId();
        address.setId(addressId);
        address.setMember(member);
        updateAddress(address);
    }

地址:

@RooJpaEntity
public class Address {

    private String formattedAddress;
    private double latitude;
    private double longitude;

    @OneToOne
    private Member member;
}

会员:

@OneToOne
private Address address;

StaleObjectStateException:

org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.bignibou.domain.Address#5]
    org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:303)
    org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151)
    org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:76)
    org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:903)
    org.hibernate.internal.SessionImpl.merge(SessionImpl.java:887)
    org.hibernate.internal.SessionImpl.merge(SessionImpl.java:891)
    org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:879)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:366)
    com.sun.proxy.$Proxy49.merge(Unknown Source)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241)
    com.sun.proxy.$Proxy48.merge(Unknown Source)
    org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:353)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:333)
    org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:318)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
    org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:92)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    com.sun.proxy.$Proxy65.save(Unknown Source)
    com.bignibou.service.PreferencesServiceImpl_Roo_Service.ajc$interMethod$com_bignibou_service_PreferencesServiceImpl_Roo_Service$com_bignibou_service_PreferencesServiceImpl$updateAddress(PreferencesServiceImpl_Roo_Service.aj:53)
    com.bignibou.service.PreferencesServiceImpl.updateAddress(PreferencesServiceImpl.java:1)
    com.bignibou.service.PreferencesServiceImpl_Roo_Service.ajc$interMethodDispatch1$com_bignibou_service_PreferencesServiceImpl_Roo_Service$com_bignibou_service_PreferencesServiceImpl$updateAddress(PreferencesServiceImpl_Roo_Service.aj)
    com.bignibou.service.PreferencesServiceImpl.modifyAddress_aroundBody12(PreferencesServiceImpl.java:100)
    com.bignibou.service.PreferencesServiceImpl$AjcClosure13.run(PreferencesServiceImpl.java:1)
    org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96cproceed(AbstractTransactionAspect.aj:59)
    org.springframework.transaction.aspectj.AbstractTransactionAspect$AbstractTransactionAspect$1.proceedWithInvocation(AbstractTransactionAspect.aj:65)
    org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
    org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96c(AbstractTransactionAspect.aj:63)
    com.bignibou.service.PreferencesServiceImpl.modifyAddress(PreferencesServiceImpl.java:93)
    com.bignibou.controller.PreferenceController.modifyAddress(PreferenceController.java:168)

编辑3

地址的哈希码:

public int hashCode() {
        return new HashCodeBuilder().append(formattedAddress).append(getId()).append(latitude).append(longitude).toHashCode();
    }

地址等于(来自ITD):

public boolean Address.equals(Object obj) {
        if (!(obj instanceof Address)) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        Address rhs = (Address) obj;
        return new EqualsBuilder().append(formattedAddress, rhs.formattedAddress).append(id, rhs.id).append(latitude, rhs.latitude).append(longitude, rhs.longitude).append(member, rhs.member).isEquals();
    }

ITD for Address entity:

privileged aspect Address_Roo_Jpa_Entity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long Address.id;

    @Version
    @Column(name = "version")
    private Integer Address.version;

    public Long Address.getId() {
        return this.id;
    }

    public void Address.setId(Long id) {
        this.id = id;
    }

    public Integer Address.getVersion() {
        return this.version;
    }

    public void Address.setVersion(Integer version) {
        this.version = version;
    }

}

顺便说一句,ITD只是方面。

编辑4 :我已在此为您设置示例应用:https://github.com/balteo/sample-app-gab

1 个答案:

答案 0 :(得分:1)

(错误的原始诊断我删除,我让链接无论如何因为它们仍然是有益的)

请参阅:http://www.objectdb.com/api/java/jpa/OneToOneJPA Hibernate One-to-One relationship以及http://www.techavalanche.com/2012/05/10/one-to-one-unidirectional-mapping-using-foreign-key/

编辑:

  • 实际上你得到StaleObjectStateException,因为你正在修改现有实体的主键
  • 如果您恢复原始(和正确)配置(其中成员拥有单向OneToOne关系)。
     您可以通过保存成员执行以下操作来更新地址表:
    • 使用地址a0保存成员m0
    • 齐平
    • 检索m0.a0并编辑值
    • 保存m0
    要么
    • 保存没有地址m1的成员
    • 保存地址a1
    • 齐平
    • 将a1附加到m1,保存m1
    要么
    • 使用地址a2保存会员m2
    • 齐平
    • 将m2地址设置为null
    • 保存m2,这里a2将更新并保持不带关联成员(孤儿),你可以使用delete-orphan选项强制关闭这种情况下的a2删除

Ofc,只有在同一事务中进行操作时才需要刷新调用。

所有这些都可以与您的示例应用程序一起运行。

请注意一个有趣的事情,OneToOne关系并不能确保地址和成员之间的单一性。 (您可以有2个成员引用相同的地址)。您必须使用@JoinColumn(name = "address", unique=true)

显式唯一约束