HHH-16592 be more consistent with the spec
don't infer column name from @Id to @MapsId
This commit is contained in:
parent
38f761daca
commit
0f94777cfa
|
@ -335,19 +335,19 @@ public class AnnotatedJoinColumn extends AnnotatedColumn {
|
|||
|
||||
private String defaultColumnName(int columnIndex, PersistentClass referencedEntity, String logicalReferencedColumn) {
|
||||
final AnnotatedJoinColumns parent = getParent();
|
||||
if ( parent.hasMapsId() ) {
|
||||
// infer the join column of the association
|
||||
// from the name of the mapped primary key
|
||||
// column (this is not required by the JPA
|
||||
// spec) and is arguably backwards, given
|
||||
// the name of the @MapsId annotation, but
|
||||
// it's better than just having two different
|
||||
// column names which disagree
|
||||
return parent.resolveMapsId().getValue().getColumns().get( columnIndex ).getQuotedName();
|
||||
}
|
||||
else {
|
||||
// if ( parent.hasMapsId() ) {
|
||||
// // infer the join column of the association
|
||||
// // from the name of the mapped primary key
|
||||
// // column (this is not required by the JPA
|
||||
// // spec) and is arguably backwards, given
|
||||
// // the name of the @MapsId annotation, but
|
||||
// // it's better than just having two different
|
||||
// // column names which disagree
|
||||
// return parent.resolveMapsId().getValue().getColumns().get( columnIndex ).getQuotedName();
|
||||
// }
|
||||
// else {
|
||||
return parent.buildDefaultColumnName( referencedEntity, logicalReferencedColumn );
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
public void addDefaultJoinColumnName(PersistentClass referencedEntity, String logicalReferencedColumn) {
|
||||
|
|
|
@ -751,6 +751,21 @@ public class TableBinder {
|
|||
final AnnotatedJoinColumn firstColumn = joinColumns.getJoinColumns().get(0);
|
||||
firstColumn.linkValueUsingDefaultColumnNaming( i, column, referencedEntity, value );
|
||||
firstColumn.overrideFromReferencedColumnIfNecessary( column );
|
||||
final Column createdColumn = firstColumn.getMappingColumn();
|
||||
if ( createdColumn != null ) {
|
||||
final String logicalColumnName = createdColumn.getQuotedName();
|
||||
if ( logicalColumnName != null && joinColumns.hasMapsId() ) {
|
||||
final Value idValue = joinColumns.resolveMapsId().getValue();
|
||||
final Column idColumn = idValue.getColumns().get(i);
|
||||
// infer the names of the primary key column
|
||||
// from the join column of the association
|
||||
// as (sorta) required by the JPA spec
|
||||
if ( !idColumn.getQuotedName().equals(logicalColumnName) ) {
|
||||
idColumn.setName( logicalColumnName );
|
||||
idValue.getTable().columnRenamed( idColumn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -170,8 +170,7 @@ public class Column implements Selectable, Serializable, Cloneable, ColumnTypeIn
|
|||
|
||||
boolean useRawName = name.length() + suffix.length() <= dialect.getMaxAliasLength()
|
||||
&& !quoted
|
||||
// TODO: get the row id name from the Dialect
|
||||
&& !name.equalsIgnoreCase( "rowid" );
|
||||
&& !name.equalsIgnoreCase( dialect.rowId(null) );
|
||||
if ( !useRawName ) {
|
||||
if ( suffix.length() >= dialect.getMaxAliasLength() ) {
|
||||
throw new MappingException(
|
||||
|
@ -243,7 +242,7 @@ public class Column implements Selectable, Serializable, Cloneable, ColumnTypeIn
|
|||
public int getSqlTypeCode(Mapping mapping) throws MappingException {
|
||||
if ( sqlTypeCode == null ) {
|
||||
final Type type = getValue().getType();
|
||||
int[] sqlTypeCodes;
|
||||
final int[] sqlTypeCodes;
|
||||
try {
|
||||
sqlTypeCodes = type.getSqlTypeCodes( mapping );
|
||||
}
|
||||
|
@ -301,8 +300,9 @@ public class Column implements Selectable, Serializable, Cloneable, ColumnTypeIn
|
|||
|
||||
private static Type getUnderlyingType(Mapping mapping, Type type, int typeIndex) {
|
||||
if ( type.isComponentType() ) {
|
||||
final ComponentType componentType = (ComponentType) type;
|
||||
int cols = 0;
|
||||
for ( Type subtype : ((ComponentType) type).getSubtypes() ) {
|
||||
for ( Type subtype : componentType.getSubtypes() ) {
|
||||
int columnSpan = subtype.getColumnSpan( mapping );
|
||||
if ( cols+columnSpan > typeIndex ) {
|
||||
return getUnderlyingType( mapping, subtype, typeIndex-cols );
|
||||
|
@ -312,7 +312,8 @@ public class Column implements Selectable, Serializable, Cloneable, ColumnTypeIn
|
|||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
else if ( type.isEntityType() ) {
|
||||
Type idType = ((EntityType) type).getIdentifierOrUniqueKeyType(mapping);
|
||||
final EntityType entityType = (EntityType) type;
|
||||
final Type idType = entityType.getIdentifierOrUniqueKeyType( mapping );
|
||||
return getUnderlyingType( mapping, idType, typeIndex );
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -0,0 +1,125 @@
|
|||
package org.hibernate.orm.test.annotations.mapsid;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.IdClass;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.MapsId;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import org.hibernate.testing.orm.junit.DomainModel;
|
||||
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
@SessionFactory
|
||||
@DomainModel(annotatedClasses = {DefaultedMapsIdTest.Loan.class, DefaultedMapsIdTest.Extension.class})
|
||||
public class DefaultedMapsIdTest {
|
||||
|
||||
@Test void test(SessionFactoryScope scope) {
|
||||
ExtensionId eid = scope.fromTransaction( s -> {
|
||||
Loan loan = new Loan();
|
||||
loan.id = 999L;
|
||||
Extension extension = new Extension();
|
||||
extension.exLoanId = loan.id;
|
||||
extension.loan = loan;
|
||||
extension.exNo = 1;
|
||||
extension.exExtensionDays = 30;
|
||||
loan.extensions.add(extension);
|
||||
extension = new Extension();
|
||||
extension.exLoanId = loan.id;
|
||||
extension.loan = loan;
|
||||
extension.exNo = 2;
|
||||
extension.exExtensionDays = 14;
|
||||
loan.extensions.add(extension);
|
||||
s.persist(loan);
|
||||
return new ExtensionId(extension.exLoanId, extension.exNo );
|
||||
});
|
||||
scope.inSession( s -> {
|
||||
List<Extension> extensions = s.createQuery("from Extension", Extension.class).getResultList();
|
||||
assertEquals(2, extensions.size());
|
||||
} );
|
||||
scope.inSession( s -> {
|
||||
Extension extension = s.find(Extension.class, eid);
|
||||
assertEquals(14, extension.exExtensionDays);
|
||||
assertEquals(2, extension.exNo);
|
||||
assertEquals(999L, extension.exLoanId);
|
||||
assertNotNull( extension.loan );
|
||||
});
|
||||
scope.inSession( s -> {
|
||||
Loan loan = s.find(Loan.class, eid.exLoanId);
|
||||
Extension extension = loan.extensions.get(0);
|
||||
assertEquals(1, extension.exNo);
|
||||
assertEquals(30, extension.exExtensionDays);
|
||||
assertEquals(999L, extension.exLoanId);
|
||||
assertEquals(loan, extension.loan);
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "Loan")
|
||||
static class Loan {
|
||||
@Id
|
||||
@Column(name = "LOAN_ID")
|
||||
private Long id;
|
||||
|
||||
private BigDecimal amount = BigDecimal.ZERO;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, mappedBy = "loan")
|
||||
private List<Extension> extensions = new ArrayList<>();
|
||||
}
|
||||
|
||||
static class ExtensionId {
|
||||
private Long exLoanId;
|
||||
private int exNo;
|
||||
|
||||
public ExtensionId(Long exLoanId, int exNo) {
|
||||
this.exLoanId = exLoanId;
|
||||
this.exNo = exNo;
|
||||
}
|
||||
|
||||
public ExtensionId() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof ExtensionId)) return false;
|
||||
ExtensionId that = (ExtensionId) o;
|
||||
return exNo == that.exNo && Objects.equals(exLoanId, that.exLoanId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(exLoanId, exNo);
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "Extension")
|
||||
@IdClass(ExtensionId.class)
|
||||
static class Extension {
|
||||
@Id
|
||||
// @Column(name = "EX_LOAN_ID")
|
||||
private Long exLoanId;
|
||||
|
||||
@Id
|
||||
@Column(name = "EX_NO")
|
||||
private int exNo;
|
||||
|
||||
@Column(name = "EX_EXTENSION_DAYS")
|
||||
private int exExtensionDays;
|
||||
|
||||
@ManyToOne
|
||||
@MapsId("exLoanId")
|
||||
// @JoinColumn(name = "EX_LOAN_ID")
|
||||
private Loan loan;
|
||||
}
|
||||
}
|
|
@ -108,7 +108,7 @@ public class MapsIdTest {
|
|||
@IdClass(ExtensionId.class)
|
||||
static class Extension {
|
||||
@Id
|
||||
@Column(name = "EX_LOAN_ID")
|
||||
@Column(name = "EX_LOAN_ID") //TODO: this should really cause an error
|
||||
private Long exLoanId;
|
||||
|
||||
@Id
|
||||
|
|
Loading…
Reference in New Issue