我有以下实体:
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- ADDRESSPK
@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 对象时,在首次插入保险时,它已成功保留,但在第二次插入时另一项保险我得到以下例外。
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)
请告知如何修复此异常。
答案 0 :(得分:0)
ISSUE在数据库方面,UNIQUE INDEX
上有customer_EmailAddress
,主键顺序必须等同于连接列
PRIMARY KEY (`Customer_EmailAddress`, `Address_Id`)