-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Keesun Baik (a.k.a, Whiteship)
committed
Apr 7, 2018
1 parent
382f5f5
commit da22a1b
Showing
7 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
jpa-getting-started/src/main/java/me/whiteship/jpasudy/Event.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package me.whiteship.jpasudy; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@Getter @Setter @EqualsAndHashCode(of = "id") | ||
public class Event { | ||
|
||
@Id @GeneratedValue | ||
private Long id; | ||
|
||
private String title; | ||
|
||
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) | ||
private EventDetail eventDetail; | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
jpa-getting-started/src/main/java/me/whiteship/jpasudy/EventDetail.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package me.whiteship.jpasudy; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@Getter @Setter @EqualsAndHashCode(of = "id") | ||
public class EventDetail { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@Lob @Basic(fetch = FetchType.LAZY) | ||
private String content; | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
jpa-getting-started/src/main/java/me/whiteship/jpasudy/EventDetailRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package me.whiteship.jpasudy; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface EventDetailRepository extends JpaRepository<EventDetail, Long> { | ||
} |
6 changes: 6 additions & 0 deletions
6
jpa-getting-started/src/main/java/me/whiteship/jpasudy/EventRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package me.whiteship.jpasudy; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface EventRepository extends JpaRepository<Event, Long> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Database | ||
spring.datasource.url=jdbc:postgresql://localhost:5432/jpa | ||
spring.datasource.username=jpa | ||
spring.datasource.password=jpa | ||
|
||
# JPA | ||
spring.jpa.hibernate.ddl-auto=create-drop | ||
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false | ||
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect | ||
spring.jpa.properties.hibernate.format_sql=true | ||
spring.jpa.show-sql=true |
60 changes: 60 additions & 0 deletions
60
jpa-getting-started/src/test/java/me/whiteship/jpasudy/EventRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package me.whiteship.jpasudy; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import javax.persistence.EntityManager; | ||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@RunWith(SpringRunner.class) | ||
@DataJpaTest | ||
public class EventRepositoryTest { | ||
|
||
@Autowired | ||
EventRepository eventRepository; | ||
|
||
@Autowired | ||
EntityManager entityManager; | ||
|
||
@Test | ||
public void addEvent() { | ||
Event event1 = new Event(); | ||
event1.setTitle("new title1"); | ||
|
||
EventDetail eventDetail1 = new EventDetail(); | ||
eventDetail1.setContent("new content1"); | ||
event1.setEventDetail(eventDetail1); | ||
|
||
Event event2 = new Event(); | ||
event2.setTitle("new title"); | ||
|
||
EventDetail eventDetail2 = new EventDetail(); | ||
eventDetail2.setContent("new content"); | ||
event2.setEventDetail(eventDetail2); | ||
|
||
// 2 Inserts | ||
Event savedEvent1 = eventRepository.saveAndFlush(event1); | ||
assertThat(savedEvent1.getId()).isNotNull(); | ||
|
||
Event savedEvent2 = eventRepository.saveAndFlush(event2); | ||
assertThat(savedEvent2.getId()).isNotNull(); | ||
|
||
System.out.println("find by id"); | ||
entityManager.clear(); | ||
|
||
Event eventOnly = eventRepository.findById(savedEvent1.getId()).orElseThrow(() -> new RuntimeException());// Select | ||
|
||
System.out.println("get detail content"); | ||
assertThat(eventOnly.getEventDetail().getContent()).isEqualTo("new content1"); // Select detail | ||
|
||
System.out.println("find all"); | ||
entityManager.clear(); | ||
eventRepository.findAll(); // Select | ||
} | ||
|
||
} |