I am mapping a complex JSON response to two JPA Entity model classes using Jackson. The classes are CxExport and Mention, Mention has a Many to one relationship with CxExport I.e. Many mentions belong to one CxExport. The HTTP Response returns a List of mentions 2. The JPA model is a flattened instance of one of the list of mention objects returned in the HTTP response. the Mention JPA Entitys model's field names are different from the ones returned in the JSON response,
my code:
enum class ExportType(var type: Int){
mention(0),
dashboard(1)
}
@SequenceGenerator(name = "seq_cx_export", sequenceName = "SEQ_CX_EXPORT", allocationSize = 1, initialValue = 1)
@Access(AccessType.FIELD)
@Entity
class CxExport(
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_cx_export")
override var pk_id: Long? = null,
var accountId: Long? = null,
@Enumerated
@Column(columnDefinition = "smallint")
var exportType: ExportType? = null,
var exportCount: Int? = null,
var exportStart: Timestamp? = null,
var exportEnd: Timestamp? = null
) : AbstractJpaPersistable<Long>() {}
@SequenceGenerator(name = "seq_ment", sequenceName = "SEQ_MENTION", allocationSize = 1, initialValue = 1)
@Access(AccessType.FIELD)
@Entity
class Mention(
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_ment")
override var pk_id: Long? = null,
@ManyToOne(cascade = [(CascadeType.MERGE)])
@JoinColumn(name = "export_id")
var cxExport: CxExport,
var authorId: String? = null,
var authorUrl: String? = null,
var authorImg: String? = null,
var authorTags: String?,
var origAuthorName: String? = null,
var messageTitle: String? = null,
@Lob
var messageContent: String? = null,
var messageLanguage: String? = null,
var messageSentiment: String? = null,
var messageType: String? = null,
var sourceId: String? = null,
var sourceCategory: String? = null,
var sourceType: String? = null,
var sourceDomain: String? = null,
var sourceUrl: String? = null,
var sourceProfile: String? = null,
var sourceProfileName: String? = null,
var locContinent: String? = null,
var locCountry: String? = null,
var locCity: String? = null,
var locRegion: String? = null,
var locLongitude: String? = null,
var locLatitude: String? = null,
var permalink: String? = null,
var priorityId: Int? = null,
var priorityName: String? = null,
var priorityColor: String? = null,
var responseTime: Int? = null,
var resolveTime: Int? = null,
var handleTime: Int? = null,
var dateAdded: Int? = null,
var datePublished: Int? = null
) : AbstractJpaPersistable<Long>()
@MappedSuperclass
abstract class AbstractJpaPersistable<T: Serializable> : Persistable<T> {
companion object {
var serialVersionUID = -5554308939380869754L
}
abstract var pk_id: T?
override fun getId() : T? {
return pk_id
}
override fun isNew() = null == getId()
override fun toString() ="Entity oftype ${this.javaClass.name} with id: $id"
override fun equals(other: Any?): Boolean {
other ?: return false
if (this === other) return true
if (javaClass != ProxyUtils.getUserClass(other)) return false
other as AbstractJpaPersistable<*>
return if (null == this.getId()) false else this.getId() == other.getId()
}
override fun hashCode(): Int {
return 58
}
}
fun deserializeMentions(body: String) {
var export = CxExport()
var response = mapper.readTree(body)
var count = response["response"]["count"].asInt()
export.exportCount = count
export.accountId= 51216
export.exportType = ExportType.mention
export.exportStart = Timestamp.from(Instant.now())
export.exportEnd = Timestamp.from(Instant.now())
cxExportRepo.saveAndFlush(export)
var mentions: MutableList<Mention> = ArrayList()
var data = response["response"]["data"]
data.forEach {
println(it["permalink"]?.asText())
var locContinent: String? = null
var locCountry: String? = null
var locCity: String? = null
var locRegion: String? = null
var locLongitude: String? = null
var locLatitude: String? = null
var priorityId: Int? = null
var priorityName: String? = null
var priorityColor: String? = null
it["priority"]?.let {
priorityId = it["id"]?.asInt()
priorityColor = it["color"]?.asText()
priorityName = it["name"]?.asText()
}
it["location"]?.let {
locContinent = it["continent"]?.asText()
locCountry = it["country"]?.asText()
locCity = it["city"]?.asText()
locRegion = it["region"]?.asText()
locLongitude = it["longitude"]?.asText()
locLatitude = it["latitude"]?.asText()
}
var tags: String? = null
it["author"]["tags"]?.forEach {
it?.asText()?.let {
tags += it
}
}
mentions.add(Mention(
cxExport = export,
authorId = it["author"]["id"]?.asText(),
authorUrl = it["author"]["url"]?.asText(),
authorImg = it["author"]["img"]?.asText(),
authorTags = tags,
messageTitle =it["message"]["title"]?.asText(),
messageContent =it["message"]["content"]?.asText(),
messageLanguage = it["message"]["language"]?.asText(),
messageSentiment =it["message"]["sentiment"]?.asText(),
sourceId = it["source"]["id"]?.asText(),
sourceCategory = it["source"]["category"]?.asText(),
sourceType = it["source"]["type"]?.asText(),
sourceDomain = it["source"]["domain"]?.asText(),
sourceUrl = it["source"]["url"]?.asText(),
sourceProfile = it["source"]["profile_id"]?.asText(),
sourceProfileName = it["source"]["profile_name"]?.asText(),
locContinent = locContinent,
locCountry = locCountry,
locCity = locCity,
locRegion = locRegion,
locLongitude = locLongitude,
locLatitude = locLatitude,
permalink = it["permalink"]?.asText(),
priorityId = priorityId,
priorityName = priorityName,
priorityColor = priorityColor,
responseTime = it["timestamps"]["response_time"]?.asInt(),
resolveTime = it["timestamps"]["resolve_time"]?.asInt(),
handleTime = it["timestamps"]["handle_time"]?.asInt(),
dateAdded = it["date"]["added"]?.asInt(),
datePublished = it["date"]["published"]?.asInt()
)
)
}
mentionRepo.saveAll(mentions)
}