-->
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.  [ 1 post ] 
Author Message
 Post subject: JPA 2.0 CriteriaBuilder API and CollectionOfElements
PostPosted: Wed Aug 04, 2010 6:13 am 
Newbie

Joined: Wed Aug 04, 2010 5:22 am
Posts: 1
I am trying to do a search using the JPA 2.0 CriteriaBuilder API and the CollectionOfElements annotation.

My situation is as follow:
I have an User class which had a collection of elements which is a Set of Role enum values and I want to search for users which have one of more roles. I want to use the CriteriaBuilder, because I have a search form with a lot of fields which are optional.

I am using hibernate version 3.5.4 final and my code is like this:

The User class:
Code:
@Entity
@Table(name = "USERS")
@Access(AccessType.FIELD)
public class User implements Serializable {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "ID")
   private Long id;

   @Column(name = "USERNAME", unique = true, nullable = false, length = 128)
   private String username;

   @ElementCollection
   @Enumerated(EnumType.STRING)
   @Column(name = "ROLE")
   @CollectionTable(name = "USER_ROLES", joinColumns = @JoinColumn(name = "USER_ID"))
   private Set<Role> roles = new HashSet<Role>();

   ...
}


The enum Role:
Code:
public enum Role {
   ROLE_1, ROLE_2, ROLE_3;
}


Creating the query with the CriteriaBuilder
Code:
Set<Role> roles = new HashSet<Role>();
roles.add(Role.ROLE_1);
roles.add(Role.ROLE_2);

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<User> criteriaQuery = criteriaBuilder.createQuery(User.class);

Root<User> root = criteriaQuery.from(User.class);

criteriaQuery.where(root.join("roles").in(roles));

TypedQuery<User> query = entityManager.createQuery(criteriaQuery);
query.getResultList();


This is the exception:
Code:
java.lang.IllegalArgumentException: Parameter value [ROLE_2] was not matching type [java.util.Set]
   at org.hibernate.ejb.AbstractQueryImpl.registerParameterBinding(AbstractQueryImpl.java:360)
   at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:359)
   at org.hibernate.ejb.criteria.CriteriaQueryCompiler$1$1.bind(CriteriaQueryCompiler.java:194)
   at org.hibernate.ejb.criteria.CriteriaQueryCompiler.compile(CriteriaQueryCompiler.java:247)
   at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:437)
...


I am probably doing something wrong...

**UPDATE**
...not because it works with OpenJPA 2.0.0

How can I report a bug at Hibernate?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.