本文介绍了JPA批注,与单独包含的实例相对的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将JPA与数据集合一起使用,如何注释类型为List,ArrayList,Collections等(@OneToMany或@ManyToMany等)的成员时,对我来说很清楚.以下:

It is pretty clear to me when using JPA with a collection of data, how to annotate a member which is of type List, ArrayList, Collections, etc. (@OneToMany or @ManyToMany etc), but what if we have the following:

    @Entity    
    class page{

        Person creator;
        Person modifier;
        Person owner;
    }

在这种情况下,我要做出设计决定,使其不具有任何类型的人员实体集合,而要单独列出变量.我对如何正确注释这些成员感到困惑.

In this case, I want to make the design decision to not have any sort of collection of person entities, but rather separately listed variables. I am confusing myself as to how to correctly annotate these members.

我正确的假设是,即使它们被单独列出,我也应该将它们注释为列表,即

Am I correct in assuming that I should annotate them as if they were a list even though they are listed separately, ie

@ManyToMany,因为许多页面包含许多人"和许多人参与了许多页面",而不是@ManyToOne许多页面包含一个人的创建者实例"(没有创建者实体仅是个人,创建者会仅是Page的成员变量)?

@ManyToMany because "Many Pages include many Persons" and "Many Persons are involved in Many Pages" as opposed to @ManyToOne "Many Pages include One Creator instance of Person" (There is no Creator entity only person, and creator would only be the member variable of Page) ?

推荐答案

您正在寻找的是ManyToOne关系.

What you are looking for is a ManyToOne relationship.

这很简单:

@ManyToOne

@ManyToOne

  1. 一个人可以创建多少个页面?很多.
  2. 有多少人可以创建一个页面?一个

@ManyToMany

@ManyToMany

  1. 一个人可以修改多少个页面?许多
  2. 有多少人可以修改一个页面?许多(但是我会在这里使用OneToMany并列出修改页面的人员列表.@ ManyToMany公平的做法很棘手)

@ManyToOne

@ManyToOne

  1. 一个人如何拥有页面?很多
  2. 人们如何拥有一个页面?一个(有时您可以拥有多个所有者,但在您的情况下,我猜只需要一个所有者)
  1. How pages can be owned by a single person?Many
  2. How persons can own a single page? One (Well sometimes you can have more than one owner,but in your scenario, am guessing only one owner is desired)

所以:

@Entity
public class Page{

 @ManyToOne
 private Person creator;
 @OneToMany
 private List<Person> modifiers;
 @ManyToOne
 private Person owner; //which i guess may be the same as owner.

}

这篇关于JPA批注,与单独包含的实例相对的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 14:16