tag:blogger.com,1999:blog-535871139796137802.post2972815332485888933..comments2022-12-05T05:19:05.534+02:00Comments on Chris Ritchies Java Blog: Mapping enums with a fixed ID in JPA - An alternative to String and OrdinalChrishttp://www.blogger.com/profile/17099003228715114731noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-535871139796137802.post-72305354339783401832018-01-08T06:30:23.900+02:002018-01-08T06:30:23.900+02:00"Note: by using an integer data type rather t..."Note: by using an integer data type rather than a String data type, will mean quicker database queries"<br /><br />Not always true. char (1 byte) is more space-efficient (both in disk and RAM) and will be more performant than smallint (2 bytes) and integer (4 bytes), as most enums have no more than 255 distinct values. Your example uses 4 values in a space of 4 bytes, it fits within a char and even a smallint. As enums are usually indexed this matters even more. For non-indexed enums this will matter much less.Hendyhttps://www.blogger.com/profile/05192845149798446052noreply@blogger.comtag:blogger.com,1999:blog-535871139796137802.post-70556733263457118212017-11-25T08:39:13.830+02:002017-11-25T08:39:13.830+02:00greatgreatfor ict 99https://www.blogger.com/profile/01234222908168002434noreply@blogger.comtag:blogger.com,1999:blog-535871139796137802.post-45545764340647927452015-10-27T18:06:43.801+02:002015-10-27T18:06:43.801+02:00Hi, I find the lumpynose's approach interestin...Hi, I find the lumpynose's approach interesting as the converter will only be as followed<br /><br />@Converter<br />public class PositionConverter implements AttributeConverter {<br /> @Override<br /> public Integer convertToDatabaseColumn(final Position attribute) {<br /> return attribute.getId();<br /> }<br /><br /> @Override<br /> public Position convertToEntityAttribute(final String dbData) {<br /> return Position.fromInteger(dbData);<br /> }<br />}<br />D.https://www.blogger.com/profile/05754587005374054597noreply@blogger.comtag:blogger.com,1999:blog-535871139796137802.post-32432158634298538412015-04-22T02:02:00.495+02:002015-04-22T02:02:00.495+02:00Thanks a lot, your replay made me look for a bette...Thanks a lot, your replay made me look for a better way to implement enums in JPA, I ended up implementing converters, their funcionality is exatly the one I was looking for. JRhttps://www.blogger.com/profile/02427877031702449548noreply@blogger.comtag:blogger.com,1999:blog-535871139796137802.post-77719783238725575652014-10-17T23:59:30.247+02:002014-10-17T23:59:30.247+02:00By "all the converter methods do is call thos...By "all the converter methods do is call those enum methods" I meant the fromInteger() and getId(). You'd might prefer to rename fromInteger() to fromId() for consistency.lumpynosehttps://www.blogger.com/profile/10098634476010203590noreply@blogger.comtag:blogger.com,1999:blog-535871139796137802.post-14434027114624190792014-10-17T23:52:38.591+02:002014-10-17T23:52:38.591+02:00To answer Thor's question, in the enum put the...To answer Thor's question, in the enum put the following code:<br />private final static Map<Integer, Position> intToEnum = new HashMap<>();<br />static {<br />for (Position pos : values())<br /> intToEnum.put(pos.id, pos);<br />}<br />public static Position fromInteger(Integer id) {<br /> return intToEnum.get(id);<br />}<br /><br />This is from Effective Java, 2nd ed, item 30, page 154.<br /><br />I'm using this with the @Converter and all the converter methods do is call those enum methods; 1 line or so of code in each method.lumpynosehttps://www.blogger.com/profile/10098634476010203590noreply@blogger.comtag:blogger.com,1999:blog-535871139796137802.post-5311572694573551962014-04-17T14:19:12.030+02:002014-04-17T14:19:12.030+02:00I don't really understand your question, could...I don't really understand your question, could you clarify?Chrishttps://www.blogger.com/profile/17099003228715114731noreply@blogger.comtag:blogger.com,1999:blog-535871139796137802.post-17817489595567339822014-04-17T14:02:46.503+02:002014-04-17T14:02:46.503+02:00I have seen several implementations of this, may u...I have seen several implementations of this, may using a hashmap for reverse mapping?Thorhttps://www.blogger.com/profile/15122492444425462178noreply@blogger.comtag:blogger.com,1999:blog-535871139796137802.post-25677484165858331302014-02-27T09:17:01.468+02:002014-02-27T09:17:01.468+02:00I used to think this. From a database point of vie...I used to think this. From a database point of view, it it more correct to have the enum ID's, values and descriptions in a look up table, but from the Java side, it is quicker and simpler to use the enum directly. Plus there is no need for the extra join in the database.<br /><br />The down side to this is that if you are querying the database directly, you need to know which ID (example above - positionId in player) means what, and for this you will need to look at the Java enum.<br />Chrishttps://www.blogger.com/profile/17099003228715114731noreply@blogger.comtag:blogger.com,1999:blog-535871139796137802.post-60465524678328678772014-02-27T03:53:39.222+02:002014-02-27T03:53:39.222+02:00So, You don't recommend saving position descri...So, You don't recommend saving position description along with it's id into database for future queries ease?JRhttps://www.blogger.com/profile/02427877031702449548noreply@blogger.comtag:blogger.com,1999:blog-535871139796137802.post-33264028871038110262013-11-24T14:22:19.060+02:002013-11-24T14:22:19.060+02:00this is a clever idea ;) this is a clever idea ;) Houssemhttps://www.blogger.com/profile/01703774059228916603noreply@blogger.com