2

私は次のエンティティを持っています:

1-アドレス

@Entity
@Table(name = "Address")
public class Address implements Serializable
{
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private AddressPK id = new AddressPK();

               @OneToOne(mappedBy = "address")
    private Insurance_Detail insuranceDetail;

}

2-アドレスPK

@Embeddable
public class AddressPK implements Serializable
{
    // default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(unique = false, nullable = false, length = 45)
    private String customer_EmailAddress;

    @Column(unique = true, nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int address_Id;

}

3- Insurance_Detail

@Entity
@Table(name = "Insurance_Detail")
public class Insurance_Detail implements Serializable
{
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private AddressPK id;

    @OneToOne
@JoinColumns({
        @JoinColumn(name = "customer_EmailAddress", referencedColumnName = "customer_EmailAddress", nullable = false, insertable = false, updatable = false),
        @JoinColumn(name = "address_Id", referencedColumnName = "address_Id", nullable = false, insertable = false, updatable = false) })



}

4-アドレスオブジェクトの永続化は次のとおりです。

Address address= new Address(data1,data2);

address.getId().setCustomer_EmailAddress(
                    currentCustomer.getEmailAddress());

entityManager.persist(address);

5-保険詳細オブジェクトの永続化は次のとおりです。

Insurance_Detail insuranceDetail = new Insurance_Detail(address.getId(), data3,data4);
entityManager.persist(insuranceDetail);

そしてそれは問題なく正しく保存されます。

問題:Insurance_Detailオブジェクトを永続化しようとすると、保険の最初の挿入では正常に永続化されますが、別の保険の2番目の挿入では、次の例外が発生します。

javax.servlet.ServletException: org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.ConstraintViolationException: Duplicate entry 'test@msn.com' for key 'customer_EmailAddress'; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Duplicate entry 'test@msn.com' for key 'customer_EmailAddress'
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
    org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)

この例外を修正する方法を教えてください。

4

1 に答える 1

0

ISSUEはデータベース側にあり、私はデータベース側にUNIQUE INDEXありcustomer_EmailAddress、主キーの順序は列の結合と同等である必要があります

PRIMARY KEY (`Customer_EmailAddress`, `Address_Id`)
于 2012-10-29T14:20:38.463 に答える