Skip to content

Commit 90df86e

Browse files
committed
[FIX] #26 SpringBootJpa
- refactoring
1 parent 6cad44d commit 90df86e

File tree

8 files changed

+19
-30
lines changed

8 files changed

+19
-30
lines changed

Diff for: SpringBootJpa/src/main/kotlin/com/example/kotlin/simple/domain/Customer.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import javax.persistence.*
44

55
@Entity
66
@NamedQuery(name = "Custom.findByName", query = "SELECT c FROM Customer c WHERE c.name = :name ")
7-
class Customer(
7+
data class Customer(
88
@Id @GeneratedValue val idx: Long? = null,
99
@Column(length = 50) val name: String,
1010
@Column(length = 14) val tel: String,

Diff for: SpringBootJpa/src/main/kotlin/com/example/kotlin/simple/domain/TimeData.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import javax.persistence.Id
77
import java.time.LocalDateTime
88

99
@Entity
10-
class TimeData(
10+
data class TimeData(
1111
@Id @GeneratedValue val idx: Long? = null,
1212
val date: LocalDateTime) {
1313

Diff for: SpringBootJpa/src/main/kotlin/com/example/kotlin/simple/repository/SimpleCustomJpqlRepository.kt

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
package com.example.kotlin.simple.repository
22

33
import com.example.kotlin.simple.domain.Customer
4-
54
import javax.persistence.EntityManager
6-
import javax.persistence.PersistenceContext
7-
8-
class SimpleCustomJpqlRepository : CustomJpqlRepository {
95

10-
@PersistenceContext
11-
private val em: EntityManager? = null
6+
class SimpleCustomJpqlRepository(val em: EntityManager) : CustomJpqlRepository {
127

138
override fun findByName(name: String): List<Customer> {
14-
return em!!.createNamedQuery("Custom.findByName", Customer::class.java)
9+
return em.createNamedQuery("Custom.findByName", Customer::class.java)
1510
.setParameter("name", name)
1611
.resultList
1712
}

Diff for: SpringBootJpa/src/main/kotlin/com/example/kotlin/simple/service/SimpleCustomerService.kt

+5-8
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,22 @@ import java.util.function.Supplier
99

1010
@Service
1111
@Transactional
12-
class SimpleCustomerService : CustomService {
13-
14-
@Autowired
15-
private val repository: CustomerRepository? = null
12+
class SimpleCustomerService(val repository: CustomerRepository) : CustomService {
1613

1714
override fun upsert(customer: Customer): Customer {
18-
return repository!!.save(customer)
15+
return repository.save(customer)
1916
}
2017

2118
override fun find(idx: Long?): Customer {
22-
return repository!!.findById(idx!!).orElseThrow<RuntimeException> { RuntimeException() }
19+
return repository.findById(idx!!).orElseThrow<RuntimeException> { RuntimeException() }
2320
}
2421

2522
override fun findByName(name: String): List<Customer> {
26-
return repository!!.findByName(name)
23+
return repository.findByName(name)
2724
}
2825

2926
override fun delete(customer: Customer): Customer {
30-
repository!!.delete(customer)
27+
repository.delete(customer)
3128
return customer
3229
}
3330
}

Diff for: SpringBootJpa/src/main/kotlin/com/example/kotlin/simple/web/CustomController.kt

+4-7
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,20 @@ import org.springframework.web.bind.annotation.*
88

99
@RestController
1010
@RequestMapping("/customer")
11-
class CustomController {
12-
13-
@Autowired
14-
private val service: CustomService? = null
11+
class CustomController(val service: CustomService) {
1512

1613
@PostMapping
1714
fun upsert(@RequestBody customer: Customer): ResponseEntity<*> {
18-
return ResponseEntity.ok(service!!.upsert(customer))
15+
return ResponseEntity.ok(service.upsert(customer))
1916
}
2017

2118
@GetMapping(value = ["/findByName"])
2219
fun findByName(@RequestParam("name") name: String): ResponseEntity<*> {
23-
return ResponseEntity.ok(service!!.findByName(name))
20+
return ResponseEntity.ok(service.findByName(name))
2421
}
2522

2623
@GetMapping(value = ["/{id}"])
2724
fun find(@PathVariable("id") id: Long?): ResponseEntity<*> {
28-
return ResponseEntity.ok(service!!.find(id))
25+
return ResponseEntity.ok(service.find(id))
2926
}
3027
}

Diff for: SpringBootJpa/src/test/kotlin/com/example/kotlin/CustomerRepositoryTests.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import org.springframework.test.context.junit4.SpringRunner
1515
open class CustomerRepositoryTests {
1616

1717
@Autowired
18-
private val repository: CustomerRepository? = null
18+
lateinit var repository: CustomerRepository
1919

2020
@Test
2121
fun test_update() {
22-
val customer = repository!!.save(Customer(name = "heo won chul", tel = "010-xxxx-xxxx", bigo = "developer")) // 비영속성 데이터
22+
val customer = repository.save(Customer(name = "heo won chul", tel = "010-xxxx-xxxx", bigo = "developer")) // 비영속성 데이터
2323
customer.changeBigo("Developer")
2424

2525
assertNotEquals(repository.save(customer).bigo, "developer")

Diff for: SpringBootJpa/src/test/kotlin/com/example/kotlin/EntityManagerTests.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import org.springframework.test.context.junit4.SpringRunner
1414
open class EntityManagerTests {
1515

1616
@Autowired
17-
private val testEntityManager: TestEntityManager? = null
17+
lateinit var testEntityManager: TestEntityManager
1818

1919
@Test
2020
fun test_insertClearAndFindAndUpdateClear() {
21-
val customer = testEntityManager!!.persistFlushFind(com.example.kotlin.simple.domain.Customer(name = "heo won chul", tel = "010-xxxx-xxxx", bigo = "developer")) // 비영속성 데이터
21+
val customer = testEntityManager.persistFlushFind(com.example.kotlin.simple.domain.Customer(name = "heo won chul", tel = "010-xxxx-xxxx", bigo = "developer")) // 비영속성 데이터
2222
customer.changeBigo("Developer")
2323
testEntityManager.flush() // Database 동기화
2424
// testEntityManager.clear(); // Persistence Context 초기화

Diff for: SpringBootJpa/src/test/kotlin/com/example/kotlin/TimeTests.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import java.time.LocalDateTime
1515
class TimeTests {
1616

1717
@Autowired
18-
internal var timeDataRepository: TimeDataRepository? = null
18+
lateinit var timeDataRepository: TimeDataRepository
1919

2020
@Test
2121
fun test_save() {
22-
timeDataRepository!!.save(TimeData(date = LocalDateTime.now()))
22+
timeDataRepository.save(TimeData(date = LocalDateTime.now()))
2323
}
2424

2525
}

0 commit comments

Comments
 (0)