-->
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.  [ 2 posts ] 
Author Message
 Post subject: Adding criteria to many-to-many
PostPosted: Fri Jul 04, 2008 7:25 am 
Newbie

Joined: Fri Jul 04, 2008 7:13 am
Posts: 1
Hello,

I have searched the internet and this forum, but haven't been able to find some hints to get things done. So, here it goes:

I have this situation.
- Table 'Articles'
- Table 'Categories'
- Jointable 'Article_categories'

The relation between articles and categories is a many-to-many. Easy to map this.

The class Category has a method getArticles that returns a set with all related articles. I'm mapping this using the known way, but I want something extra: I do not want all articles, but only articles with the status 'Published'.

Is it possible to add this criteria to the mapping? I don't want to create a seperate query in my dao to get this data.

I tried some things with the formula-tag, but couldn't get it to work in this particular situation. Tried some examples with one-to-many (resulting in a one-to-one situation), but that's different form my situation.

Anyone an idea?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 07, 2008 8:37 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
This should be pretty east to do with a criteria query.

Just create an example class and set the property you're looking for to "publisher". Then, send that Example, which implements the Hibernate Criterion interface, to a findByCriterion method in a DAO.

It might look something like this:

Code:
package com.examscam.criteria;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import com.examscam.HibernateUtil;
import com.examscam.model.User;

public class FindVerifiedUsers {
  public static void main(String[] args) {
    User user = new User();
    user.setVerified(false);
    Example example = Example.create(user);
    Session session = HibernateUtil.beginTransaction();
    Criteria criteria = session.createCriteria(User.class);
    criteria.add(example);
    List results = criteria.list();
    HibernateUtil.commitTransaction();
    for (int i = 0; i<results.size(); i++) {
      System.out.println(results.get(i).toString());
    }
  }
}


From the Hibernate3 Tutorial on using the Hibernate Criteria API and JPA Annotations

This will work well with a findByExample or findByCriterion method in a Data Access Object:




Code:
package com.examscam.criteria;
import java.util.List;import org.hibernate.*;
import org.hibernate.criterion.*; import com.examscam.model.User;
import com.examscam.HibernateUtil;
public class UserHelper {
public static List findByCriterion(Criterion... criterion){
   Session session = HibernateUtil.getSession();
   Criteria criteria=session.createCriteria(User.class);
   for (Criterion crit : criterion) {
      criteria.add(crit);
   }
   return criteria.list();
}
}



Code:
public List<T> findByExample(T exampleInstance,
                          String[] excludeProperty) {
  Criteria crit = HibernateUtil.getSession()
                   .createCriteria(persistentClass);
  Example example = Example.create(exampleInstance);
  for (int i=0; i< excludeProperty.length; i++) {
    example.excludeProperty(excludeProperty[i]);
  }
  crit.add(example);
  return crit.list();
}


Creating Advanced Data Access Objects with Hibernate: Tutorial and Examples

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.