2

I am trying to understand this concept but any explanation I search for online is too complex or assumes too much knowledge. Can someone break this concept down for me and explain it simply?

2 Answers 2

4

What it means is that the database is abstracted away from your application so much, that the concepts of "saving" to the database doesn't show up on the highest levels.

In other words, your application would set data on an business object, and somewhere deep down below, in a lower layer, that would be saved, but automatically. Transparently.

In essence, its a holy grail of abstraction, in terms of separating your application from the database layer.

1
  • Ok. So to make sure I understand this. Suppose I have an object car with a property NumberPlate and this object is mapped to a table in the database via an ORM. If I did some thing like car.NumberPlate = "22okyu7" then the transparent persistence you are talking about is the fact that when I set that property the database record is automatically updated without any further action on my part? Commented Dec 31, 2013 at 21:58
2

The real beauty of an ORM is the transparent persistence that provides, which will let you write plain objects and make them persistent with little work. To understand how transparent persistence works, you have to understand the following concepts.

The first one is a concept called Persistence Context. The Persistence Context is the place used by the ORM to keep track of your Objects changes in order to persist them at a later point. Each Object in a persistence context will have one of these states:

  • new, or transient: the object has just been instantiated and is not associated with a persistence context. It has no persistent representation in the persistent storage.
  • persistent: the object is associated with a persistence context and has representation in the persistent storage.
  • detached: the object is no longer associated with a persistence context (usually because the persistence context was closed).
  • removed: the object is associated with a persistence context, however it is scheduled for removal from the persistent storage.

The other important concept is called Persistence by Reachability. Which states that any transient object that gets related to a persistent object, will become persistent too.

These two concepts implemented make the so called Transparent Persistence possible.

Lets put these in java/jpa code:


    EntityManager em = //obtain the EntityManger instance
    EntityTransaction tx = null;
    try {
        tx = em.getTransaction();
        tx.begin();
    
        Department dept1 = em.find(Department.class, "department1");
        dept1.addEmployee(new Employee("José", 10503f));
    
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        throw new RuntimeException(e);
    } finally {
        em.close();
    }

On the line:


    Department dept1 = em.find(Department.class, "department1");

I'm getting the Department department1 from the persistent storage and on line:


    dept1.addEmployee(new Employee("José", 10503f));

just append a new employee to it. Due to department1 is in the persistent state, by reachability the transient instance of Employee (José) will be persisted and attached to the department.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.