Java - JPA 01

What exactly is persistence?
Persistence refers to information that continues to exist, even after the process or application that created it, is no longer running.

If you are authoring a document in Word, but haven't clicked the save button. The document is considered to be in-memory and it's only persisted, or saved, when you click the save button. If you never save the document, the information inside of it will be lost.

We persist data so that it can be later retrieved, processed, transformed and or analyzed. There are several storage options when it comes to persisting data. Relational databases, NoSQL schemaless databases, file systems, disk storage et cetera.

What is JPA?

  • The Java Persistence API, JPA specification and its associated implementations, help Java developers with accessing, persisting and managing data between Java objects, classes, and relational databases.
  • JPA is an abstraction on top of JDBC that makes it easy to map objects to relational databases and vice versa.
  • JPA is a specification, or a set of guidelines. And can do nothing on its own, as it only a set of empty methods and collection of interfaces that only describe Java persistence methodologies.
  • JPA provides standardized programming though the JPA implementation.
  • JPA is very flexible and can map any entity to a table.
  • JPA doesn't support schemaless or NoSQL databases. It also doesn't support JSON. JPA is aimed only at relational databases.

JPA Instance Provider

In order to be fully functional, JPA needs an implementation, also known as an instance provider. There are four implementations of the JPA specification. EclipseLink, Hibernate, OpenJPA and Data Nucleus. Spring with JPA uses Hibernate as the persistence provider.

The mapping between Java objects and database tables is defined via the persistence metadata(元数据). The JPA instance provider uses the persistence metadata information to perform the correct database operations. 

ORM

Object relational mapping, ORM, is the bridge that connects two totally separate islands allowing them to communicate and share information. The two islands in question are Java and relational databases.

Thanks to object relational mapping, objects can be mapped to tables and tables can be mapped to objects. This makes the process of storing objects to and retrieving (检索) objects from a database easy for the application programmer.

The process of object relational mapping is often delegated to external tools or frameworks. In Java EE, this framework is called Java Persistence, API, JPA.

ORM Benefits

  • Productivity: a lot of the data access code is generated for developer.
  • Application design
  • Clean separation of code
  • Maintenance: code generated by ORM should already be well-tested and the data model can be refactored without affecting how data objects are used.

ORM serves as the bridge between relational databases and Java programs, allowing them to communicate and share information.

ORM Mapping

 

 

JPA Annotation

  • @Entity defines that a class can be mapped to a table.
  • @Table is the name of the underlying table this entity is mapped to.
  • @Id marks a field as a primary key field.
  • @GeratedValue generates a unique identifier for the entity.
  • @Column changes the name or length for any column in the database.

Entity: Entities are objects that live in a database and have the ability to be mapped to a database.

Entity Life Cycle

An entity has events during its lifecycle, events that range from persisting, updating, removing, and loading.

Entity States

  • Regular Java Object: use new to create an entity and cant be used throughout the application
  • Transient or new state: before the entity is persisted, it is considered to be in the transient or new state.
  • Managed state: entity is persisted or entity is loaded from database
  • Loaded from database
  • Removed state: marked for deletion using entity manager's remove method
  • Detached state: final state for an entity. 

When an entity is in the managed state, and is under the control of the entity manager, any updates made to the entity using its setter methods are automatically synchronized with the database.

An object becomes detached if it was removed and flushed, or committed. Once an entity is detached, the object will continue to live in memory until the garbage collector gets rid of it.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值