Edoardo, JPQL has SIZE function that you can use to get the size of a ToMany relation field. Assuming your entity class is named MainObject that contains public List<Sub> getSub() { return sub; }, the following query returns the size of the "sub" for the given primary key (id) of MainObject:
String query = "SELECT SIZE(m.sub) FROM MainObject m where m.id = ?1 "; Query q = em.createQuery(query); Long subcount = q.setParameter(1, 123).getSingleResult(); Hope this helps. Catalina On Wed, Mar 25, 2009 at 12:04 AM, Edoardo Panfili <edoa...@aspix.it> wrote: > Hi, > I have an object that use a one-to many relation > -------------------------------- > @OneToMany(cascade={CascadeType.ALL}, mappedBy="segnalazione", > fetch=FetchType.LAZY) > public List<Sub> getSub() { return sub; } > public void setSub(List<Sub> sub) { this.sub = sub;} > > @Transient > public int getCounter() { > return this.getSub().size(); > } > -------------------------------- > > sometimes I need to know only the number of Sub but getCounter() retrieve > from DB a lot of not useful data. > > I found this fragment of code in openJpa manual > -------------------------------- > EntityManager em = ... > Query q = em.createQuery("SELECT MAX(x.price) FROM Magazine x WHERE x.title > = 'JDJ'"); > Number result = (Number) q.getSingleResult(); > -------------------------------- > > This is usefull in a java program but I need to use the counter in a JSP (a > servlet retrieves the data and a JSP write a summary of data retrieved whit > the counter), It is more useful to me to use a property of my object (like > getCounter() ). > > I can't figure how to write it, can someone help me? > > thank you > Edoardo > >