Skip to Main Content

Integration

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to specify an enum type in a named query

328707Dec 10 2008
I'm using TopLink with GlassFish. All is working well but I cannot figure out how to properly write a named query using an enum without binding the enum to the Query. What I would like to write is the following:

@NamedQuery(name = "Address.findAll", query = "select object(o) from Address as o WHERE o.addressState <> AddressState.Type.ARCHIVED")

but this gets me an Exception. The other way I tried was:

@NamedQuery(name = "Address.findAll", query = "select object(o) from Address as o WHERE o.addressState <> 'ARCHIVED'")

but this gives me a ClassCastException as a String cannot be converted into an Enum.

The only way that I seem to be able to get this to work is with the following:
@NamedQuery(name = "Address.findAll", query = "select object(o) from Address as o WHERE o.addressState <> :addressState"),

and then I must bind the AddressState.ARCHIVED enum to the Query. While this works correctly it is a bit of a pain. Is there a simply work-around or am I forced to bind the object to the Query?

Thanks for the help. Sample code below.


// Address Class

@Entity
@Table(name = "address")
@MappedSuperclass
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING, length = 10)
@NamedQueries( {
@NamedQuery(name = "Address.findAll", query = "select object(o) from Address as o WHERE o.addressState <> :addressState"),
@NamedQuery(name = "Address.getCount", query = "select count(o) from Address as o WHERE o.addressState <> :addressState") })
public class Address implements Serializable {

// ... omitted

/** Field description */
@ManyToOne
@JoinColumn(nullable = false)
protected AddressState addressState;


// Address State Class
@Entity
@Table( name = "address_state" )
@NamedQueries({
@NamedQuery(
name="AddressState.findAll",
query="select object(o) from AddressState as o"
),
@NamedQuery( name = "AddressState.getCount",
query = "select count(o) from AddressState as o"
)
})
public class AddressState implements Serializable {

// ... omitted

/** Field description */
@Id @Column(
name = "id",
insertable = true,
length = 10,
nullable = false,
unique = false
)
@Enumerated( EnumType.STRING )
private Type id = Type.PENDING;

/**
* Enum description
*
*/
public enum Type { HOLD, PENDING, APPROVED, ARCHIVED }

;
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 7 2009
Added on Dec 10 2008
0 comments
1,718 views