Skip to content

Commit

Permalink
HHH-18829 Hibernate Core: use ID class generated by Hibernate Process…
Browse files Browse the repository at this point in the history
…or when class is not annotated with @IdClass, but have more than one member annotated with @id
  • Loading branch information
cigaly committed Nov 14, 2024
1 parent a6d5f2d commit a16ce73
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,17 @@ private boolean mapAsIdClass(
final ClassDetails classWithIdClass = inheritanceState.getClassWithIdClass( false );
if ( classWithIdClass != null ) {
final IdClass idClassAnn = classWithIdClass.getDirectAnnotationUsage( IdClass.class );
final Class<?> idClassValue = idClassAnn.value();
final ClassDetails compositeClass =
getMetadataCollector().getSourceModelBuildingContext().getClassDetailsRegistry()
.resolveClassDetails( idClassValue.getName() );
final ClassDetails compositeClass;
if ( idClassAnn == null ) {
compositeClass = getMetadataCollector().getSourceModelBuildingContext()
.getClassDetailsRegistry()
.resolveClassDetails( inheritanceState.getClassDetails().getClassName() + "_$Id" );
}
else {
final Class<?> idClassValue = idClassAnn.value();
compositeClass = getMetadataCollector().getSourceModelBuildingContext()
.getClassDetailsRegistry().resolveClassDetails( idClassValue.getName() );
}
final TypeDetails compositeType = new ClassTypeDetailsImpl( compositeClass, TypeDetails.Kind.CLASS );
final TypeDetails classWithIdType = new ClassTypeDetailsImpl( classWithIdClass, TypeDetails.Kind.CLASS );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import org.hibernate.AnnotationException;
import org.hibernate.boot.spi.AccessType;
Expand Down Expand Up @@ -183,6 +184,13 @@ else if ( classDetails.hasDirectAnnotationUsage( IdClass.class ) ) {
return classDetails;
}
else {
final long count = Stream.concat(
classDetails.getFields().stream(),
classDetails.getMethods().stream()
).filter( t -> t.hasDirectAnnotationUsage( Id.class ) ).count();
if ( count > 1 ) {
return classDetails;
}
final InheritanceState state = getSuperclassInheritanceState( classDetails, inheritanceStatePerClass );
if ( state != null ) {
return state.getClassWithIdClass( true );
Expand Down

0 comments on commit a16ce73

Please sign in to comment.