diff --git a/src/main/kotlin/com/example/web/jdbc/web/jdbc/Application.kt b/src/main/kotlin/com/example/web/jdbc/web/jdbc/Application.kt index c506426..7270238 100644 --- a/src/main/kotlin/com/example/web/jdbc/web/jdbc/Application.kt +++ b/src/main/kotlin/com/example/web/jdbc/web/jdbc/Application.kt @@ -5,7 +5,6 @@ import org.springframework.boot.ApplicationArguments import org.springframework.boot.ApplicationRunner import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication -import org.springframework.data.domain.PageRequest import org.springframework.stereotype.Component @SpringBootApplication @@ -17,58 +16,12 @@ fun main(args: Array) { @Component class AppRunner( - private val subjectRepository: SubjectRepository, - private val branchRepository: BranchRepository, - private val personRepository: PersonRepository, private val orderRepository: OrderRepository, ) : ApplicationRunner { private val logger = LoggerFactory.getLogger(this::class.java) override fun run(args: ApplicationArguments) { - branchRepository.deleteAll() - subjectRepository.deleteAll() - - val subj1: Subject = subjectRepository.save(Subject(0, "Software Engineering", "Apply key aspects of software engineering processes for the development of a complex software system")) - val subj2: Subject = subjectRepository.save(Subject(0, "Distributed System", "Explore recent advances in distributed computing systems")) - val subj3: Subject = subjectRepository.save(Subject(0, "Business Analysis and Optimization", "understand the Internal and external factors that impact the business strategy")) - - val branch1: Branch = Branch(0, "Computer Science and Engineering", "CSE", "CSE department offers courses under ambitious curricula in computer science and computer engineering..") - branch1.addSubject(subj1) - branch1.addSubject(subj2) - branch1.branchData = BranchData("Classic office", 100, "A pretty good office") - val createdBranch1: Branch = branchRepository.save(branch1) - logger.info("Created first branch {}", createdBranch1) - - val branch2: Branch = Branch(0, "Information Technology", "IT", "IT is the business side of computers - usually dealing with databases, business, and accounting") - branch2.addSubject(subj1) - branch2.addSubject(subj3) - val createdBranch2: Branch = branchRepository.save(branch2) - logger.info("Created second branch {}", createdBranch2) - - val findById = branchRepository.findById(createdBranch1.branchId) - logger.info("Found --- first branch {}", findById) - - logger.info("Deleting first branch {}", createdBranch1) - branchRepository.delete(createdBranch1) - logger.info("Searching for first branch {}", branchRepository.existsById(createdBranch1.branchId)) - - logger.info("Deleting second branch {}", createdBranch2) - branchRepository.delete(createdBranch2) - logger.info("Searching for second branch {}", branchRepository.existsById(createdBranch2.branchId)) - - logger.info("Checking if branches still presents") - val allBranches = branchRepository.findAll() - allBranches.forEach { logger.info("Found branch {}", it) } - - logger.info("Checking if subjects still presents") - val allSubjects = subjectRepository.findAll() - allSubjects.forEach { logger.info("Found subject {}", it) } - - - // https://docs.spring.io/spring-data/jdbc/docs/current/reference/html/#jdbc.query-methods - val foundPersons = personRepository.findByLastName("Doe", PageRequest.of(1, 10)) - foundPersons.forEach { logger.info("Found Person {}", it) } // https://spring.io/blog/2018/09/24/spring-data-jdbc-references-and-aggregates // val orders = orderRepository.findAll() @@ -83,7 +36,7 @@ class AppRunner( orders.forEach { logger.info("Found order {}", it) } logger.info("=== deleting order with its items ===") - orderRepository.delete(savedOrder) + orderRepository.deleteByShippingAddress("Kutaisi") val afterDeleteOrders = orderRepository.findAll() afterDeleteOrders.forEach { logger.info("Found order after delete {}", it) } diff --git a/src/main/kotlin/com/example/web/jdbc/web/jdbc/Entities.kt b/src/main/kotlin/com/example/web/jdbc/web/jdbc/Entities.kt index 0b8301e..68b55ae 100644 --- a/src/main/kotlin/com/example/web/jdbc/web/jdbc/Entities.kt +++ b/src/main/kotlin/com/example/web/jdbc/web/jdbc/Entities.kt @@ -3,32 +3,6 @@ package com.example.web.jdbc.web.jdbc import org.springframework.data.annotation.Id import org.springframework.data.relational.core.mapping.Column import org.springframework.data.relational.core.mapping.MappedCollection -import org.springframework.data.relational.core.mapping.Table - - -data class Subject(@Id val subjectId: Int, var subjectDesc: String, var subjectName: String) - -@Table("branch_subject") -data class SubjectRef(val subjectId: Int) - -data class Branch( - @Id val branchId: Int, - var branchName: String, - @Column("branch_short_name") var branchShortName: String, - var description: String? = null, - @MappedCollection(idColumn = "branch_id") - private val subjects: MutableSet = HashSet(), - var branchData: BranchData? = null -) { - - fun addSubject(subject: Subject) { - subjects.add(SubjectRef(subject.subjectId)) - } -} - -data class BranchData(val buildingType: String?, var rating: Int, var comment: String?) - -data class Person (val id: Long, var firstName: String, var lastName: String) data class OrderItem ( @Id diff --git a/src/main/kotlin/com/example/web/jdbc/web/jdbc/JdbcConfig.kt b/src/main/kotlin/com/example/web/jdbc/web/jdbc/JdbcConfig.kt index a5b9af5..8a7a4b1 100644 --- a/src/main/kotlin/com/example/web/jdbc/web/jdbc/JdbcConfig.kt +++ b/src/main/kotlin/com/example/web/jdbc/web/jdbc/JdbcConfig.kt @@ -1,45 +1,10 @@ package com.example.web.jdbc.web.jdbc -import com.fasterxml.jackson.databind.ObjectMapper -import org.postgresql.util.PGobject import org.springframework.context.annotation.Configuration -import org.springframework.core.convert.converter.Converter -import org.springframework.data.convert.ReadingConverter -import org.springframework.data.convert.WritingConverter -import org.springframework.data.jdbc.core.convert.JdbcCustomConversions import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration -abstract class AbstractJsonWritingConverter ( - private val objectMapper: ObjectMapper -) : Converter { - override fun convert(source: T): PGobject { - val jsonObject = PGobject() - jsonObject.type = "json" - jsonObject.value = objectMapper.writeValueAsString(source) - return jsonObject - } -} - -abstract class AbstractJsonReadingConverter( - private val objectMapper: ObjectMapper, - private val valueType: Class -) : Converter { - override fun convert(pgObject: PGobject): T? { - val source = pgObject.value - return objectMapper.readValue(source, valueType) - } -} @Configuration -class JdbcConfig(private val objectMapper: ObjectMapper) : AbstractJdbcConfiguration() { +class JdbcConfig : AbstractJdbcConfiguration() { - override fun userConverters() = listOf( - PersonDataWritingConverter(objectMapper), PersonDataReadingConverter(objectMapper), - ) } - -@WritingConverter -class PersonDataWritingConverter(objectMapper: ObjectMapper) : AbstractJsonWritingConverter(objectMapper) - -@ReadingConverter -class PersonDataReadingConverter(objectMapper: ObjectMapper) : AbstractJsonReadingConverter(objectMapper, BranchData::class.java) diff --git a/src/main/kotlin/com/example/web/jdbc/web/jdbc/Repositories.kt b/src/main/kotlin/com/example/web/jdbc/web/jdbc/Repositories.kt index c6344d7..af7879c 100644 --- a/src/main/kotlin/com/example/web/jdbc/web/jdbc/Repositories.kt +++ b/src/main/kotlin/com/example/web/jdbc/web/jdbc/Repositories.kt @@ -1,24 +1,10 @@ package com.example.web.jdbc.web.jdbc -import org.springframework.data.domain.Page -import org.springframework.data.domain.Pageable -import org.springframework.data.jdbc.repository.query.Query +import org.springframework.data.jdbc.repository.query.Modifying import org.springframework.data.repository.CrudRepository -import org.springframework.stereotype.Repository - - -@Repository -interface SubjectRepository : CrudRepository - -@Repository -interface BranchRepository : CrudRepository - -@Repository -interface PersonRepository : CrudRepository { - fun findByLastName(lastname: String, pageable: Pageable) : Page -} interface OrderRepository : CrudRepository { - @Query("select count(*) from order_item") - fun countItems(): Int + + @Modifying + fun deleteByShippingAddress(shippingAddress: String) } \ No newline at end of file diff --git a/src/main/resources/db/changelog.yml b/src/main/resources/db/changelog.yml index 01218fc..120d369 100644 --- a/src/main/resources/db/changelog.yml +++ b/src/main/resources/db/changelog.yml @@ -1,28 +1,4 @@ databaseChangeLog: - - changeSet: - id: 1 - author: nkonev - changes: - - sqlFile: - path: /db/changelog/1648380286__init.sql - - changeSet: - id: 2 - author: nkonev - changes: - - sqlFile: - path: /db/changelog/1648459518__branch_data_jsonb.sql - - changeSet: - id: 3 - author: nkonev - changes: - - sqlFile: - path: /db/changelog/1648633719__person.sql - - changeSet: - id: 4 - author: nkonev - changes: - - sqlFile: - path: /db/changelog/1648633720__person_doe.sql - changeSet: id: 5 author: nkonev diff --git a/src/main/resources/db/changelog/1648380286__init.sql b/src/main/resources/db/changelog/1648380286__init.sql deleted file mode 100644 index b266a51..0000000 --- a/src/main/resources/db/changelog/1648380286__init.sql +++ /dev/null @@ -1,17 +0,0 @@ -CREATE TABLE BRANCH ( - BRANCH_ID SERIAL PRIMARY KEY, - BRANCH_SHORT_NAME varchar(45) NOT NULL, - BRANCH_NAME varchar(100) NOT NULL, - DESCRIPTION varchar(200) DEFAULT NULL -); - -CREATE TABLE SUBJECT ( - SUBJECT_ID SERIAL PRIMARY KEY, - SUBJECT_NAME varchar(100) NOT NULL, - SUBJECT_DESC varchar(200) DEFAULT NULL -); - -CREATE TABLE BRANCH_SUBJECT ( - BRANCH_ID int REFERENCES BRANCH (BRANCH_ID), - SUBJECT_ID int REFERENCES SUBJECT (SUBJECT_ID) -); \ No newline at end of file diff --git a/src/main/resources/db/changelog/1648459518__branch_data_jsonb.sql b/src/main/resources/db/changelog/1648459518__branch_data_jsonb.sql deleted file mode 100644 index 9d0b8a4..0000000 --- a/src/main/resources/db/changelog/1648459518__branch_data_jsonb.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE BRANCH ADD COLUMN branch_data JSONB; \ No newline at end of file diff --git a/src/main/resources/db/changelog/1648633719__person.sql b/src/main/resources/db/changelog/1648633719__person.sql deleted file mode 100644 index 662e249..0000000 --- a/src/main/resources/db/changelog/1648633719__person.sql +++ /dev/null @@ -1,4 +0,0 @@ -CREATE TABLE person(id BIGSERIAL PRIMARY KEY, first_name text not null , last_name text not null); - -INSERT INTO person (first_name, last_name) -SELECT 'John' || i, 'Doe' || i FROM generate_series(0, 200) AS i; \ No newline at end of file diff --git a/src/main/resources/db/changelog/1648633720__person_doe.sql b/src/main/resources/db/changelog/1648633720__person_doe.sql deleted file mode 100644 index 2daef21..0000000 --- a/src/main/resources/db/changelog/1648633720__person_doe.sql +++ /dev/null @@ -1,2 +0,0 @@ -INSERT INTO person (first_name, last_name) -SELECT 'John' || i, 'Doe' FROM generate_series(0, 50) AS i; \ No newline at end of file