Java Persistence Query Language

Monday May 01, 2006

Enumerated Annotation in GlassFish Persistence

Several users ran into problems using Enumerated annotation in combination with Java Persistence Query Language (JPQL). Here is an example showing user how to use @Enumerated annotation correctly.

An Enumerated annotation specifies that a persistent property or field should be persisted as an enumerated type. An enum can be mapped as either a string or an integer. There are two enumerated types: ORDINAL and STRING. If the enumerated type is not specified, the enumerated type is assumed to be ORDINAL.

Here is an example about how to define enum and use Enumerated annotation in persistence class:

In persistence class Item.java, EmployeeStatus is an enum. And the field status in Item is defined as EmployeeStatus and is mapped to an integer.

@Entity
@Table(name="ITEM_TABLE")
public class Item implements java.io.Serializable {
public enum EmployeeStatus { FULL_TIME, PART_TIME, CONTRACT };

@Id
@Column(name="ID")
private Integer itemId;
private String name;
@Enumerated(ORDINAL)
private EmployeeStatus status;

public Item() {}

public Integer getItemId() {
return itemId;
}

public void setItemId(Integer id) {
this.itemId = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void setStatus(EmployeeStatus status) {
this.status = status;
}

public EmployeeStatus getStatus() {
return status;
}
}

When using JPQL to query status from client, below is the sample code in j2se environment.

// Create EMF for a persistence unit called j2seEnvironment.
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("j2seEnvironment");

// create EM
EntityManager em = emf.createEntityManager();

// get Transaction
EntityTransaction tx = em.getTransaction();

// create Item and insert into database
Item item = new Item();
item.setItemId(new Integer(1));
item.setName("Jie Leng");
item.setStatus(Item.EmployeeStatus.FULL_TIME);
tx.begin();
em.persist(item);
tx.commit();

// run a JPQL query
String ejbql = "SELECT i FROM Item i WHERE i.status = :status";
Query query = em.createQuery(ejbql);
query.setParameter("status", Item.EmployeeStatus.FULL_TIME);
List result = query.getResultList();

I saw some users using wrong JPQL query like following code and got errors:

// wrong code
String ejbql2 = "SELECT i FROM Item i WHERE i.status = 0";
Query query2 = em.createQuery(ejbql2);
List result2 = query2.getResultList();

Since status is enum type, you can not just using integer directly in where clause.

Comments:

sdsd

Posted by 220.225.40.2 on July 23, 2007 at 03:27 AM PDT #

SSSSSKKKKKKYYYY

Posted by 220.225.40.2 on July 23, 2007 at 03:35 AM PDT #

AAAAAAA

Posted by Sunil on July 23, 2007 at 03:36 AM PDT #

Viagra is a term that is probably no longer new to anybody. It has come
to be recognized with the who’s who of the male sexual disease called
erectile dysfunction. Everybody is aware of the fact that Viagra has
something to do with the male sex organ. These tit bit informations are
alright for the layman, but for those who are prescribed the drug, it is
very important that they have complete knowledge about it, for this
they can refer any <a
href="http://www.besthealthmed.com/viagra_guide.html">viagra guide</a>
available on the net.

Posted by Viagra guide on October 23, 2007 at 10:18 PM PDT #

Thank you for this information! I had a very hard time finding any documentation on this feature.

Posted by jzacharuk on April 14, 2008 at 10:53 AM PDT #

tnx

Posted by order viagra on August 07, 2008 at 12:03 PM PDT #

tnx

Posted by order cialis on August 07, 2008 at 12:04 PM PDT #

tnx

Posted by order levitra on August 07, 2008 at 12:04 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Calendar

Feeds

Search

Links

Navigation

Referrers