You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Christian Bourque <ch...@gmail.com> on 2010/02/01 23:23:27 UTC

injection bug

Hi,

I have a weird problem with injection and I would like to confirm
whether or not it is a bug:

-----------------------------------------------------------------------------------------------------------------------------------
public abstract class A
{
  private EntityManager em;

  public EntityManager getEntityManager() { return em; }
  public void setEntityManager(EntityManager em) { this.em = em; }
}

@Stateless
public class B extends A implements IB
{
  @PersistenceContext
  private EntityManager em;

  @PostConstruct
  public void init()
  {
    System.out.println("*** Entity Manager: " + em);

    setEntityManager(em);
  }
}
-----------------------------------------------------------------------------------------------------------------------------------

In B the injected entity manager is always null! And If I simply
change the variable name to say "em2" then it works!

Tested on OpenEJB 3.1.2...

Thanks

Christian

RE: injection bug

Posted by Stephen Davidson <st...@etcc.com>.
Hi Jason.

No, you can have private in "A", and then a var of the same name (any
scope) in a child of "A".  However, for Design, NOT a good idea, as one
frequently finds either one's own foot (and/or leg) removed by an
explosively discharging projectile (and often same to one or more
colleagues) by this pattern.  But it is perfectly legal.  For design and
code stability, I would second your suggestion of a protected var.  

But it looks like what Christian is trying to accomplish here is an
injected value that was not (or can not be) annotated for whatever
reason in the parent class.  So I suspect he may have found, well not
sure if its an actual bug, more like a missing feature that ought to be
there?  Unless an annotation is getting deleted/dropped when the
hierarchy is getting walked, in which case, that IS a bug.

Regards,
Steve

-----Original Message-----
From: Jason Russo [mailto:jrusso@genscape.com] 
Sent: Tuesday, February 02, 2010 8:46 AM
To: users@openejb.apache.org
Subject: Re: injection bug

Pardon me for chiming in, but I would think that java rules ought to
prohibit you from declaring variable em in class B, since it is declared
private in class A.  Shouldn't you declare em as protected in class A
instead?



Jason Russo

Application Developer

Genscape- See the Energy

445 E. Market St, Suite 200

Louisville, KY 40202

Tel: 502-583-2091

www.genscape.com



On Mon, 2010-02-01 at 17:23 -0500, Christian Bourque wrote:

> Hi,
> 
> I have a weird problem with injection and I would like to confirm
> whether or not it is a bug:
> 
>
------------------------------------------------------------------------
-----------------------------------------------------------
> public abstract class A
> {
>   private EntityManager em;
> 
>   public EntityManager getEntityManager() { return em; }
>   public void setEntityManager(EntityManager em) { this.em = em; }
> }
> 
> @Stateless
> public class B extends A implements IB
> {
>   @PersistenceContext
>   private EntityManager em;
> 
>   @PostConstruct
>   public void init()
>   {
>     System.out.println("*** Entity Manager: " + em);
> 
>     setEntityManager(em);
>   }
> }
>
------------------------------------------------------------------------
-----------------------------------------------------------
> 
> In B the injected entity manager is always null! And If I simply
> change the variable name to say "em2" then it works!
> 
> Tested on OpenEJB 3.1.2...
> 
> Thanks
> 
> Christian

Re: injection bug

Posted by Jason Russo <jr...@genscape.com>.
Pardon me for chiming in, but I would think that java rules ought to
prohibit you from declaring variable em in class B, since it is declared
private in class A.  Shouldn't you declare em as protected in class A
instead?



Jason Russo

Application Developer

Genscape– See the Energy

445 E. Market St, Suite 200

Louisville, KY 40202

Tel: 502-583-2091

www.genscape.com



On Mon, 2010-02-01 at 17:23 -0500, Christian Bourque wrote:

> Hi,
> 
> I have a weird problem with injection and I would like to confirm
> whether or not it is a bug:
> 
> -----------------------------------------------------------------------------------------------------------------------------------
> public abstract class A
> {
>   private EntityManager em;
> 
>   public EntityManager getEntityManager() { return em; }
>   public void setEntityManager(EntityManager em) { this.em = em; }
> }
> 
> @Stateless
> public class B extends A implements IB
> {
>   @PersistenceContext
>   private EntityManager em;
> 
>   @PostConstruct
>   public void init()
>   {
>     System.out.println("*** Entity Manager: " + em);
> 
>     setEntityManager(em);
>   }
> }
> -----------------------------------------------------------------------------------------------------------------------------------
> 
> In B the injected entity manager is always null! And If I simply
> change the variable name to say "em2" then it works!
> 
> Tested on OpenEJB 3.1.2...
> 
> Thanks
> 
> Christian