Jpa (2)
For the first part of JPA, we covered basic information and various methods to implement it. However, although it can’t be called superficial, I believe that one must take a more profound look at the given topic to be able to understand it at its fullest.
Persistence
As you all know, JPA stands for Java Persistence API–but what does persistence mean in this context Persistence refers to the data that outlives the process that created it–meaning that data will continue to exist even when a program terminates. An entity,an object that represents data stored in a relational database, should be considered fleeting if it only resides in the memory and evaporates upon its parental program’s demise. Okay, so how is the context of persistence applied to JPA There exists a core term that exists in JPA called persistence context. It stands for an environment where entities are permanently stored in, acting as an abstract storage that resides between the application and database. If an entity is saved or sought for with the entity manager, they are managed within the persistence context Let’s take a look at an entity’s life cycle
- Transient It’s nothing but an object, and doesn’t have any persistence context.
- Managed The object has been saved by the Entity Manager as an entity. It is contained by the persistence context.
- Detached It was once saved by the persistence context, but has been separated currently.
- Removed The once-an-entity has been removed from both the persistence context and database.![image]
What is the purpose of such life cycle It enables the entities to be in sync with the connected database. When an empty object gets attached with the persistence context, it will be constantly synchronized with the underlying database. But, one might wonder What’s going on underneath JPA to allow such astounding features such as database synchronization or execution of query with methods Here, we can take a look at Hibernate.
Hibernate
Hibernate. One would inevitably have to wonder, why would it have such name Hibernate actually is derived from the functionality it has which enables the entities to stay dormant after it being attached to the persistence context until it is put to use later on. Hibernate is an implementation of JPA, which enables JPA to carry out the aforementioned functions.
It resides atop JPA, serving as a bridge between JPA and the application. JDBC is still running within it; the users just don’t have to write queries on their own. Hibernate will connect a Java object with the database. What this will do is that the database will change according to the alteration made on the object, and allow seamless connection between the two by allowing methods and objects to be used in the place of queries.
Conclusion
JPA and Hibernate allows developers to handle the database with much more ease. The process is also automated by its life cycle, which further enhances the experience. Of course, developers should be acquainted with SQL and the reason for using JPA would be hard to justify in small projects, but in a larger-scale project, the management, automation, and synchronization features of JPA is going to be very useful. I will try my best to constantly apply the newly acquired knowledge into my projects.
Reference
https://velog.io@adam2JPA%EB%8A%94-%EB%8F%84%EB%8D%B0%EC%B2%B4-%EB%AD%98%EA%B9%8C-orm-%EC%98%81%EC%86%8D%EC%84%B1-hibernate-spring-data-jpa https://www.interviewbit.comblogjpa-vs-hibernate https://dev.to/yigi/hibernate-vs-jdbc-vs-jpa-vs-spring-data-jpa-1421
Comments powered by Disqus.