openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Copeland <t...@jotobjects.com>
Subject Re: how to count sub-elements without retrieving them?
Date Wed, 25 Mar 2009 17:30:49 GMT
Hi Edoardo -

This is a bit off the topic of your question, but I am interested in 
your pattern for setting the EntityManager in a filter.  That seems like 
a reasonable thing to do.  I wonder if other servlet developers on the 
list do it any other way?

One question I have for you is that doFilter() throws ServletException 
and IOException.  Don't you have to declare those in the signature?  And 
lastly, it would be a good practice to put entityManager.close() in a 
finally clause.

- Paul

On 3/25/2009 10:18 AM, Edoardo Panfili wrote:
> Kevin Sutter ha scritto:
>> What I meant is how are you maintaining the lifecycle of the 
>> EntityManager?
>> Are you using application-managed persistence and creating your own 
>> EMF and
>> associated EM's?  Where are you doing this logic?
> I must re-read the open-jpa manual.
>
> My application runs inside tomcat 6.0.18
> I have a filter in my application that does all the work:
> -------------------------------------------------
> public class ManagerJpa implements Filter{
>
>     EntityManagerFactory entityManagerFactory;
>     public void init(FilterConfig fc) throws ServletException {
>         entityManagerFactory = 
> Persistence.createEntityManagerFactory("openjpa-habitat");
>     }
>
>     public void doFilter(ServletRequest req, ServletResponse res, 
> FilterChain chain){
>         EntityManager entityManager = null;
>         entityManager = entityManagerFactory.createEntityManager();
>         req.setAttribute("em", entityManager);
>         chain.doFilter(req,res);
>         entityManager.close();
>     }
>
> }
> ---------------------------------------------------
> in the servlet I use
> entityManager = (EntityManager) req.getAttribute("em");
> I do all the work and at the end of servlet-jsp cycle the filter 
> closes the manager.
>
>
>>> my solution (not so beautiful) based on your post
>>> -------------------------------------------
>>> @Entity
>>> @NamedQueries({
>>> @NamedQuery(name="counter",
>>> query="SELECT COUNT(x) FROM SubObject x WHERE 
>>> x.mainObject.idMainObject =
>>> ?1")
>>> })
>>> public class MainObject {
>>>    [.....]
>>>    private int count;
>>>    @Transient
>>>    public int getCount() { return count; }
>>>    public void setCount(int count) { this.count =count; }
>>>
>>> }
>>> -------------------------------------------
>>>
>>> And during object retrieve
>>> -------------------------------------------
>>> public class find extends HttpServlet{
>>>
>>> public void doPost(HttpServletRequest req, HttpServletResponse res){
>>> [.....]
>>> queryString = "SELECT * FROM MainObject WHERE ........";
>>> Query query = entityManager.createNativeQuery(queryString,
>>> MainObject.class);
>>> lista = query.getResultList();
>>> Query q = entityManager.createNamedQuery("counter");
>>> for(Object o: lista){
>>>    MainObject k = ((MainObject)o);
>>>    q.setParameter(1, k.getIdMainObject());
>>>    Number result = (Number) q.getSingleResult();
>>>    k.setOoo(result.intValue());
>>> }
>>> [.....]
>>> }
>>>
>>> }
>>> -------------------------------------------
>>>
>>> Now my application uses less memory, is faster (but less clean as 
>>> you say).
>>
>>
>> Cool.  So, we're making progress...  Tell me this, what would your 
>> doPost
>> method look like if you had your way?  It looks confusing to do the 
>> native
>> query just to get the set of MainObject id's and then rifle through the
>> named query invocations to count the number of SubObjects.  I'm 
>> trying to
>> figure out what your intended usage is.  And, I'm trying to figure 
>> out how
>> you are separating your data model from your business logic.
> The problem probably is: I am using OpenJPA in a wrong way.
>
> The idea is:
> (servlet)
> 1-parse the HttpServletRequest to build a MainObject as search pattern
> 2-retrieve a List<MainObject> using
> queryString = ("SELECT * FROM Segnalazione WHERE "+wherePart+" ORDER 
> BY code USING ~<~  NULLS FIRST");
> 3-count the number of SubObject of each retieved MainObject, set this 
> number as a property of MainObject.
> 4-bind the List<MainObject> to the request
> (JSP)
> 4-build an html page that views the List<MainObject>
>
>
> In (2) I am now using nativeQuery because I feel more comfortable with 
> plain SQL :-(
>
> The main confusion is the "for(Object o: lista)" I know, but this is 
> the part that speeds up my servlet and saves memory. Before that I 
> simply retrieve the whole list of SubObject to use only the size of 
> that list.
>
> Now the data model and business logic are mixed, this is not a clean 
> to do the work :-(
>
>
> Thank you
> Edoardo
>
>
>


Mime
View raw message