담배팔이소년
Starting JPA 본문
Hibernate library
- hibernate-core : hibernate library
- hibernate-entitymanager : library that implemented JPA standard. Let hibernate operate as implemented JPA
- hibernate-jpa-X.X-api : collection of JPA X.X standard API
or simply by spring-boot-starter-data-jpa
- spring-boot-starter-data-jpa
- Abstract library for Spring Boot
- Manages version of JPA-related libraries by version of Spring Boot
Object Mapping
CREATE TABLE MEMBER (
ID LONG AUTO_INCREMENT NOT NULL, -- 아이디(기본키)
NAME VARCHAR(255), -- 이름
AGE INTEGER NOT NULL, -- 나이
PRIMARY KEY (ID)
)
package jpabook.start;
import javax.persistence.*;
@Entity
@Table(name="MEMBER")
public class Member {
@Id
@Column(name = "ID")
private Long id;
@Column(name = "NAME")
private String username;
private Integer age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
MEMBER TABLE Member Class
ID | id |
NAME | username |
AGE | age |
- @Entity
- Class will be mapped to Table
- Class with @Entity is called Entity Class
- @Table
- Tells which Table to map
- Specify Table by name property : @Table(name="MEMBER")
- map Class to Table named CLASS_NAME if name is omitted
- @Id
- Filed with @Id is mapped to Primary Key of Table
- Filed is called Identifier Field
- @Column
- map filed to column of table
- can specify column name by name property : @Column(name="NAME")
- Filed without mapping data
- Filed is mapped to column named FILED_NAME
- if DB is case sensitive, mapping must be done explicitly by name property
Hibernate ddl-auto configuration
- create : delete existing schema & re-create schema when Session factory is executed
- create-drop : same as create but delete shcema when ssesion factory shuts
- update : compare domain & schema, and do extra works that are required. Data is not deleted
- validate : check whether schema is valid when Session factory is executed. Throws Exception when schema is not valid
Entity Manager
Creating EntityManager Procedure
- Create EntityManagerFactory by configuration data of persistence.xml
- Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME)
- generally, regist persistence unit per DB to connect
- Entity Manager Factory costs a lot
- configurations, Object for JPA operation, DB Connection pool, …
- should be created ONLY ONCE & be shared for entire application
- also shared for different threads & safe for simultaneous accesses
- Create EntityManager
- entityManagerFactory.createEntityManager()
- EntityManager has close relationship with DB connection, shouldn’t be shared for threads
- can CRUD entity to DB by using Entity Manager
- Closing
- Entity Manager must be closed after usage
- Entity Manager Factory must be close when application exits
Transaction Management
- JPA always modifies data in Transaction.
- modifying without transaction throws exception
- Must get Transaction API from EntityManager before starting transaction
EntityTransaction tx = entityManager.Transaction();
try {
tx.begin();
// BIZ LOGIC
tx.commit(); // commit transaction
} catch (Exception e) {
tx.rollback(); // rollback transaction
}
- JPA modification : find → MODIFY → persist
- JPA tracks which Entity has been modifed
JPQL(Java Persistence Query Language)
- When searching multiple items, Searching Entity Object requires all the data of DB to be loaded & transformed to Entity Object.
- JPA resolves it by JPQL
- Unlike SQL, JPQL is Query about Entity Object
- JPQL got no idea about DB table, JPA anlyze JPQL & create adequate SQL to read DB
- JPQL is case-sensitive
'JPA' 카테고리의 다른 글
JPA(ORM) vs. MyBatis(SQL Mapper) (0) | 2023.04.11 |
---|
Comments