users@glassfish.java.net

Re: Generics and EJB3 Entity Beans

From: Sanjeeb Kumar Sahoo <Sanjeeb.Sahoo_at_Sun.COM>
Date: Tue, 21 Nov 2006 08:13:55 +0530

Behrang Saeedzadeh wrote:
> Hi,
>
> Can we use Generics when designing EJB3 Entity Beans?
Marina has already responded to this one. I will answer the specific
ones here. See response inline...
> Say something like this:
>
> public abstract class Field<Type> {
> ...
> }
>
> @Entity
> public class StringField extends Field<String> {
> ...
> }
>
Yes, the above code is allowed. But, keep in mind that you can't write
something like this:
@Entity public class Field<T> {...}
> or
>
> @Entity
> public class Form {
>
> public List<Field> getFields() {
> ...
> }
>
> }
>
This is not correct, because /public List<Field> getFields()/ is not
annotated. It will be treated as if it were annotated with *_at_Basic* and
eventually fail because a List<Field> is *not* Serializable. Either you
want to do something like
@OneToMany public List<StringField> getFields() ...
or
public ArrayList<Field> getFields() ...

In the former case, you can't store an IntergerField in fields, where as
the later case allows you to store different kinds of Fields, but they
will be stored as Serialized byte stream in the database.

Thanks,
Sahoo
> Form f = new Form();
> f.add(new StringField());
> f.add(new IntegerField());
> ...
>
> Thanks in advance,
> Behi
>