Skip to content

HHH-19706 Composite @Id with a generic part in @MappedSuperclass #10749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,8 @@ private static Member resolveVirtualIdentifierMember( Property property, EntityP

private static Member resolveEntityMember(Property property, EntityPersister declaringEntity) {
final String propertyName = property.getName();
final var attributeMapping = declaringEntity.findAttributeMapping( propertyName );
return attributeMapping == null
return !propertyName.equals( declaringEntity.getIdentifierPropertyName() )
&& declaringEntity.findAttributeMapping( propertyName ) == null
// just like in #determineIdentifierJavaMember , this *should* indicate we have an IdClass mapping
? resolveVirtualIdentifierMember( property, declaringEntity )
: getter( declaringEntity, property, propertyName, property.getType().getReturnedClass() );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.mapping.generics.compositeid;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.MappedSuperclass;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;

import java.io.Serializable;
import java.util.Objects;

@JiraKey("HHH-19706")
@DomainModel(
annotatedClasses = {
CompositeIdWithGenericPartInMappedSuperclassTest.SampleCompositeIdEntity.class,
CompositeIdWithGenericPartInMappedSuperclassTest.SampleEntity.class,
CompositeIdWithGenericPartInMappedSuperclassTest.SampleSuperclass.class
}
)
@SessionFactory
class CompositeIdWithGenericPartInMappedSuperclassTest {

@Test
void test(SessionFactoryScope scope) {
scope.inSession( s -> s.createQuery( "from SampleEntity", SampleEntity.class ).getResultList() );
}

@Entity
@IdClass(SampleCompositeIdEntity.AdditionalIdEntityId.class)
static class SampleCompositeIdEntity extends SampleSuperclass<Long> {

@Id
@Column(nullable = false, updatable = false)
private Long additionalId;

static class AdditionalIdEntityId implements Serializable {

final Long mainId;
final Long additionalId;

public AdditionalIdEntityId() {
this( 0L, 0L );
}

public AdditionalIdEntityId(Long mainId, Long additionalId) {
this.mainId = mainId;
this.additionalId = additionalId;
}

@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( !(o instanceof AdditionalIdEntityId) ) {
return false;
}
AdditionalIdEntityId key = (AdditionalIdEntityId) o;
return Objects.equals( mainId, key.mainId ) && Objects.equals( additionalId, key.additionalId );
}

@Override
public int hashCode() {
return Objects.hash( mainId, additionalId );
}

}

}

@Entity(name = "SampleEntity")
static class SampleEntity extends SampleSuperclass<Long> {

private String sampleField;

}

@MappedSuperclass
abstract static class SampleSuperclass<K extends Serializable & Comparable<K>> {

@Id
private K mainId;

}
}