What's the advantage of load() vs get() in Hibernate?

前端 未结 10 2610
感情败类
感情败类 2020-12-02 04:58

Can anyone tell me what\'s the advantage of load() vs get() in Hibernate?

相关标签:
10条回答
  • 2020-12-02 05:22

    Get() returns the object by fetching it from database or from hibernate cache whereas load() just returns the reference of an object that might not actually exists, it loads the data from database or cache only when you access other properties of the object.

    With load(), we are able to print the id but as soon as we try to access other fields, it fires database query and throws org.hibernate.ObjectNotFoundException if there is no record found with the given identifier. It’s hibernate specific Runtime Exception, so we don’t need to catch it explicitly.

    0 讨论(0)
  • 2020-12-02 05:24

    From the "Java Persistence with Hibernate" book, page 405:

    The one difference between get() and load() is how they indicate that the instance could not be found. If no row with the given identifier value exists in the database, get() returns null. The load() method throws an ObjectNotFoundException. It’s your choice what error-handling you prefer.

    More important, the load() method may return a proxy, a placeholder, without hitting the database. A consequence of this is that you may get an ObjectNotFoundException later, as soon as you try to access the returned placeholder and force its initialization (this is also called lazy loading; we discuss load optimization in later chapters.) The load() method always tries to return a proxy, and only returns an initialized object instance if it’s already managed by the current persistence context. In the example shown earlier, no database hit occurs at all! The get() method on the other hand never returns a proxy, it always hits the database.

    You may ask why this option is useful—after all, you retrieve an object to access it. It’s common to obtain a persistent instance to assign it as a reference to another instance. For example, imagine that you need the item only for a single purpose: to set an association with a Comment: aComment.setForAuction(item). If this is all you plan to do with the item, a proxy will do fine; there is no need to hit the database. In other words, when the Comment is saved, you need the foreign key value of an item inserted into the COMMENT table. The proxy of an Item provides just that: an identifier value wrapped in a placeholder that looks like the real thing.

    0 讨论(0)
  • 2020-12-02 05:27

    Explanation of semantics of these methods doesn't explain the practical difference between them. Practical rule is the following:

    • Use get() when you want to load an object

    • Use load() when you need to obtain a reference to the object without issuing extra SQL queries, for example, to create a relationship with another object:

      public void savePost(long authorId, String text) {
          Post p = new Post();
          p.setText(text);
      
          // No SELECT query here. 
          // Existence of Author is ensured by foreign key constraint on Post.
          p.setAuthor(s.load(Author.class, authorId));
      
          s.save(p);
      }
      
    0 讨论(0)
  • 2020-12-02 05:28

    Whats the advantage of load() vs get() in Hibernate?


    source

    Proxy means, hibernate will prepare some fake object with given identifier value in the memory without hitting a database.

    For Example:
    If we call session.load(Student.class,new Integer(107));

    hibernate will create one fake Student object [row] in the memory with id 107, but remaining properties of Student class will not even be initialized.

    Source

    0 讨论(0)
提交回复
热议问题