| Currently we are using JOINED inheritance type. We have a specific case where 2 descendant entities have a many-to-one association with same attribute name. The problem is that Hibernate uses only first found attribute in entity hierarchy and the next one (with same name) will be ignored. Unfortunately the TREAT keyword doesn't help and the only workaround is to rename these attributes (for instance with indexes). I found this problem at two places in AbstractEntityPersister: 1. collectAttributeDefinitions(...):5659:
final AttributeDefinition oldAttributeDefinition = attributeDefinitionsByName.get(
attributeDefinition.getName()
);
if ( oldAttributeDefinition != null ) {
if ( LOG.isTraceEnabled() ) {
LOG.tracef("Ignoring subclass attribute definition [%s.%s] because it is defined in a superclass ", entityMetamodel.getName(),
attributeDefinition.getName()
);
}
}
2. At the end of getSubclassPropertyTableNumber(String propertyPath):
int index = ArrayHelper.indexOf(
getSubclassPropertyNameClosure(),
rootPropertyName
); return index == -1 ? 0 : getSubclassPropertyTableNumber( index );
I've attached a test case which executes the following JPQL:
SELECT ur FROM UserRole ur
JOIN TREAT(ur.role AS CustomerDependentRole1) cr
JOIN cr.customer c
WHERE ur.userName='etamfra' AND upper(cr.name) = 'CUSTOMER10_ROLE' AND c.name = 'Customer10'
It leads to following SQL:
select
userrole0_.id as id1_5_,
userrole0_.role_id as role_id3_5_,
userrole0_.user_name as user_nam2_5_
from user_role userrole0_
inner join role role1_ on userrole0_.role_id=role1_.id
left outer join base_role role1_1_ on role1_.id=role1_1_.id
left outer join customer_dependent_role2 role1_2_ on role1_.id=role1_2_.id
inner join customer_dependent_role1 role1_3_ on role1_.id=role1_3_.id
inner join customer customer2_ on role1_2_.customer_id=customer2_.id where userrole0_.user_name='etamfra'
and upper(role1_.name)='CUSTOMER10_ROLE'
and customer2_.name='Customer10'
I think the key of attribute caching should contain the entity type as well and TREATing should have an effect on alias calculation. |