Hibernate bidirectional onetoone Fetchtype lazy problem

Source: Internet
Author: User

 hibernate bidirectional onetoone fetchtype lazy problemCategory: Hibernate2013-06-26 14:54 932 people read comments (2) favorite reports

Reproduced in: http://mshijie.javaeye.com/admin/blogs/440057

When you use JPA (Hibernate) today to implement a one-to-one association, you find that lazy loading cannot be used. The person is associated with a picture. When the person is read, the picture is displayed for the picture. Read 10 Person A database query occurred 11 times.

Finally, after reviewing the information, it was found that the understanding of Onetoone was not thorough enough. The previous association was defined as such.

Person code
    1. @Entity
    2. public class Person {
    3. @Id
    4. @GeneratedValue (strategy = generationtype.identity)
    5. private int id;
    6. @OneToOne (cascade = cascadetype.all, fetch = fetchtype.lazy, Mappedby = "person
    7. ")
    8. private picture picture;
    9. }
Java code
    1. @Entity
    2. Public class Picture {
    3. @Id
    4. @GeneratedValue (strategy = generationtype.identity)
    5. private int id;
    6. @OneToOne
    7. private person person;
    8. }
[Java]View Plaincopy
    1. <span style="Font-size:medium;" >@Entity
    2. Public class Picture {
    3. @Id
    4. @GeneratedValue (strategy = generationtype.identity)
    5. private int id;
    6. @OneToOne
    7. private person person;
    8. }
    9. </span>
[Java]View Plaincopy
  1. <span style="FONT-SIZE:14PX;" >@Entity
  2. Public class Picture {
  3. @Id
  4. @GeneratedValue (strategy = generationtype.identity)
  5. private int id;
  6. @OneToOne
  7. private person person;
  8. }
  9. </span>

The primary table is the picture, which is the person from the table. The Foreign key field is defined in the????

Because if lazy loading is to work, you must set up a proxy object. But Personn can not associate a picture, if there is a picture association is set to proxy object lazy loading, if the picture is not present, set NULL, Since the foreign key field is defined in the picture table, hibernate cannot determine whether the object is associated with a chart without reading it, so it is not possible to set NULL or proxy objects, unify the proxy object, and not be able to satisfy the non-associative situation. So the lazy load cannot be used, only the display reads the picture.

The reason has been found. Made the following changes

Person code
    1. @Entity
    2. public class Person {
    3. @Id
    4. @GeneratedValue (strategy = generationtype.identity)
    5. private int id;
    6. @OneToOne (cascade = cascadetype.all, fetch = Fetchtype.lazy)
    7. private picture picture;
    8. }
Java code
    1. @Entity
    2. Public class Picture {
    3. @Id
    4. @GeneratedValue (strategy = generationtype.identity)
    5. private int id;
    6. @OneToOne (mappedby = "picture")
    7. private person person;
    8. }
[Java]View Plaincopy
    1. <span style="Font-size:medium;" >@Entity
    2. Public class Picture {
    3. @Id
    4. @GeneratedValue (strategy = generationtype.identity)
    5. private int id;
    6. @OneToOne (mappedby = "picture")
    7. private person person;
    8. }
    9. </span>
[Java]View Plaincopy
  1. <span style="FONT-SIZE:14PX;" >@Entity
  2. Public class Picture {
  3. @Id
  4. @GeneratedValue (strategy = generationtype.identity)
  5. private int id;
  6. @OneToOne (mappedby = "picture")
  7. private person person;
  8. }
  9. </span>

The master-slave relationship of the table is modified, and the foreign key field is defined in the Personnel table. Hibernate can set null or proxy objects based on foreign key fields without reading the picture table, and lazy loading will work.

In the same way, we usually use a one-to-many case is that the multi-terminal is the primary table, so you can set the proxy object or null through the foreign key field. At one end, though, hibernate cannot determine whether an object is associated. But even if the object is not associated, it should not be set to null. Instead of being set to an empty list or map, hibernate can delay loading by acting on the list or map. This is also why, when we set one end associated, the general explicit new is a ArrayList or haskmap, such as:

Java code
    1. @OneToMany (cascade = cascadetype.all, fetch = fetchtype.lazy, Mappedby ="Personnel")
    2. Private List<reward> Rewards = new arraylist<reward> ();
[Java]View Plaincopy
    1. <span style="Font-size:medium;" >@OneToMany (cascade = cascadetype.all, fetch = fetchtype.lazy, Mappedby = "Personnel")
    2. Private List<reward> Rewards = new arraylist<reward> ();</span>
[Java]View Plaincopy
    1. <span style="FONT-SIZE:14PX;" >@OneToMany (cascade = cascadetype.all, fetch = fetchtype.lazy, Mappedby = "Personnel")
    2. Private List<reward> Rewards = new arraylist<reward> ();</span>

This is to avoid using NULL to denote no association, and to use an empty list to be consistent.

Hibernate bidirectional onetoone Fetchtype lazy problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.