On Fri, Jun 5, 2009 at 3:25 PM, David Beer <david.m.b...@googlemail.com>wrote:

> Hi All
>
> I too would much rather see @NamedQuery(ies) defined in a seperate
> class like I do other more complicated queries. I also see this as a
> good way of keeping things modular or decoupled from the model.
>
> Using a seperate java class which then registers those queries at run
> time would be a good way of doing things. The registration not need be
> in the persistence.xml file as long as it can be added to the
> persistence unit later.


This can be done, provided the separate class is an Entity /
MappedSuperclass. You have to add it to the PersistenceUnit at factory
creation time though.

Something like this should work..

Map m = new HashMap()
m.put("openjpa.MetaDataFactory=jpa(Types=${list all your entity types
here};${new query container type goes here})");

emf = javax.persistence.Persistence.createEntityManagerFactory(emfName, m);

The catch is that if Types is specified it must be the complete list (as I
remember).

hth
-mike


>
> David
>
> On Fri, 5 Jun 2009 08:33:19 -0700 (PDT)
> Pinaki Poddar <ppod...@apache.org> wrote:
>
> >
> > Hi Daryl,
> >  This is one feature, for a long time, I am interested to be included
> > in JPA or at least in OpenJPA. Having your queries *only* in the
> > major compilation unit is a poor idea for usability point of view and
> > takes away much of the power from tuning a query a posteriori.
> >   However, I had considered the facility slightly differently than
> > yours. The primary feature that I considered important in this aspect
> > is to dissociate my queries from the major compilation units. The
> > compilation units will only refer them by name.
> >
> >  How about the following
> >   <property name="openjpa.NamedQueryRegistry"
> > value="path/to/file/that/contains/NamedQueries.java"/>
> >
> >   and in code
> >    Query q = em.createQuery("ANameThatAppearsInNamedQueryRegsitry");
> >
> >
> > Daryl Stultz wrote:
> > >
> > > Hello,
> > >
> > > I'm frustrated by the fact that I need to define @NamedQuery and
> > > others inside my entity classes. I would like to put what I
> > > consider "logic" where
> > > I think it's appropriate which is often not with the model. Is
> > > there a way to programmatically define Named Queries Sql Result Set
> > > Mappings, etc and register them with the persistence engine? I.e.
> > > is there any alternative to
> > > using annotations/xml for defining these things?
> > >
> > > String query = "select o...";
> > > NamedQuery nq = new NamedQuery(query);
> > > Persistence.registerNamedQuery(nq);
> > >
> > > Thanks.
> > >
> > > --
> > > Daryl Stultz
> > > _____________________________________
> > > 6 Degrees Software and Consulting, Inc.
> > > http://www.6degrees.com
> > > mailto:da...@6degrees.com
> > >
> > >
> >
> >
> > -----
> > Pinaki Poddar                      http://ppoddar.blogspot.com/
> >
> > http://www.linkedin.com/in/pinakipoddar
> > OpenJPA PMC Member/Committer
> > JPA Expert Group Member
>
>

Reply via email to