简体   繁体   English

JPA实体关系指导

[英]JPA direction of entity relationships

I am new to JPA and am having some difficulty understanding the "Direction in Entity Relationships" concepts as described here: 我是JPA的新手,在理解“实体关系方向”概念时遇到一些困难,如下所述:

http://docs.oracle.com/javaee/7/tutorial/doc/persistence-intro001.htm#BNBQI http://docs.oracle.com/javaee/7/tutorial/doc/persistence-intro001.htm#BNBQI

Is uni- or bidirectionality something that you choose when designing your entities or is it given by the database schema? 是在设计实体时选择单向性还是双向性,还是由数据库模式提供? Like in the order application ( http://docs.oracle.com/javaee/7/tutorial/doc/persistence-basicexamples001.htm ), could you for example design it so that the lineitem knows about which orders it belongs to, but an order wouldn't know which lineitems it has? 就像在顺序应用程序( http://docs.oracle.com/javaee/7/tutorial/doc/persistence-basicexamples001.htm ),你能不能例如设计它,以便lineitem知道哪些订单是属于,但order不知道它有哪些order

You decide whether a relationship is uni-directional or bi-directional by the fields and annotations you include on the entities. 您可以通过在实体上包括的字段和注释来确定关系是单向还是双向。

Uni-directional 单向

@Entity
public class Parent(){

    @OneToMany
    private List<Child> children;
}

@Entity
public class Child(){

}

Bi-directional 双向

@Entity
public class Parent(){

    @OneToMany
    private List<Child> children;
}

@Entity
public class Child(){

    @ManyToOne
    @JoinColumn
    private Parent parent;
}

As you can see the uni-directional relationship does not allow the child to access the parent, while the bi-directional does allow parent access. 如您所见, 单向关系不允许子级访问父级,而双向关系则允许父级访问。 This link is created by adding an annotated field to the child of the parent's type and is completely optional. 该链接是通过将注释字段添加到父类型的子对象来创建的,并且是完全可选的。 It boils down to a design decision. 它归结为一个设计决策。

Of course the database must support the relationship, meaning the proper primary/foreign keys are established to link the tables, but nothing special is required in your database. 当然,数据库必须支持该关系,这意味着将建立适当的主键/外键来链接表,但是数据库中不需要任何特殊的东西。

One important concept to be aware of when modeling these relationships is the owning entity. 建模这些关系时要注意的一个重要概念是拥有实体。 I have written this article about the topic which may be helpful. 我写了这篇有关该主题的文章 ,这可能会有所帮助。

That depend upon your requirement 那取决于你的要求

Unidirectional

@Entity
@AutoProperty
public class OneToOneUnidirectionalA implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    @OneToOne
    private OneToOneUnidirectionalB b;

    private String s;

    // Setters, Getters, Constructors, Pojomatic...

}


@Entity
@AutoProperty
public class OneToOneUnidirectionalB implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    // No reference to OneToOneUnidirectionalA
    // since this is a unidirectional relationship

    private String s;

    // Setters, Getters, Constructors, Pojomatic...

}

Bidirectional A owns the relationship. 双向A拥有该关系。 We need to avoid Pojomatic circular reference issues too: 我们还需要避免Pojomatic循环引用问题:

@Entity
@AutoProperty
public class OneToOneBidirectionalA implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    @Property(policy=PojomaticPolicy.NONE)
    @OneToOne
    private OneToOneBidirectionalB b;

    // Setters, Getters, Constructors, Pojomatic...

}



@Entity
@AutoProperty
public class OneToOneBidirectionalB implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    @Property(policy=PojomaticPolicy.NONE)
    @OneToOne(mappedBy="b")
    private OneToOneBidirectionalA a;

    // Setters, Getters, Constructors, Pojomatic...

}

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

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