简体   繁体   中英

Hibernate creates new Record for each OneToMany connection

I'm a hibernate and EJB Beginner. I'm having this simple ORM:

USER -> belongsTo -> GROUP

I'm used to do, in other languages, the following to create multiple new users which belong to the same group:

    Group g = new Group();
    groupsManager.create(g);

    User u1 = new User("Tom");
    User u1 = new User("Jerry");

    u1.setGroup(g);
    u2.setGroup(g);

    usersManager.create(u1);
    usersManager.create(u2);

Records are getting created, but instead of each user belongs to the same group i'm having finally 3 groups.

These are the both entity classes:

@Entity
@Table(name="Users")
public class User extends AbstractEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    public User() {
        super();
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    //*** Some other Fields


    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
        @JoinColumn(name="group_id")
    private Group group;

    //*** Setter and Getters
}

And the Group:

@Entity
@Table(name="Groups")
public class Group extends AbstractEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    public Group() {
        super();
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    @OneToMany(cascade = CascadeType.ALL)
     private Set<User> users;
}

I suppose, the {groupsManager,usersManager}.create() methods do use separate EntityManager instances, that's why multiple Group instances are created. Just make sure to use the same EntityManager as context for all operations.

It works like this, because you could have multiple persistent contexts concurrently, eg separate data models or multiple connections to different databases etc.

I suggest to do the following:

  • learn JPA properly and from ground up (as defined by the standard), don't focus on Hibernate as a single proprietary provider implementation
  • learn how JPA works in the context of a JavaEE application server
  • don't use the DAO pattern. It's was relevant pre JavaEE5 (pre 2009), now using the EntityManager directly should be sufficient in most cases (eg calling EntityManager.persist)

I've simplified the design and used @Local interfaces instead of @Remote. Both calls of the create() method used the same EntityManger instance.

I personally think using the DAO pattern is still better than using em.persist() directly and they are really great cause they helping structuring the code properly.

The advantage of using data access objects is the relatively simple and rigorous separation between two important parts of an application that can and should know almost nothing of each other, and which can be expected to evolve frequently and independently. http://en.wikipedia.org/wiki/Data_access_object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM