开发者

When to use / not to use, @OneToOne and @ManyToOne

开发者 https://www.devze.com 2023-01-07 19:39 出处:网络
I just started reading开发者_Go百科 about JPA and its implementation in Hibernate. To understand the details better I need some clarification on some of the basics.

I just started reading开发者_Go百科 about JPA and its implementation in Hibernate. To understand the details better I need some clarification on some of the basics.

  1. When to use @OneToOne?

    I may use @OneToOne if the entity manager needs to handle the persistency of the related object. The point is, I can always live without specifying @OneToOne, but then the responsibility is on me to manage the relationship and make sure that the referred objects are not in transient state.

    Is this true?

  2. When to use, or not to use @ManyToOne?

    Say, I'm defining an Employee class and need to define the relationship with the Employer class. In this case, do I need to specify @ManyToOne as below?

    @Entity   
    public class Employer {  
        String name;   
    }   
    
    @Entity   
    class Employee {  
        String name;
    
        @ManytoOne  //or not??   
        Employer employer;   
    }
    


1: When working with entity relations, you must always use the appropriate annotations (OneToOne, OneToMany, ManyToOne, or ManyToMany). The choice you have is whether you want to make sure the entity behind the relation is not transient yourself, or specify the cascade property on the OneToOne annotation to let JPA take care of that for you. This allows you to create a whole graph of objects and persist them in one call:

@OneToOne(cascade = CascadeType.ALL)
private MyType myType;

2: Yes, an employer-employee relationship sounds like a OneToMany relationship, and the employee-employer relationship would be ManyToOne. If you'd like to have both directions, that's called a bi-directional relationship. Have a look at the relevant section in the Java EE tutorial for details.

The JPA section in the Java EE tutorial is a good reference to start from.


When to use OneToOne I may use OneToOne if the entity manager needs to handle the persistency of the related object. the point is, I can always live without specifying oneToOne, but then the responsibility is on me to manage the relationship and making sure that the referred objects are not in transient state. Is this true?

You need not mark the relationship if you don't want the entity manager to handle operations. You can have the related entity as a transient field and persist the related entity manually. But in that case, you wouldn't be using the whole list of features that JPA provides.

By marking the relationships, the EntityManager comes to know about the database structure. And that is essential if you want to utilize the power of JPA.

Eg: class Car
      {
         @OneToOne
         Warehouse warehouse;
         //other fields
      }

Here

  • I can retrieve the related Warehouse object while retrieving a Car

  • I can persist a Warehouse object while persisting a Car (with cascade option as explained by the poster below )

  • likewise Update, Delete ...

Apart from these, while using JPQL,

you can traverse to Warehouse

as in

em.createQuery("select c.warehouse from Car c"); 

These wouldn't work unless you mark the relationship.

0

精彩评论

暂无评论...
验证码 换一张
取 消