1

I have 2 Objects Site and Page. A Site have many Pages. I get pages with fetch Lazy type. With a particular reason i want to get pages of a site where the date of pages > val.

@Entity
@Table(name = "site")
Public class Site {
String site_id;
Set<Page> pages;
@OneToMany(mappedBy = "site", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    public Set<Page> getPages() {
    return pages;
    }
}

@Entity
@Table(name = "page")
Public class Page{
String page_id;
Site site;
    @ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.MERGE)
    @JoinColumn(name = "site_id")
    public Site getSite() {
    return site;
    }
}

Now in SiteDao i have the mothode to call a site and its pages

@Stateless
public class SiteDao {
@PersistenceContext(unitName = "name")
private EntityManager em;

    public Site getSiteAndPages(String site_id) {        
    Query q = em.createQuery("select s From Site s where s.site_id = :site_id");
    q.setParameter("site_id", site_id);
    Site s = (Site) q.getSingleResult();
    s.getPages();        
    return s;
    }   
} 

This turns well, but s.getPages() return all the pages and i want to get only some pages using where conditions. I tried many options like:

Query q = em.createQuery(
            "select s, p From Site s"
            + " join s.pages p "
            + " where s.site_id = :site_id "
            + " and p.site = s"  
            + " and  p.lastupdate > :val"
            );
    q.setParameter("site_id", site_id); 
    q.setParameter("val", lastUpdate);

Im bloked after many searches, does any person have an idea about how can i fix this ?

B.R

N.Khalifa
  • 35
  • 6

1 Answers1

2

You need some business methods, you can keep inside Site Class

There are two options

1. Add filter

getLatestPages(Date lastupdate)
{
  List pages_=  s.getPages();   
  List latestPages=new ArrayList();
  for(Page p: pages_)
  {
     if(p.getLastUpdatedate().after(lastupdate)) {
             latestPages.add(p);
     }
  }
  reutrn latestPages;
}

2.Use query

getLatestPages(EntityManager em, Date lastupdate)
{
 Query q = em.createQuery(
        "select p From Site s"
        + " join s.pages p "
        + " where s.site_id = :site_id "
        + " and p.site = s"  
        + " and  p.lastupdate > :val"
        );
      q.setParameter("site_id", this.site_id); 
      q.setParameter("val", lastUpdate);   
      return q.getResultList();  
}

Hope this work.

vels4j
  • 11,208
  • 5
  • 38
  • 63
  • Thank you for your answear. Definitely it will works but the goal of my request is to improve memory performance and the `s.getPages` method cannot be a solution because it returns more than 500,000 pages. So i want to reduce this number by directly quering only some pages. – N.Khalifa Jun 09 '16 at 15:25
  • use 2nd option which returns as per your query – vels4j Jun 09 '16 at 16:04
  • Thank you again, i tried the 2nd option but no result reterned by the query. Any suggestions ? – N.Khalifa Jun 09 '16 at 20:30
  • Sorry, can not help more on this, You need to get familiar with JPA – vels4j Jun 10 '16 at 10:21