0

I am facing a strange behavior that i don't understand. In fact i have a DAO AbstractFacade in which i injected an EntityManager. From this abstract class i derived many subclasses. First the project did'nt work properly et resulted in many exceptions. Then i noticed from error messages that i had a problem with getting the EntityManager to do persistence jobs. That was strange because i got a getter in the abstract class that returns the

entityManager.
public class AbstractFacade<T> {
    private EntityManager em;
    private Class<T> entityClass;

    protected EntityManager getEntityManager() {
        return em;
    }

    public AbstractFacade(Class entityClass){
        this.entityClass = entityClass;
    }

So i am wondering why is'nt it working in the subclass ? I got an idea to override that method which solved the problem and no more exceptions!

@Stateless
@LocalBean
public class AirportFacade extends AbstractFacade<Airport> implements AirportFacadeLocal{
    @PersistenceContext(unitName = "flams_pu")
    private EntityManager em;

    public AirportFacade(){
        super(Airport.class);
    }

    @Override
    public EntityManager getEntityManager(){
        return em;
    }

So GOOD so far but but i am not satisfiyed because i could'nt figure out why it did'nt work before overriding the getter ??

So please if anyone knows why, let me know and thanks very much.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
velocity
  • 1,630
  • 21
  • 24

2 Answers2

3

You can't override instance variables. If you redeclare it in the subclass, you shadow the original variable with the new one, but both still exist.

In the first case, the AbstractFacade#em variable was returned, since that is where the getEntityManager() method resided.

As soon as you did an override of the getEntityManager() method, the AirportFacade#em variable was returned instead.

Keppil
  • 45,603
  • 8
  • 97
  • 119
  • Thanks for your replies, though i think you missunderstood my question because i already know that overriding applies only to methods but the question is why, before overriding the getEntityManager method i coudl'nt get the injected entityManager instance (em) from the AirportFacade since it inherits the getEntityManager method from AbstractFacade ? – velocity Jun 03 '13 at 14:39
  • @velocity: Ok. The question title in particular and your provided information suggested you had problems with overriding instance variables. If that is not the case you need to rephrase your question, or possibly ask a new question if it becomes an entirely different question. – Keppil Jun 03 '13 at 14:47
  • @velocity: In the beginning of your question you seem to indicate that you had an entity manager in your first version too, but that it didn't work properly. In that case this has nothing to do with inheritance and overriding. – Keppil Jun 03 '13 at 14:50
0

This is one of possible solutions to your problem. You cannot override field, but PersistenceContext annotation will work also on setter. The solution below let's you use different contexts in subclasses.

public class AbstractFacade<T> {

    protected EntityManager em;

    private Class<T> entityClass;

    protected EntityManager getEntityManager() {
        return em;
    }

    public AbstractFacade(Class entityClass){
        this.entityClass = entityClass;
    }
}

@Stateless
@LocalBean
public class AirportFacade extends AbstractFacade<Airport> implements AirportFacadeLocal{

    public AirportFacade(){
        super(Airport.class);
    }

    @PersistenceContext(unitName = "flams_pu")
    protected void setEntityManager(EntityManager em) {
         this.em = em;
    }
}
Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105