Skip to content

Commit caca523

Browse files
committed
[ADD] #26 SpringBootJpa/simple
- using intellij convert feature
1 parent adb6856 commit caca523

19 files changed

+400
-28
lines changed

SpringBootJpa/src/main/java/com/example/simple/repository/SimpleCustomJpqlRepository.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
public class SimpleCustomJpqlRepository implements CustomJpqlRepository {
1010

11-
@PersistenceContext
12-
private EntityManager em;
11+
@PersistenceContext
12+
private EntityManager em;
1313

14-
@Override
15-
public List<Customer> findByName(String name) {
16-
return em.createNamedQuery("Custom.findByName", Customer.class)
17-
.setParameter("name", name)
18-
.getResultList();
19-
}
14+
@Override
15+
public List<Customer> findByName(String name) {
16+
return em.createNamedQuery("Custom.findByName", Customer.class)
17+
.setParameter("name", name)
18+
.getResultList();
19+
}
2020
}

SpringBootJpa/src/main/kotlin/com/example/SpringBootJpaKotlinApplication.kt

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.example.kotlin
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication
4+
import org.springframework.boot.runApplication
5+
6+
@SpringBootApplication
7+
class SpringBootJpaKotlinApplication {
8+
fun main(args: Array<String>) {
9+
runApplication<SpringBootJpaKotlinApplication>(*args)
10+
}
11+
}
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.example.kotlin.simple.domain
2+
3+
import javax.persistence.*
4+
5+
@Entity
6+
@NamedQuery(name = "Custom.findByName", query = "SELECT c FROM Customer c WHERE c.name = :name ")
7+
class Customer(
8+
@Id @GeneratedValue val idx: Long? = null,
9+
@Column(length = 50) val name: String,
10+
@Column(length = 14) val tel: String,
11+
var bigo: String) {
12+
13+
constructor() : this(null, "", "", "")
14+
15+
fun changeBigo(bigo: String) {
16+
this.bigo = bigo
17+
}
18+
19+
override fun toString(): String {
20+
return "Customer{" +
21+
"idx=" + idx +
22+
", name='" + name + '\''.toString() +
23+
", tel='" + tel + '\''.toString() +
24+
", bigo='" + bigo + '\''.toString() +
25+
'}'.toString()
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.example.kotlin.simple.domain
2+
3+
import javax.persistence.Entity
4+
import javax.persistence.GeneratedValue
5+
import javax.persistence.GenerationType
6+
import javax.persistence.Id
7+
import java.time.LocalDateTime
8+
9+
@Entity
10+
class TimeData(
11+
@Id @GeneratedValue val idx: Long? = null,
12+
val date: LocalDateTime) {
13+
14+
constructor() : this(null, LocalDateTime.now())
15+
16+
override fun toString(): String {
17+
return "TimeData{" +
18+
"idx=" + idx +
19+
", date=" + date +
20+
'}'.toString()
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.example.kotlin.simple.repository
2+
3+
import com.example.kotlin.simple.domain.Customer
4+
5+
interface CustomJpqlRepository {
6+
7+
fun findByName(name: String): List<Customer>
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.kotlin.simple.repository
2+
3+
import com.example.kotlin.simple.domain.Customer
4+
import org.springframework.data.jpa.repository.JpaRepository
5+
6+
interface CustomerRepository : JpaRepository<Customer, Long>, CustomJpqlRepository
7+
8+
9+
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.example.kotlin.simple.repository
2+
3+
import com.example.kotlin.simple.domain.Customer
4+
5+
import javax.persistence.EntityManager
6+
import javax.persistence.PersistenceContext
7+
8+
class SimpleCustomJpqlRepository : CustomJpqlRepository {
9+
10+
@PersistenceContext
11+
private val em: EntityManager? = null
12+
13+
override fun findByName(name: String): List<Customer> {
14+
return em!!.createNamedQuery("Custom.findByName", Customer::class.java)
15+
.setParameter("name", name)
16+
.resultList
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.example.kotlin.simple.repository
2+
3+
import com.example.kotlin.simple.domain.TimeData
4+
import org.springframework.data.jpa.repository.JpaRepository
5+
6+
interface TimeDataRepository : JpaRepository<TimeData, Long>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.example.kotlin.simple.service
2+
3+
import com.example.kotlin.simple.domain.Customer
4+
5+
interface CustomService {
6+
7+
fun upsert(customer: Customer): Customer
8+
9+
fun find(idx: Long?): Customer
10+
11+
fun findByName(name: String): List<Customer>
12+
13+
fun delete(customer: Customer): Customer
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.example.kotlin.simple.service
2+
3+
import com.example.kotlin.simple.domain.Customer
4+
import com.example.kotlin.simple.repository.CustomerRepository
5+
import org.springframework.beans.factory.annotation.Autowired
6+
import org.springframework.stereotype.Service
7+
import org.springframework.transaction.annotation.Transactional
8+
import java.util.function.Supplier
9+
10+
@Service
11+
@Transactional
12+
class SimpleCustomerService : CustomService {
13+
14+
@Autowired
15+
private val repository: CustomerRepository? = null
16+
17+
override fun upsert(customer: Customer): Customer {
18+
return repository!!.save(customer)
19+
}
20+
21+
override fun find(idx: Long?): Customer {
22+
return repository!!.findById(idx!!).orElseThrow<RuntimeException> { RuntimeException() }
23+
}
24+
25+
override fun findByName(name: String): List<Customer> {
26+
return repository!!.findByName(name)
27+
}
28+
29+
override fun delete(customer: Customer): Customer {
30+
repository!!.delete(customer)
31+
return customer
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example.kotlin.simple.web
2+
3+
import com.example.kotlin.simple.domain.Customer
4+
import com.example.kotlin.simple.service.CustomService
5+
import org.springframework.beans.factory.annotation.Autowired
6+
import org.springframework.http.ResponseEntity
7+
import org.springframework.web.bind.annotation.*
8+
9+
@RestController
10+
@RequestMapping("/customer")
11+
class CustomController {
12+
13+
@Autowired
14+
private val service: CustomService? = null
15+
16+
@PostMapping
17+
fun upsert(@RequestBody customer: Customer): ResponseEntity<*> {
18+
return ResponseEntity.ok(service!!.upsert(customer))
19+
}
20+
21+
@GetMapping(value = ["/findByName"])
22+
fun findByName(@RequestParam("name") name: String): ResponseEntity<*> {
23+
return ResponseEntity.ok(service!!.findByName(name))
24+
}
25+
26+
@GetMapping(value = ["/{id}"])
27+
fun find(@PathVariable("id") id: Long?): ResponseEntity<*> {
28+
return ResponseEntity.ok(service!!.find(id))
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.example.kotlin.simple.web
2+
3+
import org.springframework.web.bind.annotation.GetMapping
4+
import org.springframework.web.bind.annotation.RequestMapping
5+
import org.springframework.web.bind.annotation.RestController
6+
7+
import java.time.LocalDateTime
8+
9+
@RestController
10+
@RequestMapping("date")
11+
class TimeController {
12+
13+
val localDateTime: LocalDateTime
14+
15+
@GetMapping
16+
get() = LocalDateTime.now()
17+
}

SpringBootJpa/src/test/java/com/example/CustomerRepositoryTests.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.junit.runner.RunWith;
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
9-
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
109
import org.springframework.test.context.junit4.SpringRunner;
1110

1211
import static org.junit.Assert.assertNotEquals;
@@ -17,17 +16,10 @@ public class CustomerRepositoryTests {
1716

1817
@Autowired
1918
private CustomerRepository repository;
20-
@Autowired
21-
private TestEntityManager testEntityManager;
22-
23-
// 비영속성 데이터
24-
private Customer getPersistenceContextCustomer() {
25-
return new Customer("heo won chul", "010-xxxx-xxxx", "developer");
26-
}
2719

2820
@Test
2921
public void test_update() {
30-
Customer customer = repository.save(getPersistenceContextCustomer());
22+
Customer customer = repository.save(new Customer("heo won chul", "010-xxxx-xxxx", "developer")); // 비영속성 데이터
3123
customer.changeBigo("Developer");
3224

3325
assertNotEquals(repository.save(customer).getBigo(), "developer");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.example.kotlin
2+
3+
import com.example.kotlin.simple.domain.Customer
4+
import com.example.kotlin.simple.repository.CustomerRepository
5+
import org.junit.Assert.assertNotEquals
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
import org.springframework.beans.factory.annotation.Autowired
9+
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
10+
import org.springframework.test.context.junit4.SpringRunner
11+
12+
13+
@RunWith(SpringRunner::class)
14+
@DataJpaTest
15+
open class CustomerRepositoryTests {
16+
17+
@Autowired
18+
private val repository: CustomerRepository? = null
19+
20+
@Test
21+
fun test_update() {
22+
val customer = repository!!.save(Customer(name = "heo won chul", tel = "010-xxxx-xxxx", bigo = "developer")) // 비영속성 데이터
23+
customer.changeBigo("Developer")
24+
25+
assertNotEquals(repository.save(customer).bigo, "developer")
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.example.kotlin
2+
3+
import com.example.kotlin.simple.domain.Customer
4+
import org.junit.Assert.assertNotEquals
5+
import org.junit.Test
6+
import org.junit.runner.RunWith
7+
import org.springframework.beans.factory.annotation.Autowired
8+
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
9+
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager
10+
import org.springframework.test.context.junit4.SpringRunner
11+
12+
@RunWith(SpringRunner::class)
13+
@DataJpaTest
14+
open class EntityManagerTests {
15+
16+
@Autowired
17+
private val testEntityManager: TestEntityManager? = null
18+
19+
@Test
20+
fun test_insertClearAndFindAndUpdateClear() {
21+
val customer = testEntityManager!!.persistFlushFind(com.example.kotlin.simple.domain.Customer(name = "heo won chul", tel = "010-xxxx-xxxx", bigo = "developer")) // 비영속성 데이터
22+
customer.changeBigo("Developer")
23+
testEntityManager.flush() // Database 동기화
24+
// testEntityManager.clear(); // Persistence Context 초기화
25+
26+
val result = testEntityManager.find(Customer::class.java, customer.idx)
27+
assertNotEquals(result.bigo, "developer")
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.example.kotlin;
2+
3+
import com.example.onetomany.domain.Order;
4+
import com.example.onetomany.domain.Product;
5+
import com.example.onetomany.repository.OrderRepository;
6+
import com.example.onetomany.repository.ProductRepository;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
13+
import java.util.Arrays;
14+
import java.util.List;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
18+
@RunWith(SpringRunner.class)
19+
@DataJpaTest
20+
public class OneToManyTests {
21+
22+
@Autowired
23+
private OrderRepository orderRepository;
24+
25+
@Autowired
26+
private ProductRepository productRepository;
27+
28+
private static final String PRODUCT_NAME = "java";
29+
30+
@Test
31+
public void test_productFindByName() {
32+
// given
33+
insertBaseData();
34+
35+
// when
36+
Product product = productRepository.findByName(PRODUCT_NAME);
37+
38+
// then
39+
assertThat(product.getName()).isEqualTo(PRODUCT_NAME);
40+
}
41+
42+
private void insertBaseData() {
43+
Product javaProduct = new Product("java", "java content");
44+
Product kotlinProduct = new Product("kotlin", "kotlin content");
45+
Product scalaProduct = new Product("scala", "scala content");
46+
47+
productRepository.save(javaProduct);
48+
productRepository.save(kotlinProduct);
49+
productRepository.save(scalaProduct);
50+
51+
javaProduct = productRepository.findByName(javaProduct.getName());
52+
kotlinProduct = productRepository.findByName(kotlinProduct.getName());
53+
scalaProduct = productRepository.findByName(scalaProduct.getName());
54+
55+
List<Order> javaOrders = Arrays.asList(new Order(3, "", javaProduct), new Order(3, "", javaProduct));
56+
List<Order> kotlinOrders = Arrays.asList(new Order(2, "", kotlinProduct), new Order(2, "", kotlinProduct));
57+
List<Order> scalaOrders = Arrays.asList(new Order(1, "", scalaProduct));
58+
59+
orderRepository.saveAll(javaOrders);
60+
orderRepository.saveAll(kotlinOrders);
61+
orderRepository.saveAll(scalaOrders);
62+
63+
System.out.println("========================= Product =========================");
64+
productRepository.findAll().forEach(System.out::println);
65+
System.out.println("========================== Order ==========================");
66+
orderRepository.findAll().forEach(System.out::println);
67+
}
68+
}

0 commit comments

Comments
 (0)