Friday, February 6, 2009

JPA uses enum in query

If java 5 enum is used, here is a sample.
String queryString = "SELECT a FROM Address a " + "where a.addressType = jpa.model.AddressType.OFFICE";

If apache common enum is used, the entity would look like this:
@Persistent(optional = false)
@Column(name = "STORE_STATE")
public StoreState getStoreState() {...

getValue and valueOf are the methods of StoreState which is a extension of commons enum.
Externalizer method will be executed when the JPA is going to save the java class (StoreState) into database.
Factory method will be executed when the JPA gets the data from database and convert it to a java class and populate it.

You can use the state code in JPQL directly. Openjpa before version 1.2.0 has a bug on this.
SELECT s FROM StoreImpl s WHERE s.storeState=?1

No comments:

Post a Comment