Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -55,6 +55,7 @@
import java.util.function.BiFunction;

import static java.util.Collections.unmodifiableMap;
import static java.util.stream.Collectors.toSet;
import static org.hibernate.metamodel.internal.InjectionHelper.injectField;

/**
Expand Down Expand Up @@ -345,14 +346,16 @@ else if ( MappedSuperclass.class.isAssignableFrom( mapping.getClass() ) ) {
final MappedSuperclassDomainType<Object> jpaType = (MappedSuperclassDomainType<Object>)
mappedSuperclassByMappedSuperclassMapping.get( safeMapping );

applyIdMetadata( safeMapping, jpaType );
final Set<String> idProperties = applyIdMetadata( safeMapping, jpaType );
applyVersionAttribute( safeMapping, jpaType );
// applyNaturalIdAttribute( safeMapping, jpaType );

for ( Property property : safeMapping.getDeclaredProperties() ) {
if ( !safeMapping.isVersioned()
if ( !idProperties.contains( property.getName() )
// skip already applied properties
&& (!safeMapping.isVersioned()
// skip the version property, it was already handled previously.
|| property != safeMapping.getVersion() ) {
|| property != safeMapping.getVersion()) ) {
buildAttribute( property, jpaType );
}
}
Expand Down Expand Up @@ -568,7 +571,7 @@ private EmbeddableTypeImpl<?> applyIdClassMetadata(Component idClassComponent) {
return embeddableType;
}

private <X> void applyIdMetadata(MappedSuperclass mappingType, MappedSuperclassDomainType<X> jpaMappingType) {
private <X> Set<String> applyIdMetadata(MappedSuperclass mappingType, MappedSuperclassDomainType<X> jpaMappingType) {
@SuppressWarnings("unchecked")
final AttributeContainer<X> attributeContainer = (AttributeContainer<X>) jpaMappingType;
if ( mappingType.hasIdentifierProperty() ) {
Expand All @@ -582,6 +585,7 @@ private <X> void applyIdMetadata(MappedSuperclass mappingType, MappedSuperclassD
attributeFactory::buildIdAttribute
);
attributeContainer.getInFlightAccess().applyIdAttribute( attribute );
return Set.of(attribute.getName());
}
}
//a MappedSuperclass can have no identifier if the id is set below in the hierarchy
Expand All @@ -592,7 +596,9 @@ else if ( mappingType.getIdentifierMapper() != null ) {
mappingType.getIdentifierMapper().getProperties()
);
attributeContainer.getInFlightAccess().applyIdClassAttributes( attributes );
return attributes.stream().map( Attribute::getName ).collect( toSet());
}
return Set.of();
}

private <X> void applyVersionAttribute(PersistentClass persistentClass, EntityDomainType<X> jpaEntityType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.metamodel.hhh18868;

import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.MappedSuperclass;

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

@IdClass(PKey.class)
@MappedSuperclass
public class BaseSummary implements Serializable {

@Id
private Integer year;
@Id
private Integer month;
private BigDecimal value;

public Integer getYear() {
return year;
}

public void setYear(Integer year) {
this.year = year;
}

public Integer getMonth() {
return month;
}

public void setMonth(Integer month) {
this.month = month;
}

public BigDecimal getValue() {
return value;
}

public void setValue(BigDecimal value) {
this.value = value;
}

@Override
public boolean equals(Object o) {
if ( o == null || getClass() != o.getClass() ) {
return false;
}
BaseSummary that = (BaseSummary) o;
return Objects.equals( year, that.year ) && Objects.equals( month, that.month );
}

@Override
public int hashCode() {
return Objects.hash( year, month );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.metamodel.hhh18868;

import jakarta.persistence.metamodel.SingularAttribute;
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 static org.assertj.core.api.Assertions.assertThat;

@DomainModel(annotatedClasses = {Summary.class, BaseSummary.class})
@SessionFactory
@JiraKey( "HHH-18858" )
public class HHH18868Test {

@Test
public void test(SessionFactoryScope scope) {
scope.inSession( entityManager -> {
final var yearAttribute = Summary_.year.getDeclaringType().getAttribute( "year" );
assertThat( yearAttribute ).isEqualTo( Summary_.year );
assertThat( ((SingularAttribute<?, ?>) yearAttribute).isId() ).isTrue();

final var monthAttribute = Summary_.month.getDeclaringType().getAttribute( "month" );
assertThat( monthAttribute ).isEqualTo( Summary_.month );
assertThat( ((SingularAttribute<?, ?>) monthAttribute).isId() ).isTrue();
} );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.metamodel.hhh18868;

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

public class PKey implements Serializable {

private Integer year;
private Integer month;

public Integer getYear() {
return year;
}

public void setYear(Integer year) {
this.year = year;
}

public Integer getMonth() {
return month;
}

public void setMonth(Integer month) {
this.month = month;
}

@Override
public boolean equals(Object o) {
if ( o == null || getClass() != o.getClass() ) {
return false;
}
PKey pKey = (PKey) o;
return Objects.equals( year, pKey.year ) && Objects.equals( month, pKey.month );
}

@Override
public int hashCode() {
return Objects.hash( year, month );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.metamodel.hhh18868;

import jakarta.persistence.Entity;

@Entity
public class Summary extends BaseSummary {
}
Loading