users@glassfish.java.net

Re: CDI doubts

From: CasMeiron <casmeiron_at_gmail.com>
Date: Thu, 2 Sep 2010 19:16:34 -0300

Obviously is not cast 'cause it would work (child->base), don't know what's
goin' on, no clue.
-------------------------------
Paulo Reis




On Thu, Sep 2, 2010 at 7:13 PM, CasMeiron <casmeiron_at_gmail.com> wrote:

> Hello guys,
>
> I have two persistence units on my app, let's call the first one "PUA" and
> the second "PUB".
>
> I want to design a way to give the user the appropriate persistence unit
> without having to specify @PersistenceContext(unitName="REAL_NAME") so I've
> created a wrapper (also gives the entity manager some cool features).
>
> I've designed the follow interface:
>
> public interface EntityManagerWrapper {
> <T extends Object>T persist(T object);
> <T extends Object>void remove(T object);
> <T extends Object>T find(Class<T> objectClass, Serializable key);
> <T extends Object>T merge(T object);
> <T extends Object>Collection<T> list(Class<T> type, String namedQuery,
> Map<String, Object> parameters);
> <T extends Object>Collection<T> list(Class<T> type, String namedQuery,
> Map<String, Object> parameters, Integer startingAt, Integer maxResults);
> <T extends Object>T loadSingle(Class<T> objectClass, String namedQuery,
> Map<String, Object> parameters);
>
> }
>
> Then I've created a base class:
>
> public abstract class BaseEntityManagerWrapper implements
> EntityManagerWrapper {
>
> ...
> }
>
> And two qualifiers:
>
> @Qualifier
> @Retention(RUNTIME)
> @Target({ TYPE, METHOD, PARAMETER, FIELD})
> public @interface PUA {
> }
>
>
>
> @Qualifier
> @Retention(RUNTIME)
> @Target({ TYPE, METHOD, PARAMETER, FIELD})
> public @interface PUB {
> }
>
>
> So I have 2 implementations of the EntityManagerWraper. Follow the PUA one:
>
> @Stateless
> @LocalBean
> @PUA
> public class PUAEntityManagerWrapper extends BaseEntityManagerWrapper {
>
> private EntityManager manager;
> @PersistenceContext(unitName="REAL_NAME_PUA")
> void setManager(EntityManager manager) {
> this.setManager(manager);
> }
>
> public EntityManager getManager() {
> return manager;
> }
> }
>
>
> And when I want to use persistence manager I just put on my code:
>
> @PUA
> EntityManagerWrapper managerA;
>
> @PUB
> EntityManagerWrapper managerB;
>
> But when I try to use I get some weird CDI error:
>
> Caused by: java.lang.IllegalStateException: Unable to convert ejbRef for
> ejb PUAEntityManagerWrapper to a business object of type class
> package.BaseEntityManagerWrapper
>
> It's seems it try to cast the implementations to the base class? What Am I
> doin' wrong?
>
> Thanks in advance.
> -------------------------------
> Paulo Reis
>
>
>