views:

34

answers:

2

I have the following entity (not exact but gives a general idea):

@Entity
public class WebElement implements Serializable {
   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue
   private Long id;

   @ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
   private Set<CoreElement> coreElements;

   private String agent;

   // ... omitting const' get/set hashcode equals etc.
}
public class CoreElement implements Serializable {
   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue
   private Long id;

   private String value;
   // ... omitting const' get/set hashcode equals etc.
}

My problem is when trying to fetch WebElements using the Criteria API vs. HQL
When executing the following I get an empty list.

getCurrentSession().createCriteria(WebElement.class)
                        .createCriteria("coreElements").add(
                                        Restrictions.eq("value", value)).list();

But when executing the following HQL I get the correct result.

select distinct we from WebElement we, in(we.coreElements) core 
                                  where core.value = :inputValue

Can you help finding what am I doing wrong or different between those calls?
(NOTE My preference is to work with the Criteria API instead of HQLs.

A: 

You are using a Restrictions.eq instead of Restrictions.in()..as you are using the HQL.

khmarbaise
The in() method is HQL short for inner join. Both calls produce the (almost) the same SQL. I changed the Criteria API to work with 'in' but got the same no-result outcome.
Bivas
A: 

In your HQL you are creating an inner join which causes Hibernate to fetch the elements.

In the Criteria Query you can use setFetchMode() using FetchMode.JOIN

Since your query is static, I would recommend using HQL - it's easier to understand.

Eran Harel
Tried that too, fetch mode didn't change the result. I left it with HQL but still this issue bugs me.
Bivas
show us you code then...
Eran Harel