tasfe / generic-dao

Automatically exported from code.google.com/p/generic-dao
0 stars 0 forks source link

@PersistenceContext not inject dependencies #2

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hi EntityManager in GenericDao is not injected. This could be, because
GenericDao is not a EJB class managed. How can inject automatically ?

Thanks.
Edson.

Original issue reported on code.google.com by edson.go...@gmail.com on 3 Dec 2008 at 2:22

GoogleCodeExporter commented 9 years ago
If i put this code in a Stateless bean and try to set manually the 
EntityManager by
the setEntityManager method of GenericDao, the result is ok and all operations 
done
successfully.

@PersistenceContext(unitName="myPU")
public EntityManager em;

    public void testPais() {
        PaisDaoImpl pais = new PaisDaoImpl();
        pais.setEntityManager(em);
        List<Pais> paisList = pais.getAll();
        for (Pais p: paisList) {
            System.out.println(p.getDescr());
        }
    }

Original comment by edson.go...@gmail.com on 3 Dec 2008 at 2:26

GoogleCodeExporter commented 9 years ago
Perhaps it would be better to annotate the field EntityManager in GenericDao 
with
@PersistenceContext, rather than the setter.
I have imported all source in a project, and made three changes:
1 - annotate the field.
2 - make it protected.
3 - Remove the setter and getter for EntityManager

Subclasses use directly the entity manager, if needed.

Now it seems to work

Original comment by ermanno....@gmail.com on 22 Dec 2008 at 6:40

GoogleCodeExporter commented 9 years ago
I have same problem, how i can fixit? sorry for my english.

Original comment by ccha...@gmail.com on 11 Sep 2009 at 5:16

GoogleCodeExporter commented 9 years ago
You must extend the IDao in the interface and extend GenericDao in your EJB. 

Interface Example:

@Local
public interface NotaFiscalReferenDaoLocal extends IDao<NotaFiscalReferen, 
Long> {

}

Implementation Example:

/**
 * Session Bean implementation class NfReferenDaoBean
 */
@Stateless
public class NotaFiscalReferenDaoBean extends GenericDao<NotaFiscalReferen, 
Long>
implements NotaFiscalReferenDaoLocal {

}

NotaFiscalReferen is the Entity, in this case.

Original comment by edson.go...@gmail.com on 11 Sep 2009 at 5:51