Skip to content

Instantly share code, notes, and snippets.

@henninb
Last active August 16, 2021 18:20
Show Gist options
  • Save henninb/a8588eab375dda5fae6e9fa980fd5238 to your computer and use it in GitHub Desktop.
Save henninb/a8588eab375dda5fae6e9fa980fd5238 to your computer and use it in GitHub Desktop.
kotlin enum with jooq
data class Transaction(
@JsonProperty
var transactionId: Long,
@JsonProperty
var guid: String,
@JsonProperty
var accountId: Long,
@JsonProperty
var accountType: AccountType?,
@JsonProperty
var accountNameOwner: String,
@JsonProperty
var transactionDate: Date,
@JsonProperty
var description: String,
@JsonProperty
var category: String,
@JsonProperty
var amount: BigDecimal,
@JsonProperty
var transactionState: TransactionState?,
@JsonProperty
var activeStatus: Boolean = true,
@JsonProperty
var reoccurringType: ReoccurringType? = ReoccurringType.Undefined,
@JsonProperty
var notes: String = ""
) {
constructor() : this(
0L, "", 0, AccountType.Undefined, "", Date(0),
"", "", BigDecimal(0.00), TransactionState.Undefined, true, ReoccurringType.Undefined, ""
)
@JsonGetter("transactionDate")
fun jsonGetterTransactionDate(): String {
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd")
simpleDateFormat.isLenient = false
return simpleDateFormat.format(this.transactionDate)
}
@JsonSetter("transactionDate")
fun jsonSetterTransactionDate(stringDate: String) {
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd")
simpleDateFormat.isLenient = false
this.transactionDate = Date(simpleDateFormat.parse(stringDate).time)
}
@JsonProperty
var dueDate: Date? = null
@JsonIgnore
var receiptImageId: Long? = null
@JsonIgnore
var dateAdded: Timestamp = Timestamp(Calendar.getInstance().time.time)
@JsonIgnore
var dateUpdated: Timestamp = Timestamp(Calendar.getInstance().time.time)
@JsonProperty
var receiptImage: ReceiptImage? = null
@JsonIgnore
var account: Account? = null
@JsonIgnore
var categories = mutableListOf<Category>()
override fun toString(): String {
return mapper.writeValueAsString(this)
}
companion object {
@JsonIgnore
private val mapper = ObjectMapper()
}
}
@JsonFormat
enum class AccountType(val label: String) {
@JsonProperty("credit")
Credit("credit"),
@JsonProperty("debit")
Debit("debit"),
@JsonProperty("undefined")
Undefined("undefined");
}
class TransactionRepository {
suspend fun transactions(): List<Transaction> {
return DatabaseFactory.doJooqQuery {
it.selectFrom(Tables.T_TRANSACTION)
.fetchInto(Transaction::class.java)
}
}
}
@henninb
Copy link
Author

henninb commented Aug 16, 2021

While using Jooq and try to fetchInto transactions my enum AccountType and all other enums are coming back as null. From what I can tell Jooq is not able to map it to a string value. Is there a way to do this mapping?

@henninb
Copy link
Author

henninb commented Aug 16, 2021

This is a case sensitive issue. If I set my enum credit instead of Credit it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment