Skip to content

Commit 707691f

Browse files
author
Nikita Konev
committed
Jsonb
1 parent 798a33e commit 707691f

File tree

6 files changed

+66
-4
lines changed

6 files changed

+66
-4
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
<groupId>org.springframework.boot</groupId>
2222
<artifactId>spring-boot-starter-data-jdbc</artifactId>
2323
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-json</artifactId>
27+
</dependency>
2428
<dependency>
2529
<groupId>com.fasterxml.jackson.module</groupId>
2630
<artifactId>jackson-module-kotlin</artifactId>
@@ -66,6 +70,8 @@
6670
<compilerPlugins>
6771
<plugin>spring</plugin>
6872
</compilerPlugins>
73+
<!-- https://kotlinlang.org/docs/maven.html#specifying-compiler-options current kotlin yet not supports Java 17 target -->
74+
<jvmTarget>16</jvmTarget>
6975
</configuration>
7076
<dependencies>
7177
<dependency>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ class AppRunner(private val subjectRepository: SubjectRepository,
2727
val subj1: Subject = subjectRepository.save(Subject(0, "Software Engineering", "Apply key aspects of software engineering processes for the development of a complex software system"))
2828
val subj2: Subject = subjectRepository.save(Subject(0, "Distributed System", "Explore recent advances in distributed computing systems"))
2929
val subj3: Subject = subjectRepository.save(Subject(0, "Business Analysis and Optimization", "understand the Internal and external factors that impact the business strategy"))
30+
3031
val branch1: Branch = Branch(0, "Computer Science and Engineering", "CSE", "CSE department offers courses under ambitious curricula in computer science and computer engineering..")
3132
branch1.addSubject(subj1)
3233
branch1.addSubject(subj2)
34+
branch1.branchData = BranchData("Classic office", 100, "A pretty good office")
3335
val createdBranch1: Branch = branchRepository.save(branch1)
3436
logger.info("Created first branch {}", createdBranch1)
3537

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ data class Branch(
1717
@Column("branch_short_name") var branchShortName: String,
1818
var description: String? = null,
1919
@MappedCollection(idColumn = "branch_id")
20-
private val subjects: MutableSet<SubjectRef> = HashSet()
20+
private val subjects: MutableSet<SubjectRef> = HashSet(),
21+
var branchData: BranchData? = null
2122
) {
2223

2324
fun addSubject(subject: Subject) {
2425
subjects.add(SubjectRef(subject.subjectId.toLong()))
2526
}
26-
}
27+
}
28+
29+
data class BranchData(val buildingType: String?, var rating: Int, var comment: String?)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.example.web.jdbc.web.jdbc
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper
4+
import org.postgresql.util.PGobject
5+
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
10+
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration
11+
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+
}
32+
33+
@Configuration
34+
class JdbcConfig(private val objectMapper: ObjectMapper) : AbstractJdbcConfiguration() {
35+
36+
override fun userConverters() = listOf(
37+
PersonDataWritingConverter(objectMapper), PersonDataReadingConverter(objectMapper),
38+
)
39+
}
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)

src/main/resources/db/changelog.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ databaseChangeLog:
22
- changeSet:
33
id: 1
44
author: nkonev
5-
65
changes:
76
- sqlFile:
8-
path: /db/changelog/1648380286__init.sql
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
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE BRANCH ADD COLUMN branch_data JSONB;

0 commit comments

Comments
 (0)