1

I have a class that I am attempting to query by "userid"

@Entity
@IdClass(CollectionPK.class)
@Table(name="collection", schema="mageduelsusers")
public class Collection{

@Id
@Column(name = "userid")
private int userId;

@Id
@Column(name = "cardid")
private int cardId;
...

Id class of

public class CollectionPK implements Serializable{

private int userId;
private int cardId;

public CollectionPK() {
    
}
...

Query code is

public List<Collection> readCollection(int id) {
    List<Collection> collection = null;
    Session session = factory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        
        CriteriaBuilder builder = session.getCriteriaBuilder();
        CriteriaQuery<Collection> criteriaQuery = builder.createQuery(Collection.class);
        Root<Collection> root = criteriaQuery.from(Collection.class);
        
        ParameterExpression userIdParameter = builder.parameter(Collection.class);
        
        criteriaQuery.where(builder.equal(root.get("userid"), userIdParameter));
        
        Query<Collection> query = session.createQuery(criteriaQuery);
        query.setParameter("userid", id);
        
        collection = query.getResultList();
        
        tx.commit();
    }
...

Error is

Exception in thread "main" java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [userid] on this ManagedType [com.panda.userinfo.Collection]

Ideal query would be Select * from collection where userid = 'userid';

How do I modify to make this work?

Pretty sure error is in the criteria builder section as session.save(). session.get(), and session.delete() all work properly

Update: Did a little bit of testing and the cause of the issue is definitely root.get("userid") Is there any way to check what Attributes hibernate has for a class?

Update2: Capitalizing the I in root.get("userId") fixes that error. However both forms still cause an error at query.setParameter("userId", id)

 java.lang.IllegalArgumentException: Unable to locate parameter registered with that name [userId]

Update 3: Figured it out or at least made it functional. Hibernate was renaming things in the background. Solved by printing everything to find the correct parameter name.

for(Parameter<?> p:query.getParameters()) {
    System.out.println(p.getName());
 }
 System.out.println(query.getParameters().size());

1 Answers1

0

Try to correct your query in this way:

CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Collection> criteriaQuery = builder.createQuery(Collection.class);
Root<Collection> root = criteriaQuery.from(Collection.class);
        
ParameterExpression<Integer> userIdParameter = builder.parameter(Integer.class);
        
criteriaQuery.where(builder.equal(root.get("userid"), userIdParameter));
        
List<Collection> collection = session.createQuery(criteriaQuery)
   .setParameter("userid", id)
   .getResultList();

See also this section of the documentation.

SternK
  • 11,649
  • 22
  • 32
  • 46