-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Making a query with kind of 'not exists'
PostPosted: Fri Oct 21, 2011 5:50 am 
Newbie

Joined: Fri Nov 12, 2010 4:03 pm
Posts: 5
Hi all,

I come with my little noob question but I promise you I really tried to find the solution by myself... 5hours... Shame on me...


The model I try to make is the following one (with simplification) :
I got a lot of content that must be processed by a lot of different process.
So, each process uses a specific tag (or 2/3), ask for content not tagged, processes it and tags it.

Code:
public class Content{
   @Id @GeneratedValue
   private Long id;

   @OneToMany
   @JoinTable(name = "Content_Tags)
   private Set<Tag> tags;

   @Temporal(TemporalType.TIMESTAMP)
   private Date addedDate;

   ...
}


Code:
public class Tag{
   @Id @GeneratedValue
   private Long id;

   private String name;

   ...
}



So the query is something like:

Code:
select from Content c where not exists(select from Tag t where t.contentId=c.id and t.name in (:excludedList) ) order by addedDate asc limit 1


Can you write it for me with DetachedCriteria ? (or HQL but rest of the application uses Criteria...)


Thanks by advance for reading this post and all my apologize for my english nostyle.


Top
 Profile  
 
 Post subject: Re: Making a query with kind of 'not exists'
PostPosted: Tue Oct 25, 2011 5:05 am 
Newbie

Joined: Fri Nov 12, 2010 4:03 pm
Posts: 5
Ok, I got it in SQL but it's very insane to see these SQL+ResultTransformer in the project.

Nobody has an idea and the time to post it? Please...


Top
 Profile  
 
 Post subject: Re: Making a query with kind of 'not exists'
PostPosted: Wed Oct 26, 2011 4:28 am 
Newbie

Joined: Fri Nov 12, 2010 4:03 pm
Posts: 5
Ok, still pending with a non-complete solution.
This request is ok but if content has at least one tag not in tagsExcluded list, it matches the request.
How be sure to avoid that?
Could you correct : Restrictions.not(Property.forName("tags.id").in(tagsExcludedCriteria))
Code:
      final DetachedCriteria criteria = DetachedCriteria.forClass(Content.class, "c");

      if (!tagsExcluded.isEmpty()) {
         LOGGER.trace("Adding excludedTags restrictions");
         criteria.createAlias("c.tags", "tags");

         final DetachedCriteria tagsExcludedCriteria = DetachedCriteria.forClass(Tag.class, "t2");
         tagsExcludedCriteria.add(Property.forName("t2.name").in(tagsExcluded));
         tagsExcludedCriteria.setProjection(Projections.id());

         criteria.add(Restrictions.not(Property.forName("tags.id").in(tagsExcludedCriteria)));
      }
      criteria.addOrder(Order.asc("downloadedDate"));
      return findUniqueResultByCriteria(criteria);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.