Enumerated Type

38 views
Skip to first unread message

Akram Moncer

unread,
May 7, 2012, 3:45:38 AM5/7/12
to google-we...@googlegroups.com
hello ;

can someone tell me how can i use the Enumerated Type in JPA, and how can i persist it


thanks

--
Akram MONCER
Personne

Brandon Donnelson

unread,
May 8, 2012, 4:28:04 PM5/8/12
to google-we...@googlegroups.com
What type of Datastore?

Akram Moncer

unread,
May 8, 2012, 4:53:36 PM5/8/12
to google-we...@googlegroups.com
google app engine datastore

2012/5/8 Brandon Donnelson <branfl...@gmail.com>

What type of Datastore?

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/JTEUNaoRQiwJ.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.




--
Akram MONCER
Personne

Brandon Donnelson

unread,
May 8, 2012, 5:06:45 PM5/8/12
to google-we...@googlegroups.com
If you can serialize the object it can be persisted to the datastore.

If TaskType is an enum:

public class Task {
  @Persistent   
  private TaskType taskType; 
}

public enum TaskType {
  MYTASK; 
  public String value() {
    return name().toLowerCase();
  }
  public static TaskType fromValue(String v) {
    return valueOf(v.toUpperCase());
  }
}

Hope that helps,
Brandon Donnelson

Patrick Julien

unread,
May 8, 2012, 10:05:32 PM5/8/12
to google-we...@googlegroups.com
Just use JPA's @Enumerated


use @Enumerated(EnumType.ORDINAL) to only store a number or @Enumerated(EnumType.String) to get the textual representation of your enum

Joseph Lust

unread,
May 10, 2012, 11:23:39 AM5/10/12
to google-we...@googlegroups.com
Patrick's suggest is how I usually achieve this, but think for a few minutes on your choice of EnumType.STRING vs EnumType.ORDINAL. How do you think you're application will change with time? Generally I prefer STRING since reordering of your enumeration or removal of values will leave you in a bind (say for an API that you don't control). Either way you're subject to change, but STRING is more versatile in my mind. I all depends on the volatility of your project schema.

Just don't worry about a few bytes here and there since you're now in the cloud and bytes are not what they seem. For example a boolean is a 4 bytes in GAE DataStore due to word size issues. That's right, 32 bits to store 1 bit (note you can store 2 bools in 4 bytes, but if you have just 1, it's 4 bytes).

Sincerely,
Joseph
Reply all
Reply to author
Forward
0 new messages