foxyboa wrote:
Hello!
Hibernate 3.0.4
I wish to use sqlRestriction and IN SQL statement. What must I use as third parameter (Type) in Restrictions.sqlRestriction? I wish check values in some Set or List.
Does anyone know how can I alive such code sqlRestriction:
Code:
final Criteria cr = s.createCriteria(Product.class).createCriteria("responsible");
cr.add(Restrictions.sqlRestriction("{alias}.branches IN (?)", u.getBranches(), ???));
//For simple data type everything is easy
cr.add(Restrictions.sqlRestriction("{alias}.firstName like ?", "%", Hibernate.STRING));
You can do the following. It isn't very elegant, but I'm afraid it's your only alternative.
Code:
final Criteria cr = s.createCriteria(Product.class).createCriteria("responsible");
final org.hibernate.type.Type[] types = new org.hibernate.type.Type[catIds.size()];
Arrays.fill(types, org.hibernate.Hibernate.LONG); //Let's suppose you have LONG values inside that getBranches().
final StringBuilder questionMarks= new StringBuilder();
for(int i=0;i<u.getBranches().size();i++){
if(i>0) questionMarks.append(",");
questionMarks.append("?");
}
cr.add(Restrictions.sqlRestriction("{alias}.branches IN ("+questionMarks.toString()+")", u.getBranches().toArray(), types));