Spring Java Study

JPA 연습

각 데이터 별로 Entity와 Repository를 만들었습니다.

아래처럼 localhost:8080/h2-console 을 입력해서 브라우저에서도 JPA 가 잘 작동하는지 확인했습니다.

JPA H2 console

 

 

상속을 사용해서 생성 수정 시간 관리하기

1. Timestamped 객체를 생성한다.

@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class) 
public class Timestamped {

    @CreatedDate
    @Column(updatable = false)
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column
    private LocalDateTime modifiedAt;
}

2. extends로 상속 받아서 사용 한다.

@Entity // 게시글
public class Post extends Timestamped {
	  @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(nullable = false)
    private String title;

    @Column(nullable = false, unique = true)
    private String content;
}

 

@CreatedDate
@Column(updatable = false)
private LocalDateTime createdAt;

@LastModifiedDate
@Column
private LocalDateTime modifiedAt;
  • “extends Timestamped” 으로 상속받는 객체들은 자동으로 생성, 수정시간을 데이터베이스에 입력
  • 필요하다면 해당값을 편하게 Get 할수 있음

 

Spring Data JPA

  • JPA 를 편리하게 사용하기 위해, 스프링에서 JPA 를 Wrapping 함
  • 스프링 개발자들이 JPA 를 사용할 때 필수적으로 생성해야 하나, 예상 가능하고 반복적인 코드들 → Spring Data JPA 가 대신 작성
  • Repostiory 인터페이스만 작성하면, 필요한 구현은 스프링이 대신 함

공식문서

 

Spring Data JPA - Reference Documentation

Example 119. Using @Transactional at query methods @Transactional(readOnly = true) interface UserRepository extends JpaRepository { List findByLastname(String lastname); @Modifying @Transactional @Query("delete from User u where u.active = false") void del

docs.spring.io

 

메모장 사이트 만들기

  • readOnly 메서드가 추가가 안되서 구글링했더니 아래와 같이 org.springframework.transaction.annotation.Transactional 을 해야하는 거였습니다. 
  • 자동으로 해당 패키지가 import되어서 앞부분 다시 지우고 @Transactional 만 넣어줬습니다.

 

 

The attribute readOnly is undefined for the annotation type Transactional

I am getting this error when I am putting this piece of code on a service method @Transactional(readOnly =true) I am writing this code to make a transaction read only. Can you please tell me Wh...

stackoverflow.com

 

이미지 깨짐 현상

아래처럼 static.images에 넣어준 이미지들이 깨진 것을 확인하고 문제 해결을 위해 구글링을 하고 슬랙에 질문을 했습니다.

스프링 이미지 깨짐

경로 설정이 잘못되었던 것이었습니다...

강의에서 static.images 파일에 이미지 저장하라고 해서 했는데

static 파일안에 images 파일을 만들어서 저장하라는 말이었습니다.

 

이럴수가.

 

 

개인 과제

유스케이스(usecase) 만들기

- 관련 자료

 

유스케이스

주어진 자료를 바탕으로 유스케이스 작성을 해보았습니다.

 

 

이게 맞아....??

 

일단 수업에서 한 실습 자료를 바탕으로 기능 추가 구현을 해보도록 하겠습니다.

+ Recent posts