Search code examples
hibernatepersistencepersist

Does hibernate load all the database records? anyway to control it?


I would like to know how hibernate work... Lets say that I have three entity classes: A, B and C.

I know already know the following facts:

  • I will not do any select statement on B objects.
  • I will insert (by calling persist()) hundred of thousands of new rows in B.
  • I will select and update A objects several hundred times.
  • I will not do any operation on C objects.
  • There is hundreds of thousands of C objects previously saved in the database.
  • In another sessions I will repeat exactly the same operations.

As result, the database will become more and more large, and what I am noticing that calling persist() on B objects become slower.

My questions are:

  1. Does hibernate load all the objects in the database? all A, B and C objects?
  2. How can I see the operations that hibernate is doing for loading objects from the database?
  3. Can I control the loading operations? In fact, I do not want C objects to be loaded, and I do not want also load B objects.
  4. How can I see and control what are the records in the database loaded actually as entity objects?
  5. If there is a verification operation on the uniqueness of the id's of the B objects, can I see this verification in a log file?
  6. Can I dis-activate the verification operation to accelerate the persist() operation of B objects? In fact I can manage the uniqueness of the id in my code and I do not want hibernate to verify it.

Solution

  • 1) No, only the required

    2) You can configure hibernate log to show SQL and DML sentence executed

    3) You only load objects selected. Additionaly, you can specifiy EAGER or LAZY for associations

    4) Why do you want to do this? It is Hibernate task.

    5) If an ID is not unique, it is not an ID. Period.

    6) Hibernate just sends to the DB, the DB performs most of the checks. If you want to "tweak" Hibernate for performance, just don't use Hibernate for that operation.