title: Detached entity passed to persist while using embedded class in entity
body: <p>As, I am reading the JPA (Java Persistence API). There I read about the Embeddable Classes. So, I am trying to use that concept in my entity class. But while using this I am facing a problem of detached entity.</p>
  
  <p>Here I have Embeddable Class(ZipCode) in Entity.</p>
  
  <p><strong>Embeddable Class</strong></p>
  
  <pre><code>@Embeddable public class ZipCode {    
  private String zip;    
  private String plusFour; 
  
  //getter &amp; setter also.
  }
  </code></pre>
  
  <p><strong>Entity Class</strong></p>
  
  <pre><code>@Entity
  @Table(name = "address")
  public class Address {
  
  @Id
  @GeneratedValue(strategy = IDENTITY)
  private long id;
  
  @Embedded
  private ZipCode zipCode;
  
  //getter &amp; setter also.
  
  }
  </code></pre>
  
  <p><strong>My persistence Code:</strong></p>
  
  <pre><code>@WebServlet("/RegisterServlet")
  public class RegisterServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
  @Inject
  private CrudService crudService;
  
  public RegisterServlet() {
      super();
  }
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.getWriter().append("Served at: ").append(request.getContextPath());
  }
  
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      ZipCode z = new ZipCode();
      z.setZip("bharti");
      z.setPlusFour("rawat");
  
      Address a = new Address();
      a.setId(1);
      a.setZipCode(z);
  
      try{
          crudService.create(a);
  
      }catch(Exception e){
          log.info("exception occurs "+ e.getMessage());
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>My CrudSrvice Code</strong>:</p>
  
  <pre><code>@Stateless
  @TransactionAttribute(TransactionAttributeType.REQUIRED)
  public class CrudServiceImpl implements CrudService {   
  @PersistenceContext(unitName = "localmedecube")
  private EntityManager em;
  
  public CrudServiceImpl() {
      // TODO Auto-generated constructor stub
  }
  
  @Override
  public &lt;T&gt; T create(T t) {
      this.em.persist(t);
      this.em.flush();
      this.em.refresh(t);
      return t;
  }
  
  @Override
  public void delete(@SuppressWarnings("rawtypes") Class type, Object id) {
      @SuppressWarnings("unchecked")
      Object ref = this.em.getReference(type, id);
      this.em.remove(ref);
  
  }
  
  @Override
  public &lt;T&gt; T update(T t) {
      return this.em.merge(t);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>When I try to persist my Address entity it is giving me error like this</p>
  
  <blockquote>
    <p>Caused by: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to
    persist: model.Address</p>
  </blockquote>
  
  <p>Please tell me why this exception is comes. Also provide me appropriate answer for it.</p>