-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: ConstraintViolationException Duplicate entry @EmbeddedId
PostPosted: Mon Oct 29, 2012 8:56 am 
Newbie

Joined: Sun Oct 30, 2011 5:43 am
Posts: 13
i have the following entities:

1- ADDRESS

Code:
    @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

Code:
    @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

Code:
    @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 = "address_Id", referencedColumnName = "address_Id", nullable = false, insertable = false, updatable = false),
           @JoinColumn(name = "customer_EmailAddress", referencedColumnName = "customer_EmailAddress", nullable = false, insertable = false, updatable = false) })
   
    }


4- Persisting the address object is as follows:

Code:
    Address address= new Address(data1,data2);
   
    address.getId().setCustomer_EmailAddress(
                   currentCustomer.getEmailAddress());
   
    entityManager.persist(address);


5- Persisting the insurance details object is as follows:

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


and it's saved correctly with no problems.

ISSUE: when trying to persist the Insurance_Detail object, in first insertion for an insurance, it's persisted successfully, but in second insertion for another insurance i am getting the following exception.

Code:
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)


please advise how to fix this exception.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.