Using enumeration in Entity class

In 5.0, the Java programming language gets support for enumerated types. In today's post I would like to show how to use enum in Entity classes. We should create new enumerated type firts:
    public enum AdvertisementState { OPEN, CLOSE, RESRVED}
  
This enum represents status of some advertisement. Then, we can create Entity class that represents advertisement:
  @Entity()
  public class Advertisement implements Serializable {
  private AdvertisementState state;

    @Enumerated(EnumType.ORDINAL)
    public AdvertisementState getState() {
        return state;
    }
You can use ORDINAL or STRING type. When you use ORDINAL type then columnt type is used INTEGER and for STRING the VARCHAR is used. Now, we can use the enum type for finding advertisements with OPEN status:
  em.createQuery("SELECT a FROM Advertisement a WHERE a.state = :state").
                  setParameter("state", AdvertisementState.OPEN).
                  setMaxResults(count).getResultList();
Using enum type is very easy, isn't it?
Comments:

Post a Comment:
Comments are closed for this entry.
About

pblaha

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks