1

I have two entities...

File and User

File has a @ManyToOne Reference to User with the following annotation on the member:

@ManyToOne
@JoinColumn(name="user_id")
private User user

I'm trying to find all the files for a user by using the "findByExample" method.

I've created an instance of File and an instance of User (with the username set). I've added the instance of User to the File instance and passed it to "findByExample" like this:

Criteria crit = getSession().createCriteria(File.class);
Example ex = Example.create(exampleInstance);
crit.add(ex);
return crit.list();

Unfortunately "list()" returns all the files in my db, not just the ones for the specified user. I inspected the generated SQL and I see that it's not inspecting the User object for any values, just joining the tables and doing a "where (1=1)".

Is it possible that hibernate does not inspect non-primitive members when doing a "findByExample" query?

1 Answer 1

1

An old TODO in org.hibernate.criterion.Example class line 207 explains this issue:

//TODO: get all properties, not just the fetched ones!
Object[] propertyValues = meta.getPropertyValues( entity, getEntityMode(criteria, criteriaQuery) );

My workaround was compose a Criteria with the example object (following the original question):

Criteria crit = getSession().createCriteria(File.class);
Example ex = Example.create(exampleInstance);
crit.add(ex).add(Restrictions.eq("user", exampleInstance.getUser()));
return crit.list();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.