About
How to map a custom value and type of enum constants into database with Grails Domain.TL;DR
Just add id field to the enum class and set its value for each enum constant.Example case
When modelling domain there is often some enum domain class introduced. Such as WhateverType or SomethingsStatus. Let say we want to use ordinal instead of default GORM's text mapping.class SomeDomainElement { Level level static mapping = { level enumType: 'ordinal' } }Our enum presents itself as follows:
enum Level { EASY, MEDIUM, HARD }Latter introduction of a new level may happen. Lest say ADVANCED. It would be really tempting to place such between levels MEDIUM and HARD. This however would have changed the position number of level HARD. Stop! Database mapping will change either. What about already stored 'levels HARD'?
Solution
Remove the mapping block from SomeDomainElement. It won't be needed any more. Just add the id field to the enum constants.enum Level { EASY(1), MEDIUM(2), ADVANCED(4), HARD(3) final int id private Level(int id) { this.id = id } }The field must be named id so Grails would map it automatically as DB value.
Any serializable and known by Hibernate type can be used instead of int. Like char, String, BigDecimal, Date and so on.
very useful information. thanks
ReplyDeleteMua vé tại đại lý vé máy bay Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ giá rẻ 2021
vé máy bay cam ranh đi hà nội
vé máy bay huế đi sài gòn giá rẻ
giá vé đi nha trang máy bay
ve may bay tu my ve viet nam
taxi sân bay nội bài
Casino Review, Ratings & Facts - JamBase
ReplyDeleteThe Casino is a real 안동 출장샵 time Casino and Gaming 계룡 출장샵 Site. Enjoy casino games 사천 출장안마 and sports betting, 아산 출장샵 live 속초 출장마사지 entertainment, table games, poker, blackjack,
산청출장샵
ReplyDelete군포출장샵
하남출장샵
이천출장샵
하남출장샵
안성출장샵
의왕출장샵
양평출장샵