Thread: [perfectjpattern-users] Queries in Jpa dao
Brought to you by:
bravegag
From: Mansour Al A. <man...@gm...> - 2010-12-17 02:22:44
|
Hello all: How do I use queires in a DAO that extends AbstractJpaManagedBaseDao ? For example, AbstractJpaManagedBaseDao prevents accessing entityManager. So I can not use queries to select of filter some objects. Here's a code: @Stateless(name = "GeoDao") public class GeoDaoImpl extends AbstractJpaManagedBaseDao<String, Geo> implements GeoDao { public GeoDaoImpl() { super(Geo.class); } @PersistenceContext(unitName = "myPU") public void setEntityManager(EntityManager em) { super.setEntityManager(em); } @Override public Geo getByAbbreviation(String abbreviation) { IQuery query = getSession().createQuery("select g from Geo g WHERE g.abbreviation= :abbr"); //HERE I NEED TO SET THE PARAMETERS for abbreviation List <Geo> res = query.getResultList(); return res.get(0); } } I am using openEJB with openJPA. Can not use spring or hibernate. Am I missing something ? Thank you in advance |
From: Giovanni A. <bra...@gm...> - 2010-12-17 12:07:43
|
Hello Mansour Al Akeel, Please find my answers below. On Dec 17, 2010, at 3:22 AM, Mansour Al Akeel wrote: > How do I use queires in a DAO that extends AbstractJpaManagedBaseDao ? > Judging by the code below, in general you are using it correctly. From the Generic DAO documentation page <http://perfectjpattern.sourceforge.net/dp-genericdao.html> you can see that extending JPA has limited capabilities compared to that of e.g. AbstractHibernateManagedGenericDao. See this example <http://perfectjpattern.sourceforge.net/xref/org/perfectjpattern/jee/integration/dao/jpa/MovieGenericDao.html> here your use-case is more elegantly covered by using the findByExample capability. > For example, AbstractJpaManagedBaseDao prevents accessing entityManager. > So I can not use queries to select of filter some objects. Here's a > code: > You can still do: - super.getSession().createQuery(...) or - super.getSession().createNativeQuery(...) these two methods mirror the JPA functionality, so you should be fine. HTH, Best regards, Giovanni > @Stateless(name = "GeoDao") > public class GeoDaoImpl extends AbstractJpaManagedBaseDao<String, Geo> implements GeoDao { > > public GeoDaoImpl() { > super(Geo.class); > } > > @PersistenceContext(unitName = "myPU") > public void setEntityManager(EntityManager em) { > super.setEntityManager(em); > } > > @Override public Geo getByAbbreviation(String abbreviation) { > IQuery query = getSession().createQuery("select g from Geo g WHERE g.abbreviation= :abbr"); > //HERE I NEED TO SET THE PARAMETERS for abbreviation > List <Geo> res = query.getResultList(); > return res.get(0); > } > } > > > I am using openEJB with openJPA. Can not use spring or hibernate. > Am I missing something ? > > > Thank you in advance > > ------------------------------------------------------------------------------ > Lotusphere 2011 > Register now for Lotusphere 2011 and learn how > to connect the dots, take your collaborative environment > to the next level, and enter the era of Social Business. > http://p.sf.net/sfu/lotusphere-d2d > _______________________________________________ > Perfectjpattern-users mailing list > Per...@li... > https://lists.sourceforge.net/lists/listinfo/perfectjpattern-users |
From: Giovanni A. <bra...@gm...> - 2010-12-17 12:24:33
|
Hello again, Oh I see now what you mean. I am sorry in 1.0.2 there is no way to do that, IQuery is quite limited. If you could fetch and build the snapshot 1.0.3 (trunk) then you can do this is the way you want. In 1.0.3 all improvements related to the Generic DAO implementation (including adding the necessary IQuery features) are stable and fully covered with unit tests. Best regards, Giovanni On Dec 17, 2010, at 3:22 AM, Mansour Al Akeel wrote: > Hello all: > How do I use queires in a DAO that extends AbstractJpaManagedBaseDao ? > > For example, AbstractJpaManagedBaseDao prevents accessing entityManager. > So I can not use queries to select of filter some objects. Here's a > code: > > @Stateless(name = "GeoDao") > public class GeoDaoImpl extends AbstractJpaManagedBaseDao<String, Geo> implements GeoDao { > > public GeoDaoImpl() { > super(Geo.class); > } > > @PersistenceContext(unitName = "myPU") > public void setEntityManager(EntityManager em) { > super.setEntityManager(em); > } > > @Override public Geo getByAbbreviation(String abbreviation) { > IQuery query = getSession().createQuery("select g from Geo g WHERE g.abbreviation= :abbr"); > //HERE I NEED TO SET THE PARAMETERS for abbreviation > List <Geo> res = query.getResultList(); > return res.get(0); > } > } > > > I am using openEJB with openJPA. Can not use spring or hibernate. > Am I missing something ? > > > Thank you in advance > > ------------------------------------------------------------------------------ > Lotusphere 2011 > Register now for Lotusphere 2011 and learn how > to connect the dots, take your collaborative environment > to the next level, and enter the era of Social Business. > http://p.sf.net/sfu/lotusphere-d2d > _______________________________________________ > Perfectjpattern-users mailing list > Per...@li... > https://lists.sourceforge.net/lists/listinfo/perfectjpattern-users |