Skip to content

Commit

Permalink
Bug with orderRepository.deleteByShippingAddress spring-projects/spri…
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Konev committed Apr 6, 2022
1 parent cdab926 commit 57ec646
Show file tree
Hide file tree
Showing 9 changed files with 6 additions and 176 deletions.
49 changes: 1 addition & 48 deletions src/main/kotlin/com/example/web/jdbc/web/jdbc/Application.kt
Expand Up @@ -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
Expand All @@ -17,58 +16,12 @@ fun main(args: Array<String>) {

@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()
Expand All @@ -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) }
Expand Down
26 changes: 0 additions & 26 deletions src/main/kotlin/com/example/web/jdbc/web/jdbc/Entities.kt
Expand Up @@ -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<SubjectRef> = 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
Expand Down
37 changes: 1 addition & 36 deletions 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<T> (
private val objectMapper: ObjectMapper
) : Converter<T, PGobject> {
override fun convert(source: T): PGobject {
val jsonObject = PGobject()
jsonObject.type = "json"
jsonObject.value = objectMapper.writeValueAsString(source)
return jsonObject
}
}

abstract class AbstractJsonReadingConverter<T>(
private val objectMapper: ObjectMapper,
private val valueType: Class<T>
) : Converter<PGobject, T> {
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<BranchData>(objectMapper)

@ReadingConverter
class PersonDataReadingConverter(objectMapper: ObjectMapper) : AbstractJsonReadingConverter<BranchData>(objectMapper, BranchData::class.java)
22 changes: 4 additions & 18 deletions 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<Subject, Int>

@Repository
interface BranchRepository : CrudRepository<Branch, Int>

@Repository
interface PersonRepository : CrudRepository<Person, Long> {
fun findByLastName(lastname: String, pageable: Pageable) : Page<Person>
}

interface OrderRepository : CrudRepository<PurchaseOrder, Long> {
@Query("select count(*) from order_item")
fun countItems(): Int

@Modifying
fun deleteByShippingAddress(shippingAddress: String)
}
24 changes: 0 additions & 24 deletions 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
Expand Down
17 changes: 0 additions & 17 deletions src/main/resources/db/changelog/1648380286__init.sql

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions src/main/resources/db/changelog/1648633719__person.sql

This file was deleted.

2 changes: 0 additions & 2 deletions src/main/resources/db/changelog/1648633720__person_doe.sql

This file was deleted.

0 comments on commit 57ec646

Please sign in to comment.