-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Take 2 : mapping Java enum whitout annotation
PostPosted: Tue Jun 17, 2008 8:36 am 
Newbie

Joined: Mon Jun 16, 2008 8:28 am
Posts: 4
Hi,

Pretty simple question here. I've read lots of posts in the forum, but I did not find an explicit answer to this and I'm having a hard time believing there is none.

I have the simplest mapping you can imagine (public enum MyEnum { EnumA, EnumB; })
I'm using Hibernate 3.1.3.
I do not use annotations (cannot). I use mapping files.

What I want to do is very simple : I want to map an enum object property to a column (string value). And I do not want to write UserType or other extra code.

Is there a way to achieve this in the given context ? From what I've read, it seems that I would need to either :
a) Use annotations
OR
b) Write extra code (add UserType)

Can someone please tell me is Hibernate want to do want I want (and how!!).


Thanks and have a nice day.


renam


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 9:00 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You could use name() and valueOf() on the enum. The type stored in the DB would be a string. There is extra code though - you need getters and setters for BOTH the string value and the Enum. However, the string versions can remain private as they're only needed by hibernate - doesn't pollute the class interface.

Code:
private MyEnum myEnum;

public MyEnum getMyEnum() {
    return myEnum;
}
public void setMyEnum(MyEnum myEnum) {
    this.myEnum = myEnum;
}

// hibernate accessor
private String getMyEnumName() {
    return myEnum.name();
}
// hibernate accessor
private void setMyEnumName(String myEnumName) {
    myEnum = MyEnum.valueOf(myEnumName);
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 9:09 am 
Newbie

Joined: Mon Jun 16, 2008 8:28 am
Posts: 4
Hi,

Thanks for your response.

I already have implemented the proposed solution, but I didn't realized that the Hibernate-specific getter and setter could be private. I will correct this.

So this is the only solution ? There is nothing I can write in the mapping file that would prevent me to have duplicated getters and setters because all the typedef tricks uses UserType right ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 10:48 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I presume because hibernate core is Java 1.4 compatible it can't do anything clever with enums.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 1:23 pm 
Newbie

Joined: Mon Jun 16, 2008 8:28 am
Posts: 4
I think this is a fairly good reason.

But I'm sure it would be possible to have enum-specific tricks in the mapping file that would only make sense in a jdk 1.5+ context. It could be useful for some people.

But I'm convinced that you already thought about it and have pretty good reasons no to implement something like that. So keep up the good work and thanks for you time.

-renam


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 9:16 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Quote:
I presume because hibernate core is Java 1.4 compatible it can't do anything clever with enums.


Sad but true. :( Annotations was a great leap forward for Java.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.