Skip to content

Commit e4de154

Browse files
committed
[FIX] #26 SpringBootJpa/onetoone
1 parent 90df86e commit e4de154

File tree

8 files changed

+91
-61
lines changed

8 files changed

+91
-61
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.example.kotlin.onetoone.domain
2+
3+
import javax.persistence.*
4+
5+
@Entity
6+
@Table(name = "MARKET")
7+
data class Market(
8+
@Id @GeneratedValue @Column(name = "MARKET_ID")
9+
val idx: Long? = null,
10+
@Column(name = "MARKET_NAME")
11+
var name: String? = null,
12+
@Column(name = "MARKET_LOCATION")
13+
var location: String? = null,
14+
@OneToOne @JoinColumn(name = "MARKET_ID")
15+
var owner: Owner? = null) {
16+
17+
constructor() : this(null, null, null, null)
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.example.kotlin.onetoone.domain
2+
3+
import javax.persistence.*
4+
5+
@Entity
6+
@Table(name = "OWNER")
7+
data class Owner(
8+
@Id @GeneratedValue @Column(name = "OWNER_ID")
9+
val idx: Long? = null,
10+
@Column(name = "OWNER_NAME")
11+
var name: String? = null,
12+
@OneToOne @JoinColumn(name = "OWNER_ID")
13+
var market: Market? = null
14+
) {
15+
constructor() : this(null, null, null)
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.example.kotlin.onetoone.repository
2+
3+
import com.example.kotlin.onetoone.domain.Market
4+
import org.springframework.data.jpa.repository.JpaRepository
5+
6+
interface MarketRepository : JpaRepository<Market, Long>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.example.kotlin.onetoone.repository
2+
3+
import com.example.kotlin.onetoone.domain.Owner
4+
import org.springframework.data.jpa.repository.JpaRepository
5+
6+
interface OwnerRepository : JpaRepository<Owner, Long>

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

-9
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,4 @@ data class Customer(
1515
fun changeBigo(bigo: String) {
1616
this.bigo = bigo
1717
}
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-
}
2718
}

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

-7
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,4 @@ data class TimeData(
1212
val date: LocalDateTime) {
1313

1414
constructor() : this(null, LocalDateTime.now())
15-
16-
override fun toString(): String {
17-
return "TimeData{" +
18-
"idx=" + idx +
19-
", date=" + date +
20-
'}'.toString()
21-
}
2215
}

SpringBootJpa/src/test/kotlin/com/example/kotlin/OneToOneTests.java

-45
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.example.kotlin
2+
3+
import com.example.kotlin.onetoone.domain.Market
4+
import com.example.kotlin.onetoone.domain.Owner
5+
import com.example.kotlin.onetoone.repository.MarketRepository
6+
import com.example.kotlin.onetoone.repository.OwnerRepository
7+
import org.junit.Before
8+
import org.junit.Test
9+
import org.junit.runner.RunWith
10+
import org.springframework.beans.factory.annotation.Autowired
11+
import org.springframework.boot.test.context.SpringBootTest
12+
import org.springframework.test.context.junit4.SpringRunner
13+
import org.springframework.transaction.annotation.Transactional
14+
15+
@RunWith(SpringRunner::class)
16+
@SpringBootTest
17+
@Transactional
18+
open class OneToOneTests {
19+
20+
@Autowired
21+
lateinit var marketRepository: MarketRepository
22+
23+
@Autowired
24+
lateinit var ownerRepository: OwnerRepository
25+
26+
@Before
27+
fun before_init() {
28+
marketRepository.save(Market(name = "원철 중화 반점", location = "서울 구로구"))
29+
marketRepository.save(Market(name = "나은 중화 반점", location = "서울 구로구"))
30+
marketRepository.save(Market(name = "구글 중화 반점", location = "서울 구로구"))
31+
32+
ownerRepository.save(Owner(name = "원철"))
33+
ownerRepository.save(Owner(name = "나은"))
34+
ownerRepository.save(Owner(name = "구글"))
35+
36+
marketRepository.findAll().forEach { println(it) }
37+
ownerRepository.findAll().forEach { println(it) }
38+
}
39+
40+
@Test
41+
fun test_findOne() {
42+
println(marketRepository.findById(1L).orElse(null))
43+
println(ownerRepository.findById(1L).orElse(null))
44+
}
45+
}

0 commit comments

Comments
 (0)