We migrated from Hibernate 3.6 to 5.6 and I am currently rewriting legacy Criteria API calls to JPA CriteriaBuilder. When I try to call the method root.gey(key), I get this exception:
{{java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [userName] on this ManagedType [com.myapp.ums.business.impl.UmsUserRoleVOImpl]}}
The reason for this is that the attributes that are set as key-properties could not be found in {{AbstractManagedType#getAttribute(String name)}}:
!image-20220926-131848.png|width=1310,height=241!
Here is the code I use to prepare the DB queries:
{code:java}CriteriaBuilder cb = this.getSession().getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(aliasToBeanClass); Root root = cq.from(aliasToBeanClass); // aliasToBeanClass is set to UmsUserRoleVOImpl
List<Predicate> predicates = new ArrayList<>(); predicates.add(cb.equal(root.get(key), value)); // key is set to "userName" - this line is causing the exception {code}
*My mapping file*
{{<?xml version="1.0"?>}} {{<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"}} {{ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">}} {{<hibernate-mapping>}} {{ <class name="com.myapp.ums.business.impl.UmsUserRoleVOImpl" table="ums_t_user_role"}} {{ proxy="com.myapp.ums.common.business.UmsUserRoleVO">}} {{ <composite-id>}} {{ <key-property name="userName" type="java.lang.String" column="fk_user_id"/>}} {{ <key-property name="roleName" type="java.lang.String" column="fk_role_id"/>}} {{ </composite-id>}} {{ </class>}} {{</hibernate-mapping>}}
*The interface:*
{code:java}public interface UmsUserRoleVO extends AbstractBaseProperties { String ROLE_NAME = "roleName"; String USER_NAME = "userName"; String PROPERTIES = "properties";
String roleName = "roleName"; String userName = "userName"; String getUserName(); void setUserName(final String userName); String getRoleName(); void setRoleName(final String roleName); List<PmPropertyVO> getProperties(); void setProperties(List<PmPropertyVO> properties); }{code}
*The entity:*
{code:java}package com.myapp.ums.business.impl;
public class UmsUserRoleVOImpl extends AbstractBasePropertiesImpl implements UmsUserRoleVO {
private static final long serialVersionUID = 6390635151578885216L; private String roleName; private String userName; private List<PmPropertyVO> properties; public UmsUserRoleVOImpl() { } @Override public Long getRowguid() { return super.getRowguid(); } @Override public void setRowguid(final Long rowguid) { super.setRowguid(rowguid); } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } public List<PmPropertyVO> getProperties() { return properties; } public void setProperties(List<PmPropertyVO> properties) { this.properties = properties; } public boolean equals(Object o) { ... } public int hashCode() { ... } public String toString() { return roleName + " " + userName; } @Get(UmsUserRoleVO.USER_NAME) public String getUserName() { return this.userName; } @Set(UmsUserRoleVO.USER_NAME) public void setUserName(String userName) { this.userName = userName; } }{code}
I think it has something to do with the mapping file not correctly processed. |
|