Skip to content

Commit 709bc3a

Browse files
author
Nikita Konev
committed
more complex example
1 parent d24d2a4 commit 709bc3a

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/main/kotlin/com/example/web/jdbc/web/jdbc/Application.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import org.springframework.boot.ApplicationRunner
66
import org.springframework.boot.autoconfigure.SpringBootApplication
77
import org.springframework.boot.runApplication
88
import org.springframework.data.domain.PageRequest
9-
import org.springframework.data.domain.Pageable
109
import org.springframework.stereotype.Component
1110

1211
@SpringBootApplication
@@ -72,7 +71,22 @@ class AppRunner(
7271
foundPersons.forEach { logger.info("Found Person {}", it) }
7372

7473
// https://spring.io/blog/2018/09/24/spring-data-jdbc-references-and-aggregates
75-
val orders = orderRepository.findAll();
74+
// val orders = orderRepository.findAll()
75+
// orders.forEach { logger.info("Found order {}", it) }
76+
77+
val order = PurchaseOrder(0, "Kutaisi")
78+
order.addItem(4, "Captain Future Comet Lego set")
79+
order.addItem(2, "Cute blue angler fish plush toy")
80+
val savedOrder = orderRepository.save(order)
81+
82+
val orders = orderRepository.findAll()
7683
orders.forEach { logger.info("Found order {}", it) }
84+
85+
logger.info("=== deleting order with its items ===")
86+
orderRepository.delete(savedOrder)
87+
88+
val afterDeleteOrders = orderRepository.findAll()
89+
afterDeleteOrders.forEach { logger.info("Found order after delete {}", it) }
90+
7791
}
7892
}

src/main/kotlin/com/example/web/jdbc/web/jdbc/Entities.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ data class BranchData(val buildingType: String?, var rating: Int, var comment: S
3131
data class Person (val id: Long, var firstName: String, var lastName: String)
3232

3333
data class OrderItem (
34+
@Id
35+
@Column("order_item_id")
36+
val id: Long,
3437
var quantity: Int = 0,
3538
var product: String
3639
)
@@ -43,6 +46,6 @@ data class PurchaseOrder (
4346
val items: MutableSet<OrderItem> = HashSet()
4447
) {
4548
fun addItem(quantity: Int, product: String) {
46-
items.add(OrderItem(quantity, product))
49+
items.add(OrderItem(0, quantity, product))
4750
}
4851
}

0 commit comments

Comments
 (0)