I’m using JPA 2.0 with Hibernate 4.1.0.Final. I have a couple of classes, Groups and GroupMembers. Each GroupMember is tied to a user object
@Entity
@Table
How about creating a procedure in SQL and calling the same using JPA? I created the following for calling login procedure I created.
@Query(nativeQuery = true,value = "{call Login_User(:username,:pass)}") // call stored procedure
int loginUser(@Param("username")String username,@Param("pass")String pass);
And the SQL Procedure I had created was as following:
DELIMITER //
CREATE PROCEDURE Login_User(IN username CHAR(12), IN pass text(255))
BEGIN
DECLARE password text(18);
SELECT User_Password INTO password FROM user WHERE User_ID = username;
select if(STRCMP(pass,password)= 0,1,0) as str_compare;
END//
DELIMITER ;
Hope this helps. Cheers! I'm open to know more about the same. I know mine is more like a workaround rather than the solution :)
You will need different roots for each and every user, as each of them is different from the others (untested):
final CriteriaBuilder builder = m_entityManager.getCriteriaBuilder();
CriteriaQuery<Group> criteria = builder.createQuery(Group.class);
final List<Predicate> predicates = new ArrayList<Predicate>();
final Root<Group> group = criteria.from(Group.class);
for (final User user : users)
{
final Root<GroupMember> memberRoot = group.join(Group_.members);
final Predicate p = builder.equal(memberRoot.get(GroupMember_.user), user);
predicates.add(p);
} // for
predicates.add(builder.equals(builder.count(group.get(Group_.members)),users.size()))
criteria.where(builder.and(predicates.toArray(new Predicate[predicates.size()])));
final TypedQuery<Group> results = m_entityManager.createQuery(criteria);