@Entity
- 해당 객체를 JPA에서 관리하는 객체임을 말함
- PK인 @Id가 반드시 필요
@Id
- 엔티티의 PK
@GeneratedValue
- 어노테이션 안에 GenerationType strategy() default Auto;
- GenerationType이라는 enum 객체안의 TABLE, SEQUENCE, IDENTIY, AUTO
1. TABLE : DB에 상관없이 ID를 관리하는 별도에 테이블을 사용
2. SEQUENCE : Oracle, PostgreSql, h2
3. IDENTITY : MySql, MariaDB
4. AUTO : 각 DB에 적합한 값을 전달
- 트랜잭션이 종료되기 전, Auto Increase를 통해 아이디 값을 사전에 가져온다.
@Table
- 테이블 name, catalog, schema, uniqueConstraints, indexes 설정
- indexes, uniqueConstraints는 일반적으로 사용을 안하고 DB에 직접 설정하는것을 추구(JPA상에서 생성되는 index ID와 제약조건 명이 DDL시 생성)
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Data
@Builder
@Entity
@Table(name = "TB_USER", indexes = {@Index(columnList = "name")}, uniqueConstraints = {@UniqueConstraint(columnNames = "email")})
public class User{
Hibernate:
create table tb_user (
id bigint not null,
created_at timestamp,
email varchar(255),
name varchar(255),
updated_at timestamp,
use_yn boolean,
primary key (id)
)
Hibernate: create index IDXjus3xa0l3009mkpbt9i5ilxx3 on tb_user (name)
Hibernate:
alter table tb_user
add constraint UK4vih17mube9j7cqyjlfbcrk4m unique (email)
@Column
- 컬럼에 속성을 지정
- name, nullable, unique, length, insertable, updatable
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Data
@Builder
@Entity
@Table(name = "TB_USER", indexes = {@Index(columnList = "name")}, uniqueConstraints = {@UniqueConstraint(columnNames = "email")})
public class User{
@Id
@GeneratedValue
private Long id;
@NonNull
private String name;
@NonNull
@Column(length = 250)
private String email;
@Column(name="reg_at", nullable = false, unique = true)
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
// @OneToMany(fetch = FetchType.EAGER)
// private List<Address> address;
private Boolean useYn;
}
Hibernate:
create table tb_user (
id bigint not null,
reg_at timestamp not null,
email varchar(250),
name varchar(255),
updated_at timestamp,
use_yn boolean,
primary key (id)
)
Hibernate: create index IDXjus3xa0l3009mkpbt9i5ilxx3 on tb_user (name)
Hibernate:
alter table tb_user
add constraint UK4vih17mube9j7cqyjlfbcrk4m unique (email)
Hibernate:
alter table tb_user
add constraint UK_tnspm680jp0k2fyjqm19noh9d unique (reg_at)
insertable, updatable
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Data
@Builder
@Entity
@Table(name = "TB_USER", indexes = {@Index(columnList = "name")}, uniqueConstraints = {@UniqueConstraint(columnNames = "email")})
public class User{
@Id
@GeneratedValue
private Long id;
@NonNull
private String name;
@NonNull
private String email;
@Column(updatable = false)
private LocalDateTime createdAt;
@Column(insertable = false)
private LocalDateTime updatedAt;
// @OneToMany(fetch = FetchType.EAGER)
// private List<Address> address;
private Boolean useYn;
}
@Test
void insertableAndUpdatable(){
User user = new User();
user.setId(6L);
user.setName("six");
user.setEmail("six@gmail.com");
user.setCreatedAt(LocalDateTime.now());
userRepository.save(user);
userRepository.findAll().forEach(System.out::println);
User user1 = userRepository.findById(6L).orElseThrow(RuntimeException::new);
user1.setName("seven");
user1.setUpdatedAt(LocalDateTime.now());
userRepository.save(user1);
userRepository.findAll().forEach(System.out::println);
}
Hibernate:
select
user0_.id as id1_1_0_,
user0_.created_at as created_2_1_0_,
user0_.email as email3_1_0_,
user0_.name as name4_1_0_,
user0_.updated_at as updated_5_1_0_,
user0_.use_yn as use_yn6_1_0_
from
tb_user user0_
where
user0_.id=?
Hibernate:
call next value for hibernate_sequence
Hibernate:
insert
into
tb_user
(created_at, email, name, use_yn, id)
values
(?, ?, ?, ?, ?)
Hibernate:
select
user0_.id as id1_1_,
user0_.created_at as created_2_1_,
user0_.email as email3_1_,
user0_.name as name4_1_,
user0_.updated_at as updated_5_1_,
user0_.use_yn as use_yn6_1_
from
tb_user user0_
User(id=1, name=martin, email=martin@fastcampus.com, createdAt=2022-10-16T18:21:42.087515, updatedAt=2022-10-16T18:21:42.087515, useYn=true)
User(id=2, name=dennis, email=dennis@fastcampus.com, createdAt=2022-10-16T18:21:42.089121, updatedAt=2022-10-16T18:21:42.089121, useYn=true)
User(id=3, name=sophia, email=sophia@slowcampus.com, createdAt=2022-10-16T18:21:42.089576, updatedAt=2022-10-16T18:21:42.089576, useYn=true)
User(id=4, name=james, email=james@slowcampus.com, createdAt=2022-10-16T18:21:42.089978, updatedAt=2022-10-16T18:21:42.089978, useYn=true)
User(id=5, name=martin, email=martin@another.com, createdAt=2022-10-16T18:21:42.090394, updatedAt=2022-10-16T18:21:42.090394, useYn=true)
User(id=6, name=six, email=six@gmail.com, createdAt=2022-10-16T18:21:42.295978, updatedAt=null, useYn=null)
Hibernate:
select
user0_.id as id1_1_0_,
user0_.created_at as created_2_1_0_,
user0_.email as email3_1_0_,
user0_.name as name4_1_0_,
user0_.updated_at as updated_5_1_0_,
user0_.use_yn as use_yn6_1_0_
from
tb_user user0_
where
user0_.id=?
Hibernate:
select
user0_.id as id1_1_0_,
user0_.created_at as created_2_1_0_,
user0_.email as email3_1_0_,
user0_.name as name4_1_0_,
user0_.updated_at as updated_5_1_0_,
user0_.use_yn as use_yn6_1_0_
from
tb_user user0_
where
user0_.id=?
Hibernate:
update
tb_user
set
email=?,
name=?,
updated_at=?,
use_yn=?
where
id=?
Hibernate:
select
user0_.id as id1_1_,
user0_.created_at as created_2_1_,
user0_.email as email3_1_,
user0_.name as name4_1_,
user0_.updated_at as updated_5_1_,
user0_.use_yn as use_yn6_1_
from
tb_user user0_
User(id=1, name=martin, email=martin@fastcampus.com, createdAt=2022-10-16T18:21:42.087515, updatedAt=2022-10-16T18:21:42.087515, useYn=true)
User(id=2, name=dennis, email=dennis@fastcampus.com, createdAt=2022-10-16T18:21:42.089121, updatedAt=2022-10-16T18:21:42.089121, useYn=true)
User(id=3, name=sophia, email=sophia@slowcampus.com, createdAt=2022-10-16T18:21:42.089576, updatedAt=2022-10-16T18:21:42.089576, useYn=true)
User(id=4, name=james, email=james@slowcampus.com, createdAt=2022-10-16T18:21:42.089978, updatedAt=2022-10-16T18:21:42.089978, useYn=true)
User(id=5, name=martin, email=martin@another.com, createdAt=2022-10-16T18:21:42.090394, updatedAt=2022-10-16T18:21:42.090394, useYn=true)
User(id=6, name=seven, email=six@gmail.com, createdAt=2022-10-16T18:21:42.295978, updatedAt=2022-10-16T18:21:42.463601, useYn=null)
@Transient
- 객체에서 사용할 속성으로만 지정, 영속성 처리에서 제외 , DB반영 안됨
@Enumerated
- Enum클래스를 사용할 경우.
- Enum사용시 EnumType.STRING으로 사용하지 않으면 디폴트값이 ORDINAL로 되어 Enum클래스를 사용하면 숫자가 표출
User.java
@Enumerated(value = EnumType.STRING)
private Gender gender;
UserRepository.java
@Query(value = "select * from tb_user limit 1", nativeQuery = true)
Map<String,Object> findRawRecode();
@Test
void enumTest(){
User user = userRepository.findById(1L).orElseThrow(RuntimeException::new);
user.setGender(Gender.MALE);
userRepository.save(user);
userRepository.findAll().forEach(System.out::println);
// native query 사용하여 출력시 0 출력. @Enumerated의 속성값인 ORDINARY가 적용
System.out.println(userRepository.findRawRecode().get("gender"));
}
Hibernate:
select
user0_.id as id1_1_0_,
user0_.created_at as created_2_1_0_,
user0_.email as email3_1_0_,
user0_.gender as gender4_1_0_,
user0_.name as name5_1_0_,
user0_.updated_at as updated_6_1_0_,
user0_.use_yn as use_yn7_1_0_
from
tb_user user0_
where
user0_.id=?
Hibernate:
select
user0_.id as id1_1_0_,
user0_.created_at as created_2_1_0_,
user0_.email as email3_1_0_,
user0_.gender as gender4_1_0_,
user0_.name as name5_1_0_,
user0_.updated_at as updated_6_1_0_,
user0_.use_yn as use_yn7_1_0_
from
tb_user user0_
where
user0_.id=?
Hibernate:
update
tb_user
set
email=?,
gender=?,
name=?,
updated_at=?,
use_yn=?
where
id=?
Hibernate:
select
user0_.id as id1_1_,
user0_.created_at as created_2_1_,
user0_.email as email3_1_,
user0_.gender as gender4_1_,
user0_.name as name5_1_,
user0_.updated_at as updated_6_1_,
user0_.use_yn as use_yn7_1_
from
tb_user user0_
User(id=1, name=martin, email=martin@fastcampus.com, createdAt=2022-10-16T18:43:03.386756, updatedAt=2022-10-16T18:43:03.386756, useYn=true, gender=MALE)
User(id=2, name=dennis, email=dennis@fastcampus.com, createdAt=2022-10-16T18:43:03.389445, updatedAt=2022-10-16T18:43:03.389445, useYn=true, gender=null)
User(id=3, name=sophia, email=sophia@slowcampus.com, createdAt=2022-10-16T18:43:03.390489, updatedAt=2022-10-16T18:43:03.390489, useYn=true, gender=null)
User(id=4, name=james, email=james@slowcampus.com, createdAt=2022-10-16T18:43:03.391428, updatedAt=2022-10-16T18:43:03.391428, useYn=true, gender=null)
User(id=5, name=martin, email=martin@another.com, createdAt=2022-10-16T18:43:03.392437, updatedAt=2022-10-16T18:43:03.392437, useYn=true, gender=null)
Hibernate:
select
*
from
tb_user limit 1
MALE
'Spring > 1-3. JPA' 카테고리의 다른 글
(6. Spring Data JPA) 영속성 컨텍스트 Persistence Context (0) | 2023.05.06 |
---|---|
(5. Spring Data JPA) Entity Listener 활용 (1) | 2022.10.18 |
(3. Spring Data JPA)QueryMethod 쿼리메소드 정의 및 실습 (0) | 2022.10.15 |
(2. Spring Data JPA)JpaRepository의 findById와 getOne 차이 (0) | 2022.10.12 |
(1. Spring Data JPA)Spring Data JPA 정의 (1) | 2022.09.23 |