简体   繁体   English

Hibernate为每个OneToMany连接创建新的记录

[英]Hibernate creates new Record for each OneToMany connection

I'm a hibernate and EJB Beginner. 我是一个休眠和EJB初学者。 I'm having this simple ORM: 我有一个简单的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. 记录正在创建,但是不是每个用户都属于同一组,我最终有3个组。

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. 我想,{groupsManager,usersManager} .create()方法确实使用单独的EntityManager实例,这就是为什么要创建多个Group实例的原因。 Just make sure to use the same EntityManager as context for all operations. 只需确保对所有操作使用相同的EntityManager作为上下文。

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 正确地,从基础上(根据标准定义)学习JPA,不要将Hibernate视为单个专有提供程序实现
  • learn how JPA works in the context of a JavaEE application server 了解JPA如何在JavaEE应用程序服务器的上下文中工作
  • don't use the DAO pattern. 不要使用DAO模式。 It's was relevant pre JavaEE5 (pre 2009), now using the EntityManager directly should be sufficient in most cases (eg calling EntityManager.persist) 与JavaEE5之前(2009年之前)有关,现在在大多数情况下直接使用EntityManager应该足够了(例如,调用EntityManager.persist)

I've simplified the design and used @Local interfaces instead of @Remote. 我简化了设计并使用@Local接口代替@Remote。 Both calls of the create() method used the same EntityManger instance. create()方法的两个调用都使用相同的EntityManger实例。

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. 我个人认为使用DAO模式仍然比直接使用em.persist()更好,并且它们确实很棒,因为它们有助于正确地构造代码。

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 http://en.wikipedia.org/wiki/Data_access_object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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