简体   繁体   中英

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:

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?

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. We need to avoid Pojomatic circular reference issues too:

@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...

}

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