Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is persistence in hibernate?

Tags:

java

hibernate

I was reading out the basics of ORM from here where it is defined what is persistence?

here it is defined as

we would like the state of (some of) our objects to live beyond the scope of the JVM so that the same state is available later.

I could not understated what does it mean by beyond the scope of the JVM . what i have understood form this could be

  • object are not handled by jvm but SESSION
  • we can save the state of objects using 2nd level cache.

Please correct me because truly speaking I did not understand this statement which is defined in Hibernates own official site.

like image 544
Sindhoo Oad Avatar asked Feb 09 '23 03:02

Sindhoo Oad


2 Answers

beyond the scope of the JVM means that the state will still exist even after the JVM shuts down. Or, to put it another way, the existence of the state is not dependent on the existence of the JVM. Hibernate is an ORM (object relational mapping) tool which is typically used to map Java objects to records in a database somewhere. When used this way, Hibernate stores state from your Java program in one or more database tables.

Consider the following definition for the Person class:

public class Person {
    private String firstName;
    private String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    // getters and setters
}

Suppose you create 2 Person objects like this:

Person p1 = new Person("Jon", "Skeet");
Person p2 = new Person("Gordon", "Linoff");

If you were to persist these Person objects to the database using Hibernate, you might end up with a Person table looking like this:

+-----------+----------+
| firstName | lastName |
+-----------+----------+
| Jon       | Skeet    |
| Gordon    | Linoff   |
+-----------+----------+

If you stop your Java application and then start it up again, Hibernate can also work in the opposite direction to create Person objects from the rows in this database table.

like image 114
Tim Biegeleisen Avatar answered Feb 11 '23 19:02

Tim Biegeleisen


Persistence means to save data in place that would remain or persist even after the power is turned off. For example saving data in text files is also persistence. Database is one of the ways of persisting data .

You know this, its just a big word.

Beyond the scope of JVM means the data should persist or be preserved even after JVM shuts down i.e your application shuts down.

Hibernate saves or persists a Java bean/object in database. So it is called ORM Object to Relational Mapping framework. This is simple they just use a lot of fancy words so it sounds cool.

like image 31
Oliver Avatar answered Feb 11 '23 17:02

Oliver