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):
Here is the code I use to prepare the DB queries:
CriteriaBuilder cb = this.getSession().getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(aliasToBeanClass);
Root root = cq.from(aliasToBeanClass);
List<Predicate> predicates = new ArrayList<>();
predicates.add(cb.equal(root.get(key), value));
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:
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);
}
The entity:
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;
}
}
I think it has something to do with the mapping file not correctly processed. |