Skip to content

Commit b01aaee

Browse files
author
Nikita Konev
committed
1 parent cdab926 commit b01aaee

File tree

9 files changed

+6
-176
lines changed

9 files changed

+6
-176
lines changed

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

+1-48
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import org.springframework.boot.ApplicationArguments
55
import org.springframework.boot.ApplicationRunner
66
import org.springframework.boot.autoconfigure.SpringBootApplication
77
import org.springframework.boot.runApplication
8-
import org.springframework.data.domain.PageRequest
98
import org.springframework.stereotype.Component
109

1110
@SpringBootApplication
@@ -17,58 +16,12 @@ fun main(args: Array<String>) {
1716

1817
@Component
1918
class AppRunner(
20-
private val subjectRepository: SubjectRepository,
21-
private val branchRepository: BranchRepository,
22-
private val personRepository: PersonRepository,
2319
private val orderRepository: OrderRepository,
2420
) : ApplicationRunner {
2521

2622
private val logger = LoggerFactory.getLogger(this::class.java)
2723

2824
override fun run(args: ApplicationArguments) {
29-
branchRepository.deleteAll()
30-
subjectRepository.deleteAll()
31-
32-
val subj1: Subject = subjectRepository.save(Subject(0, "Software Engineering", "Apply key aspects of software engineering processes for the development of a complex software system"))
33-
val subj2: Subject = subjectRepository.save(Subject(0, "Distributed System", "Explore recent advances in distributed computing systems"))
34-
val subj3: Subject = subjectRepository.save(Subject(0, "Business Analysis and Optimization", "understand the Internal and external factors that impact the business strategy"))
35-
36-
val branch1: Branch = Branch(0, "Computer Science and Engineering", "CSE", "CSE department offers courses under ambitious curricula in computer science and computer engineering..")
37-
branch1.addSubject(subj1)
38-
branch1.addSubject(subj2)
39-
branch1.branchData = BranchData("Classic office", 100, "A pretty good office")
40-
val createdBranch1: Branch = branchRepository.save(branch1)
41-
logger.info("Created first branch {}", createdBranch1)
42-
43-
val branch2: Branch = Branch(0, "Information Technology", "IT", "IT is the business side of computers - usually dealing with databases, business, and accounting")
44-
branch2.addSubject(subj1)
45-
branch2.addSubject(subj3)
46-
val createdBranch2: Branch = branchRepository.save(branch2)
47-
logger.info("Created second branch {}", createdBranch2)
48-
49-
val findById = branchRepository.findById(createdBranch1.branchId)
50-
logger.info("Found --- first branch {}", findById)
51-
52-
logger.info("Deleting first branch {}", createdBranch1)
53-
branchRepository.delete(createdBranch1)
54-
logger.info("Searching for first branch {}", branchRepository.existsById(createdBranch1.branchId))
55-
56-
logger.info("Deleting second branch {}", createdBranch2)
57-
branchRepository.delete(createdBranch2)
58-
logger.info("Searching for second branch {}", branchRepository.existsById(createdBranch2.branchId))
59-
60-
logger.info("Checking if branches still presents")
61-
val allBranches = branchRepository.findAll()
62-
allBranches.forEach { logger.info("Found branch {}", it) }
63-
64-
logger.info("Checking if subjects still presents")
65-
val allSubjects = subjectRepository.findAll()
66-
allSubjects.forEach { logger.info("Found subject {}", it) }
67-
68-
69-
// https://docs.spring.io/spring-data/jdbc/docs/current/reference/html/#jdbc.query-methods
70-
val foundPersons = personRepository.findByLastName("Doe", PageRequest.of(1, 10))
71-
foundPersons.forEach { logger.info("Found Person {}", it) }
7225

7326
// https://spring.io/blog/2018/09/24/spring-data-jdbc-references-and-aggregates
7427
// val orders = orderRepository.findAll()
@@ -83,7 +36,7 @@ class AppRunner(
8336
orders.forEach { logger.info("Found order {}", it) }
8437

8538
logger.info("=== deleting order with its items ===")
86-
orderRepository.delete(savedOrder)
39+
orderRepository.deleteByShippingAddress("Kutaisi")
8740

8841
val afterDeleteOrders = orderRepository.findAll()
8942
afterDeleteOrders.forEach { logger.info("Found order after delete {}", it) }

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

-26
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,6 @@ package com.example.web.jdbc.web.jdbc
33
import org.springframework.data.annotation.Id
44
import org.springframework.data.relational.core.mapping.Column
55
import org.springframework.data.relational.core.mapping.MappedCollection
6-
import org.springframework.data.relational.core.mapping.Table
7-
8-
9-
data class Subject(@Id val subjectId: Int, var subjectDesc: String, var subjectName: String)
10-
11-
@Table("branch_subject")
12-
data class SubjectRef(val subjectId: Int)
13-
14-
data class Branch(
15-
@Id val branchId: Int,
16-
var branchName: String,
17-
@Column("branch_short_name") var branchShortName: String,
18-
var description: String? = null,
19-
@MappedCollection(idColumn = "branch_id")
20-
private val subjects: MutableSet<SubjectRef> = HashSet(),
21-
var branchData: BranchData? = null
22-
) {
23-
24-
fun addSubject(subject: Subject) {
25-
subjects.add(SubjectRef(subject.subjectId))
26-
}
27-
}
28-
29-
data class BranchData(val buildingType: String?, var rating: Int, var comment: String?)
30-
31-
data class Person (val id: Long, var firstName: String, var lastName: String)
326

337
data class OrderItem (
348
@Id
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,10 @@
11
package com.example.web.jdbc.web.jdbc
22

3-
import com.fasterxml.jackson.databind.ObjectMapper
4-
import org.postgresql.util.PGobject
53
import org.springframework.context.annotation.Configuration
6-
import org.springframework.core.convert.converter.Converter
7-
import org.springframework.data.convert.ReadingConverter
8-
import org.springframework.data.convert.WritingConverter
9-
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions
104
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration
115

12-
abstract class AbstractJsonWritingConverter<T> (
13-
private val objectMapper: ObjectMapper
14-
) : Converter<T, PGobject> {
15-
override fun convert(source: T): PGobject {
16-
val jsonObject = PGobject()
17-
jsonObject.type = "json"
18-
jsonObject.value = objectMapper.writeValueAsString(source)
19-
return jsonObject
20-
}
21-
}
22-
23-
abstract class AbstractJsonReadingConverter<T>(
24-
private val objectMapper: ObjectMapper,
25-
private val valueType: Class<T>
26-
) : Converter<PGobject, T> {
27-
override fun convert(pgObject: PGobject): T? {
28-
val source = pgObject.value
29-
return objectMapper.readValue(source, valueType)
30-
}
31-
}
326

337
@Configuration
34-
class JdbcConfig(private val objectMapper: ObjectMapper) : AbstractJdbcConfiguration() {
8+
class JdbcConfig : AbstractJdbcConfiguration() {
359

36-
override fun userConverters() = listOf(
37-
PersonDataWritingConverter(objectMapper), PersonDataReadingConverter(objectMapper),
38-
)
3910
}
40-
41-
@WritingConverter
42-
class PersonDataWritingConverter(objectMapper: ObjectMapper) : AbstractJsonWritingConverter<BranchData>(objectMapper)
43-
44-
@ReadingConverter
45-
class PersonDataReadingConverter(objectMapper: ObjectMapper) : AbstractJsonReadingConverter<BranchData>(objectMapper, BranchData::class.java)
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
11
package com.example.web.jdbc.web.jdbc
22

3-
import org.springframework.data.domain.Page
4-
import org.springframework.data.domain.Pageable
5-
import org.springframework.data.jdbc.repository.query.Query
3+
import org.springframework.data.jdbc.repository.query.Modifying
64
import org.springframework.data.repository.CrudRepository
7-
import org.springframework.stereotype.Repository
8-
9-
10-
@Repository
11-
interface SubjectRepository : CrudRepository<Subject, Int>
12-
13-
@Repository
14-
interface BranchRepository : CrudRepository<Branch, Int>
15-
16-
@Repository
17-
interface PersonRepository : CrudRepository<Person, Long> {
18-
fun findByLastName(lastname: String, pageable: Pageable) : Page<Person>
19-
}
205

216
interface OrderRepository : CrudRepository<PurchaseOrder, Long> {
22-
@Query("select count(*) from order_item")
23-
fun countItems(): Int
7+
8+
@Modifying
9+
fun deleteByShippingAddress(shippingAddress: String)
2410
}

src/main/resources/db/changelog.yml

-24
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,4 @@
11
databaseChangeLog:
2-
- changeSet:
3-
id: 1
4-
author: nkonev
5-
changes:
6-
- sqlFile:
7-
path: /db/changelog/1648380286__init.sql
8-
- changeSet:
9-
id: 2
10-
author: nkonev
11-
changes:
12-
- sqlFile:
13-
path: /db/changelog/1648459518__branch_data_jsonb.sql
14-
- changeSet:
15-
id: 3
16-
author: nkonev
17-
changes:
18-
- sqlFile:
19-
path: /db/changelog/1648633719__person.sql
20-
- changeSet:
21-
id: 4
22-
author: nkonev
23-
changes:
24-
- sqlFile:
25-
path: /db/changelog/1648633720__person_doe.sql
262
- changeSet:
273
id: 5
284
author: nkonev

src/main/resources/db/changelog/1648380286__init.sql

-17
This file was deleted.

src/main/resources/db/changelog/1648459518__branch_data_jsonb.sql

-1
This file was deleted.

src/main/resources/db/changelog/1648633719__person.sql

-4
This file was deleted.

src/main/resources/db/changelog/1648633720__person_doe.sql

-2
This file was deleted.

0 commit comments

Comments
 (0)