tag:blogger.com,1999:blog-5763764290649132593.post519286692845956842..comments2023-08-09T13:17:49.225+02:00Comments on CLOSED-LOOP: .Net QuickTip: Persisting enums as strings with NHibernateAnonymoushttp://www.blogger.com/profile/00990537252799084615noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-5763764290649132593.post-39942253466264742472010-04-23T12:53:08.395+02:002010-04-23T12:53:08.395+02:00@Urs Thanks, interesting position, will have to sl...@Urs Thanks, interesting position, will have to sleep over that :-)<br /><br />@bertolami in JPA its actually much easier:<br />Mapping enums with attributes is done like this:<br /><br />@Enumerated(EnumType.STRING)<br />private Rating rating;<br /><br />Where EnumType can be STRING or ORDINAL.<br />(probably there is an equivalence to do it in xml)<br /><br />Last time I checked plain Hibernate you still had to write a custom user type:<br /><a href="http://community.jboss.org/wiki/Java5EnumUserType" rel="nofollow"> http://community.jboss.org/wiki/Java5EnumUserType</a><br /><br />Enums are actually one of the few language things I like better in Java than in .Net...<br />In Java, enums are full fledged named instances of a type, while in c# enums are basically named constants.Anonymoushttps://www.blogger.com/profile/00990537252799084615noreply@blogger.comtag:blogger.com,1999:blog-5763764290649132593.post-60491346884632210462010-04-23T10:52:42.768+02:002010-04-23T10:52:42.768+02:00@jbandi is this similarly possible for the java ve...@jbandi is this similarly possible for the java version of hibernate?<br /><br />@urs probably you are lucky your enum breaks and you notice your inconistency rather early in the change process. If you consider an enum as the set of values for a very specific issue that your application currently supports, I see no point of not using them. On the other hand, the implementation of enums in Java is rather poor, mainly because of the poor implementation of class members in Java. Thus there may be reasons not to use them.bertolaminoreply@blogger.comtag:blogger.com,1999:blog-5763764290649132593.post-73304676849810116672010-04-22T22:02:03.089+02:002010-04-22T22:02:03.089+02:00Just a related thought:
I wouldn't store enums...Just a related thought:<br />I wouldn't store enums in the database. The risk of a braking change in a follow-up version is just too great. Saving as strings is surely better than saving as int, but I suggest to abandon enums completely (when persisted) and choose simple constants. Note that enums are NOT constants. It is just too simple to make a mess by introducing or deleting a value changing all others. Yes I know that you can set a value per enum value, but then a set of constants is even simpler ;-)<br />Extensibility is another problem inherent in enums (see point 3 in http://www.planetgeek.ch/2009/02/07/top-10-of-software-design-evilness/)<br /><br />Fight the enums :-D<br /><br />Cheers,<br />UrsUrs Enzlerhttps://www.blogger.com/profile/10173393221263632511noreply@blogger.com