1 Reply Latest reply on Dec 1, 2009 2:08 PM by I need a real name

    How-to remove entity with @OneToOne relationship

    I need a real name Newbie

      Hello!


      I try to delete an entity User which is in an
      @OneToOne relation with an entity Profile.


      Since I'm using CascadeType.ALL I would expect
      that when I call the EntityManager to remove
      a User entity its related Profile entity
      should be removed, too?


      But in my case only the user entity is removed
      from the database. Please can you help me?



      User u = (User) entityManager.find(User.class, id);          
      entityManager.remove(u);          
      entityManager.flush();









      @Entity
      @Scope(SESSION)
      @Name("user")
      public class User implements Serializable {
           
           private Long id;
           private Profile profile;
           
           @Id @GeneratedValue
           public Long getId() {
                return id;
           }
      
           public void setId(Long id) {
                this.id = id;
           }
           
           @OneToOne(cascade = CascadeType.ALL)
              @PrimaryKeyJoinColumn
           public Profile getProfile() {
                return profile;
           }
           
           public void setProfile(Profile profile) {
                this.profile = profile;
           }     
      }









      @Entity
      @Scope(SESSION)
      @Name("profile")
      public class Profile implements Serializable {
           
           private Long id;
           
           @Id @GeneratedValue
           public Long getId() {
                return id;
           }
      
           public void setId(Long id) {
                this.id = id;
           }
      }





        • 1. Re: How-to remove entity with @OneToOne relationship
          I need a real name Newbie

          Ok I guess my problem is solved. The reason was that
          the entity ids in the database tables were messed up
          because of previous delete operations.


          So although I persisted both entities with a @OneToOne
          shared primary key relationship they received different
          id numbers thus preventing the cascaded remove to work
          properly.


          So I dropped both tables and started two new fresh tables
          for both entities and now the cascaded remove works
          because they receive the same ids.