Skip to content
Merged
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 @@ -16,6 +16,7 @@
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.MappedSuperclass;
Expand Down Expand Up @@ -350,11 +351,17 @@ else if ( MappedSuperclass.class.isAssignableFrom( mapping.getClass() ) ) {
// applyNaturalIdAttribute( safeMapping, jpaType );

for ( Property property : safeMapping.getDeclaredProperties() ) {
if ( !safeMapping.isVersioned()
// skip the version property, it was already handled previously.
|| property != safeMapping.getVersion() ) {
buildAttribute( property, jpaType );
if ( isIdentifierProperty( property, safeMapping ) ) {
// property represents special handling for id-class mappings but we have already
// accounted for the embedded property mappings in #applyIdMetadata &&
// #buildIdClassAttributes
continue;
}
if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) {
// skip the version property, it was already handled previously.
continue;
}
buildAttribute( property, jpaType );
}

( (AttributeContainer<?>) jpaType ).getInFlightAccess().finishUp();
Expand Down Expand Up @@ -406,6 +413,12 @@ else if ( MappedSuperclass.class.isAssignableFrom( mapping.getClass() ) ) {
}
}

private static boolean isIdentifierProperty(Property property, MappedSuperclass mappedSuperclass) {
final Component identifierMapper = mappedSuperclass.getIdentifierMapper();
return identifierMapper != null ?
ArrayHelper.contains( identifierMapper.getPropertyNames(), property.getName() ) : false;
}

private <T> void addAttribute(EmbeddableDomainType<T> embeddable, Property property, Component component) {
final PersistentAttribute<T, ?> attribute =
attributeFactory.buildAttribute( embeddable, property);
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.idclass;

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.idclass;

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 MappedSuperclassIdClassAttributesTest {

@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.idclass;

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.idclass;

import jakarta.persistence.Entity;

@Entity
public class Summary extends BaseSummary {
}
Loading