Last active
August 16, 2021 18:20
-
-
Save henninb/a8588eab375dda5fae6e9fa980fd5238 to your computer and use it in GitHub Desktop.
kotlin enum with jooq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
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
While using Jooq and try to
fetchInto
transactions my enumAccountType
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?