| We have an User entity. To collect some statistics we have a database view, counting comments, for instance. This view is mapped by a UserStatistic entity. The User entity references the UserStatistics. As the UserStatistic represents a database view it has the same id as the User entity and is referenced by the id column.
@Entity
public class User {
@Id
public Long id;
public String name;
@OneToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name="id", referencedColumnName="id", insertable=false, updatable=false)
public UserStatistic userStatistic;
}
Doing a query with a loadgraph loading the statistics for the user, everything is fine. The sql is as aspected.
If the user is get by id the statistic is missing. The sql.
I did some debugging and found the following reason:
MetamodelGraphWalker.visitAttributeDefinition
line 144:
if ( isDuplicateAssociationKey( associationKey ) ) { log.debug( "Property path deemed to be circular : " + subPath.getFullPath() );
strategy.foundCircularAssociation( associationAttributeDefinition );
return;
}
AbstractLoadPlanBuildingAssociationVisitationStrategy
line 185:
final Joinable entityPersister = (Joinable) entityDefinition.getEntityPersister();
associationKeyRegistered(
new AssociationKey( entityPersister.getTableName(), entityPersister.getKeyColumnNames() )
);
In AbstractLoadPlanBuildingAssociationVisitationStrategy an AssociationKey with table name and column name is generated to prevent circular references. As the user statistics uses the same column it is ignored in MetamodelGraphWalker. I am willing to deliver a patch for this bug, but I am not sure what would be the best way to solve it. To prevent circular refer it should either take the referenced table into account or the attribute in the entity? Why is this not happening if I get the entity by a query? PR with testcase will follow. Seems to be very simular to:
HHH-10745 - Relations are not loaded when using Fetch Profiles Open |