개발자 끄적끄적
JPA 본문
<JDBC vs JPA(Hibernate) vs Spring DATA JAP>
1) JDBC : Low-Level Database Access
2) JAP : a technical specification that stands for Java Persistence API
3) Hibernate : implementations of JPA
4) Spring Data JPA : a module that makes it eaiser to use JPA
<JPA(Java Persistence API)>
- Java 진영에서 ORM(Object-Relational Mapping) 기술 표준으로 사용하는 인터페이스 모음
- 자바 어플리케이션에서 관계형 데이터베이스를 사용하는 방식을 정의한 인터페이스
- 인터페이스 이기 때문에 Hibernate, OpenJPA 등이 JPA를 구현함
- Only a specification
- Defines a set of interfaces
- Requires an implementation to be usable
- By having a standard API, you are not locked to vendor's implementation
<Entity Class>
- At a minimum, the Entity class
- Must be annotated with @Entity
- Must have a public or protected no-argument constructor
- The class can have other constructors
- Java Annotations
- Step 1: Map 'class' to database table
- ex) @Entity
@Table{name="student"
}
- Step 2: Map 'fields' to database 'columns'
- ex) @Id
@Column{name="id"}
@Column{name="first_name"}
- Primary Key
- ex) @Id
@GeneratedValue(strategy=GeneratioType.IDENTITY)
<Entity Class>
- ID Generate Strategies
- GenerationType.AUTO : Pick an appropriate strategy for the particular database
- GenerationType.IDENTITY : Assign primary keys using 'database identity column'
- GenerationType.SEQUENCE : Assign primary keys using a 'database sequence'
- GenerationType.TABLE : Assign primary keys using an underlying 'database table' to ensure uniqueness
<Entity Manager>
- The EntityManager API is used to 'create and remove' persistent entity instances,
to 'find entities' by their primary key, and to 'query' over entities
<Entity Manager API>
- Create
- Read
- Update
- Delete
<Saving a Java Object with JPA>
- Dao class
- ex)
@Override
public void save(Student theStudent){
entitiyManager.persist(theStudent);
}
<Retrieving a Java Object with JPA>
- ex)
Student myStudent = entityManager.find(Student.class, 1) //Student = Entity class, 1 = Primary key
<Retrieving all Students>
- ex)
entityManager.createQuery("From Student(Name of JPA Entity the class name), Student.class(Return type)
- "FROM Stuent" <- this is NOT the nema of the database table, ALL JPQL sytax is based on entity name and entity fields
<Retreving Students : lastName = 'Doe'>
- ex)
entityManager.createQuery("From Student WHERE lastName='Doe'", Student.class);
- "From Student WHERE lastName='Doe'는 Field of JAP Entity이다
<ORM(Object-Relational Mapping)>
- 우리가 일반 적으로 알고 있는 애플리케이션 Class와 RDB(Relational DataBase)의 테이블을 매핑(연결)한다는 뜻이며,
기술적으로는 어플리케이션의 객체를 RDB 테이블에 자동으로 영속화 해주는 것이라고 보면된다
'웹프레임워크' 카테고리의 다른 글
RestAPI (0) | 2024.05.01 |
---|---|
Spring Security (0) | 2024.04.23 |
Spring WebForm (0) | 2024.04.23 |