+*Short summary:*+ it is not possible to fetch content fromMy mapping tables, that only have {{<composite-id>}} attributes.
I have 3 tables:
* User * Role * User_Role
We configure our entities using .hbm.xml files. The mapping file for User_Role has just a composite keys based on the IDs of User and Role. When fetching a tuple of the mapping table, I get this error in Hibernate 5.6.11:
{{java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [userName] on this ManagedType [com.myapp.ums.business.impl.UmsUserRoleVOImpl]noformat}}
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://www.hibernate.sourceforge.netorg/dtd/hibernate-mapping-3.0.dtd">}} {{ <hibernate-mapping>}} {{ <class table="CompId" name="comorg.myapphibernate.umsbugs.business.impl.UmsUserRoleVOImplCompIdVOImpl" table="ums_t_user_role"}} {{ proxy="comorg.myapphibernate.umsbugs.common.business.UmsUserRoleVOCompIdVO">}} {{ <composite-id>}} {{ <key-property name="userNameid1" type column="java.lang.Stringid1" column length="fk_user_id32" type="string"/>}} {{ <key-property name="roleNameid2" type column="java.lang.Stringid2" column length="fk_role_id32" type="string"/>}} {{ </composite-id>}} {{ </class>}} {{</hibernate-mapping> {noformat}}
*The entity interface:*
{code:javanoformat}package org.hibernate.bugs;
public interface UmsUserRoleVO extends AbstractBaseProperties CompIdVO { String ROLE_NAME = "roleName"; String USER_NAME = "userName"; String PROPERTIES = "properties"; public String getUserName getId1();
public void setUserName setId1(final String userName id1);
public String getRoleName getId2();
public void setRoleName setId2(final String roleName id2); List<PmPropertyVO> getProperties(); void setProperties(List<PmPropertyVO> properties); }{codenoformat}
*The entity implementation:*
{code:javanoformat}package com org.myapphibernate.umsbugs;
import java.businessio.implSerializable;
public class UmsUserRoleVOImpl extends AbstractBasePropertiesImpl CompIdVOImpl implements UmsUserRoleVO CompIdVO, Serializable {
private static final long serialVersionUID = 6390635151578885216L; private String roleName id1;
private String userName id2; private List<PmPropertyVO> properties;
public UmsUserRoleVOImpl CompIdVOImpl() {
} @Override public Long getRowguid CompIdVOImpl(String id1, String id2) { return super this.getRowguid()id1 = id1; this.id2 = id2; }
@Override public void setRowguid(final Long rowguid) { super.setRowguid(rowguid); } public String getRoleName getId1() { return roleName id1; }
@Override public void setRoleName setId1(String roleName id1) { this.roleNameid1 = roleName id1; }
@Override public List<PmPropertyVO> getProperties String getId2() { return properties id2; }
@Override public void setProperties setId2(List<PmPropertyVO> propertiesString id2) { this.propertiesid2 = properties id2; } public boolean equals}{noformat}
The error:
{noformat}CriteriaBuilder cb = session.getCriteriaBuilder(Object o) {; CriteriaQuery cq = cb .createQuery(CompIdVOImpl.class);
Root root = cq.from(CompIdVOImpl.class); } public int hashCode List<Predicate> predicates = new ArrayList<>() {; predicates .add(cb.equal(root. } public String toStringget("id1") {, "1")); return roleName + predicates.add(cb.equal(root.get( "id2 " + userName), "2")); } @Get cq.where(UmsUserRoleVOcb.USER_NAMEand(predicates.toArray(new Predicate[0]))); public String getUserName Query query = session.createQuery(cq) {; return this List<CompIdVO> entities = query.userNamegetResultList();{noformat}
Results in this error:
{noformat }java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [id1] on this ManagedType [org.hibernate.bugs.CompIdVOImpl] @Set at org.hibernate.metamodel.model.domain.internal.AbstractManagedType.checkNotNull(UmsUserRoleVOAbstractManagedType.USER_NAMEjava:148) public void setUserName at org.hibernate.metamodel.model.domain.internal.AbstractManagedType.getAttribute(String userNameAbstractManagedType.java:119) { this at org.userName = userName;hibernate.metamodel.model.domain.internal.AbstractManagedType.getAttribute(AbstractManagedType.java:44) } at org.hibernate.query.criteria.internal.path.AbstractFromImpl.locateAttributeInternal(AbstractFromImpl.java:111) } at org.hibernate.query.criteria.internal.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:204) at org.hibernate.query.criteria.internal.path.AbstractPathImpl.get(AbstractPathImpl.java:177) at org.hibernate.bugs.CompositeIdTest.testImplicitCompositeIdInDynamicMapMode(CompositeIdTest.java:52){codenoformat}
I think it has somethingThat is pretty much a blocker for us. Christian Beikov mentioned that this problem [might already be fixed in 6.0|https://stackoverflow.com/questions/73851482/java-lang-illegalargumentexception-unable- to do-locate-attribute- with- the mapping file not correctly processed-the-giv?noredirect=1#comment130483328_73851482]. I’d like to try to back port this fix to 5.6. Does anybody know which PR was relevant here?
*Here is a test case for the problem:*
[^CompositeId.zip]
|
|