dev@glassfish.java.net

Re: Using enum fields for persistence

From: Michael Bouschen <Michael.Bouschen_at_Sun.COM>
Date: Wed, 27 Sep 2006 09:10:14 +0200

Hi Dmitry,

you can use the enum constant directly in the query text:
  em.createQuery("select o from NotificationAddress o where
o.notificationType = <package>.NotificationType.EMAIL");
Please replace <package> by the package name of the enum type, because
the enum constant need to be qualified using the fully qualified type name.

A better alternative might be using an input parameter:
  Query q = em.createQuery("select o from NotificationAddress o where
o.notificationType = :type);
  q.setParameter("type", NotificationType.EMAIL);

Hope that helps.

Regards Michael

> Hello all.
> I try to use enum fields in entity-class
> ...
> @Entity
> public class NotificationAddress extends VersionObject {
>
> public enum NotificationType { EMAIL, SMS, SMB };
>
> private NotificationType notificationType;
> ...
>
> Then i try to filter objects by this type:
> ...
> em.createQuery("select o from NotificationAddress o where o.notificationType = " + NotificationType.EMAIL).getSingleResult();
> ...
> or
> ...
> em.createQuery("select o from NotificationAddress o where o.notificationType = " + NotificationType.EMAIL.ordinal()).getSingleResult();
> ...
>
> I receive error:
> Exception [TOPLINK-115] (Oracle TopLink Essentials - 2006.8 (Build 060908)): oracle.toplink.essentials.exceptions.DescriptorException Exception Description: No conversion value provided for the attribute [1]. Mapping: oracle.toplink.essentials.mappings.DirectToFieldMapping[notificationType-->NOTIFICATIONADDRESS.NOTIFICATIONTYPE] Descriptor: RelationalDescriptor(ejb.entity.NotificationAddress --> [DatabaseTable(VERSIONOBJECT), DatabaseTable(NOTIFICATIONADDRESS)])
>
> How to use enum types?
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: dev-help_at_glassfish.dev.java.net
>
>