[JIRA] (HHH-15553) Not possible to fetch content from mapping tables, that only have <composite-id> attributes.
by Patrick Lübbecke (JIRA)
Patrick Lübbecke ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=61a7428... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiZWJiMDUwYzgw... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-15553?atlOrigin=eyJpIjoiZWJiMD... ) HHH-15553 ( https://hibernate.atlassian.net/browse/HHH-15553?atlOrigin=eyJpIjoiZWJiMD... ) Not possible to fetch content from mapping tables, that only have <composite-id> attributes. ( https://hibernate.atlassian.net/browse/HHH-15553?atlOrigin=eyJpIjoiZWJiMD... )
Change By: Patrick Lübbecke ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=61a7428... )
+ * Short summary:*+ it is not possible to fetch content from My 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.net org / dtd/ hibernate-mapping-3.0.dtd"> }}
{{
<hibernate-mapping> }}
{{ <class table="CompId" name=" com org. myapp hibernate. ums bugs. business.impl.UmsUserRoleVOImpl CompIdVOImpl " table="ums_t_user_role"}}
{{ proxy=" com org. myapp hibernate. ums bugs. common.business.UmsUserRoleVO CompIdVO "> }}
{{ <composite-id> }}
{{ <key-property name=" userName id1 " type column =" java.lang.String id1 " column length =" fk_user_id 32 " type="string" /> }}
{{ <key-property name=" roleName id2 " type column =" java.lang.String id2 " column length =" fk_role_id 32 " type="string" /> }}
{{ </composite-id> }}
{{ </class> }}
{{ </hibernate-mapping>
{noformat } }
*The entity interface:*
{ code:java noformat } 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);
}{ code noformat }
*The entity implementation :*
{ code:java noformat }package com org. myapp hibernate. ums bugs;
import java. business io. impl Serializable ;
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. roleName id1 = roleName id1 ;
}
@Override
public List<PmPropertyVO> getProperties String getId2 () {
return properties id2 ;
}
@Override
public void setProperties setId2 ( List<PmPropertyVO> properties String id2 ) {
this. properties id2 = 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 toString get ( "id1" ) { , "1"));
return roleName + predicates.add(cb.equal(root.get( " id2 " + userName ), "2")) ;
}
@Get cq.where ( UmsUserRoleVO cb. USER_NAME and(predicates.toArray(new Predicate[0] ) ));
public String getUserName Query query = session.createQuery ( cq ) { ;
return this
List<CompIdVO> entities = query. userName getResultList() ; {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 ( UmsUserRoleVO AbstractManagedType. USER_NAME java:148 )
public void setUserName at org.hibernate.metamodel.model.domain.internal.AbstractManagedType.getAttribute ( String userName AbstractManagedType.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) { code noformat }
I think it has something That 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-illegalargumen... 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]
( https://hibernate.atlassian.net/browse/HHH-15553#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-15553#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100209- sha1:392b984 )
2 years, 2 months