-->
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.  [ 5 posts ] 
Author Message
 Post subject: Using a Java5 enum with annotations in 3.2.0GA
PostPosted: Wed Jan 31, 2007 2:57 pm 
Beginner
Beginner

Joined: Sun Nov 16, 2003 3:04 pm
Posts: 24
I tried putting this in the main forum, but it was mostly ignored. Since I'm using 3.2.0GA with annotations (and the latest versions of everything), I figure its a valid question for in this forum.

Using hibernate 3.2.0GA, and Java1.5, what's the best way to map an enum? I've had a heck of a time trying to find it in the documentation, but, basically, this is what I have:

Code:
public enum YesNoType
{
    YES
    {
        public String getLabel()
        {
            return "Yes";
        }

        public Character getValue()
        {
            return new Character('Y');
        }
    },
    NO
    {
        public String getLabel()
        {
            return "No";
        }

        public Character getValue()
        {
            return new Character('N');
        }
    };

    public static YesNoType getType(Character c)
    {
        Set<YesNoType> types = EnumSet.allOf(YesNoType.class);
        for (YesNoType type : types)
        {
            if (type.getValue().equals(c))
                return type;
        }
        return null;
    }

    public static List<YesNoType> getAll()
    {
        Set<YesNoType> types = EnumSet.allOf(YesNoType.class);
        return new ArrayList<YesNoType>(types);
    }

    public abstract Character getValue();

    public abstract String getLabel();



And a persistent object that uses it:
Code:
@Entity
@Table(name = "CHST_COM")
@IdClass(CommodityPk.class)
public class Commodity extends SomeAbsClass implements Comparable
{
    //...
    private YesNoType               activeFlag;
    //...
    @Enumerated(EnumType.STRING)
    public YesNoType getActiveFlag()
    {
        return activeFlag;
    }
    //...
}


Now how do I get hibernate to look at the character value when persisting (and loading) the data from the db??


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 31, 2007 6:04 pm 
Regular
Regular

Joined: Mon Nov 14, 2005 7:33 pm
Posts: 73
Here's an extremely simple example from some of the code from our storefront application.

The enum:

Code:
public enum LogType
{
   INFO,
   WARNING,
   INVALID_DATA,
   EXCEPTION
}


In an entity:

Code:
@Entity
@Table(name="tbl_log_entry")
public class LogEntry implements Serializable
{
   @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
   private Integer id;
 
   @Temporal(value=TemporalType.DATE)
  @Column(nullable=false)
  private Date dateEntered;
 
  @Enumerated(EnumType.STRING)
  @Column(nullable=false)
   private LogType type;
 
  @Column(nullable=false, length=100)
   private String source;
 
  @Column(nullable=false, length=1000)
   private String shortDescription;
 
  @Column(length=4000)
   private String longDescription;
........................................
}


Simply query it in a session bean:

Code:
   public List<LogEntry> getAll()
   {
      List<LogEntry> entries = null;
      
      try
      {
         String query = "select le from LogEntry le order by le.dateEntered desc";
         
         Query q = this.em.createQuery(query);         
         entries = q.getResultList();
      }
      catch (Exception exp)
      {
         exp.printStackTrace();
      }
      
      return entries;
   }


...works like a charm.

You can find out all about it here:

http://www.hibernate.org/hib_docs/annot ... le/#d0e265

Also, the book "Enterprise JavaBeans 3.0" by Bill Burke & Co. is excellent, I think that's the example I drew from.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 1:46 pm 
Beginner
Beginner

Joined: Sun Nov 16, 2003 3:04 pm
Posts: 24
tsar bomba wrote:
You can find out all about it here:

http://www.hibernate.org/hib_docs/annot ... le/#d0e265

Also, the book "Enterprise JavaBeans 3.0" by Bill Burke & Co. is excellent, I think that's the example I drew from.


Thanks for your help. I did see that documention that you linked to, but, unfortunately, it is very vague. I'm curious as to what is written to the DB. I was hoping it would be my 'value' character. But I'm assuming you are getting a string represenation of the ordinal value. Is that correct?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 2:05 pm 
Regular
Regular

Joined: Mon Nov 14, 2005 7:33 pm
Posts: 73
fortknox wrote:
tsar bomba wrote:
You can find out all about it here:

http://www.hibernate.org/hib_docs/annot ... le/#d0e265

Also, the book "Enterprise JavaBeans 3.0" by Bill Burke & Co. is excellent, I think that's the example I drew from.


Thanks for your help. I did see that documention that you linked to, but, unfortunately, it is very vague. I'm curious as to what is written to the DB. I was hoping it would be my 'value' character. But I'm assuming you are getting a string represenation of the ordinal value. Is that correct?


Yes, it doesn't really explain everything that can be done...Enums can be respresented other ways...I'd recommend just getting the book I mentioned - it's been a great reference for me.

Yes, since I've annotated it as a string, the string value is persisted...which is all I really needed. I didn't want another table for something that will likely never change...and was just too simple to justify putting in the database.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 2:12 pm 
Beginner
Beginner

Joined: Sun Nov 16, 2003 3:04 pm
Posts: 24
Thanks Tsar. I'll nab that book.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.