Skip to content

Commit 2e90f46

Browse files
committed
HHH-6328 Test and fix SecondaryRow/Table name matching with respect to physical name strategies
1 parent ebd7a9e commit 2e90f46

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,15 +2045,15 @@ private SecondaryTable findMatchingSecondaryTable(Join join) {
20452045

20462046
private SecondaryRow findMatchingSecondaryRowAnnotation(String tableName) {
20472047
final SecondaryRow row = annotatedClass.getDirectAnnotationUsage( SecondaryRow.class );
2048-
if ( row != null && ( row.table().isBlank() || tableName.equals( row.table() ) ) ) {
2048+
if ( row != null && ( row.table().isBlank() || equalsTableName( tableName, row ) ) ) {
20492049
return row;
20502050
}
20512051
else {
20522052
final SecondaryRows tables = annotatedClass.getDirectAnnotationUsage( SecondaryRows.class );
20532053
if ( tables != null ) {
20542054
final SecondaryRow[] rowList = tables.value();
20552055
for ( SecondaryRow current : rowList ) {
2056-
if ( tableName.equals( current.table() ) ) {
2056+
if ( equalsTableName( tableName, current ) ) {
20572057
return current;
20582058
}
20592059
}
@@ -2062,6 +2062,13 @@ private SecondaryRow findMatchingSecondaryRowAnnotation(String tableName) {
20622062
}
20632063
}
20642064

2065+
private boolean equalsTableName(String physicalTableName, SecondaryRow secondaryRow) {
2066+
final Identifier logicalName = context.getMetadataCollector().getDatabase().toIdentifier( secondaryRow.table() );
2067+
final Identifier secondaryRowPhysicalTableName = context.getBuildingOptions().getPhysicalNamingStrategy()
2068+
.toPhysicalTableName( logicalName, EntityTableNamingStrategyHelper.jdbcEnvironment( context ) );
2069+
return physicalTableName.equals( secondaryRowPhysicalTableName.render() );
2070+
}
2071+
20652072
//Used for @*ToMany @JoinTable
20662073
public Join addJoinTable(JoinTable joinTable, PropertyHolder holder, boolean noDelayInPkColumnCreation) {
20672074
final Join join = addJoin(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.annotations.secondarytable;
6+
7+
import jakarta.persistence.Column;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.Id;
10+
import jakarta.persistence.PrimaryKeyJoinColumn;
11+
import jakarta.persistence.SecondaryTable;
12+
import org.hibernate.annotations.SecondaryRow;
13+
import org.hibernate.boot.model.naming.Identifier;
14+
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
15+
import org.hibernate.cfg.AvailableSettings;
16+
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
17+
import org.hibernate.persister.entity.EntityPersister;
18+
import org.hibernate.persister.entity.mutation.EntityTableMapping;
19+
import org.hibernate.testing.orm.junit.DomainModel;
20+
import org.hibernate.testing.orm.junit.JiraKey;
21+
import org.hibernate.testing.orm.junit.ServiceRegistry;
22+
import org.hibernate.testing.orm.junit.SessionFactory;
23+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
24+
import org.hibernate.testing.orm.junit.Setting;
25+
import org.junit.jupiter.api.Test;
26+
27+
import static org.junit.jupiter.api.Assertions.assertFalse;
28+
29+
@ServiceRegistry(
30+
settings = {
31+
@Setting(name = AvailableSettings.PHYSICAL_NAMING_STRATEGY, value = "org.hibernate.orm.test.annotations.secondarytable.SecondaryTableQuotingTest$TestNamingStrategy")
32+
}
33+
)
34+
@DomainModel(annotatedClasses = SecondaryTableQuotingTest.Foo.class)
35+
@SessionFactory
36+
@JiraKey(value = "HHH-6328")
37+
public class SecondaryTableQuotingTest {
38+
39+
@Test
40+
public void test(SessionFactoryScope scope) {
41+
final EntityPersister entityDescriptor = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel()
42+
.getEntityDescriptor( Foo.class );
43+
final EntityTableMapping secondaryTableMapping = entityDescriptor.getTableMappings()[1];
44+
assertFalse( secondaryTableMapping.isOptional() );
45+
}
46+
47+
@Entity(name = "Foo")
48+
@SecondaryTable(name = "bar", pkJoinColumns = {@PrimaryKeyJoinColumn(name = "foo_id")})
49+
@SecondaryRow(table = "bar", optional = false)
50+
public static class Foo {
51+
@Id
52+
private Long id;
53+
private String name;
54+
@Column(name = "bar_value", table = "bar")
55+
private Long barValue;
56+
57+
}
58+
59+
public static class TestNamingStrategy extends PhysicalNamingStrategyStandardImpl {
60+
@Override
61+
public Identifier toPhysicalTableName(Identifier logicalName, JdbcEnvironment jdbcEnvironment) {
62+
return Identifier.toIdentifier( "TAB_" + logicalName.getText() );
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)