5

I have two entities with @OneToOne relationship:

@Entity
public class User implements Serializable {

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

    @OneToOne(cascade = CascadeType.MERGE, orphanRemoval=true)
    @JoinColumn(name = "id_details")
    private UserDetails details;
//...username, password, email
}

@Entity
public class UserDetails implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_user_details")
    private Long id;
//...firstname, lastname, address
}

First of all I save User with UserDetails:

public class Main {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();
        UserDetails details = new UserDetails("John", "Doe", "NYC 11");
        User user = new User("uname", "pass", "mail", details);
        em.merge(user);
        em.getTransaction().commit();

        em.close();
        emf.close();
    }
}

Everything works fine, since there is cascade MERGE, so both entities are saved. When I run another class:

public class MainUpdate {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu");
        EntityManager em = emf.createEntityManager();

        User user = em.find(User.class, 1L);
        user.setDetails(null);

        em.getTransaction().begin();
        em.merge(user);
        em.getTransaction().commit();

        em.close();
        emf.close();
    }
}

I expected that id_details in user will be set to null in the database (and it is) but since there is orphanRemoval=true I also expected that the orphan row will be removed from userdetails table but it is not.

What is interesting when I set the CascadeType PERSIST instead of MERGE the orphan is removed. Can someone explain why PERSIST works with orphanRemoval but MERGE does not?

GitHub repo: https://github.com/slwch/onetoone-orphan

Tested with Hibernate 5.0.6 and 4.1.8

swch
  • 1,432
  • 4
  • 21
  • 37
  • "When I run application again with this code....." Post that entire code block around loading setting to null and merging. – Alan Hay Feb 17 '16 at 12:06
  • Does any other entity reference `UserDetails`? – Dragan Bozanovic Feb 17 '16 at 12:08
  • No there are only these two objects. I added complete snippets – swch Feb 17 '16 at 12:22
  • Well there was a similar issue here http://stackoverflow.com/questions/20280271/hibernate-jpa-onetoone-orphan-removal-still-not-working-as-of-4-2-7-4-3-0-cr1versions which i tested and couldn't reporoduce under 4.1.8. Can you try with that version of Hibernate to see what happens? – Alan Hay Feb 17 '16 at 12:31
  • I tested on 4.1.8 and the problem still occurs. In my case I don't use LAZY initialization so this is not the case. I've also added the link to github repo – swch Feb 17 '16 at 13:21
  • @swch Have you solved the issue? I'm dealing with exactly same thing now. Using Hibernate 5.2.9.Final (latest) and LAZY + orphanRemoval doesn't work while calling merge() on entityManager. – shobull Apr 01 '18 at 20:47

0 Answers0