Skip to content

Commit 2827a59

Browse files
committed
HHH-9788 - Schema generation re-triggered where it should not be
1 parent a50e5f3 commit 2827a59

File tree

4 files changed

+191
-35
lines changed

4 files changed

+191
-35
lines changed

hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/NormalizingIdentifierHelperImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public Identifier fromMetaDataSchemaName(String schemaName) {
268268
// return null;
269269
// }
270270

271-
return toIdentifier( schemaName );
271+
return toIdentifierFromMetaData( schemaName );
272272
}
273273

274274
@Override
@@ -277,6 +277,6 @@ public Identifier fromMetaDataObjectName(String objectName) {
277277
return null;
278278
}
279279

280-
return toIdentifier( objectName );
280+
return toIdentifierFromMetaData( objectName );
281281
}
282282
}

hibernate-core/src/main/java/org/hibernate/mapping/Table.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,20 @@
4242
import org.hibernate.engine.spi.Mapping;
4343
import org.hibernate.internal.util.StringHelper;
4444
import org.hibernate.tool.hbm2ddl.ColumnMetadata;
45+
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
4546
import org.hibernate.tool.hbm2ddl.TableMetadata;
4647
import org.hibernate.tool.schema.extract.spi.ColumnInformation;
4748
import org.hibernate.tool.schema.extract.spi.TableInformation;
4849

50+
import org.jboss.logging.Logger;
51+
4952
/**
5053
* A relational table
5154
*
5255
* @author Gavin King
5356
*/
5457
@SuppressWarnings("unchecked")
5558
public class Table implements RelationalModel, Serializable, Exportable {
56-
5759
private Identifier catalog;
5860
private Identifier schema;
5961
private Identifier name;
@@ -441,7 +443,7 @@ public Iterator sqlAlterStrings(
441443

442444
while ( iter.hasNext() ) {
443445
final Column column = (Column) iter.next();
444-
final ColumnInformation columnInfo = tableInfo.getColumn( Identifier.toIdentifier( column.getName() ) );
446+
final ColumnInformation columnInfo = tableInfo.getColumn( Identifier.toIdentifier( column.getName(), column.isQuoted() ) );
445447

446448
if ( columnInfo == null ) {
447449
// the column doesnt exist at all.
@@ -489,6 +491,10 @@ public Iterator sqlAlterStrings(
489491

490492
}
491493

494+
if ( results.isEmpty() ) {
495+
Logger.getLogger( SchemaUpdate.class ).debugf( "No alter strings for table : %s", getQuotedName() );
496+
}
497+
492498
return results.iterator();
493499
}
494500

hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/InformationExtractorJdbcDatabaseMetaDataImpl.java

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.hibernate.boot.model.TruthValue;
3838
import org.hibernate.boot.model.naming.Identifier;
3939
import org.hibernate.boot.model.relational.QualifiedTableName;
40-
import org.hibernate.boot.model.relational.Schema;
4140
import org.hibernate.cfg.AvailableSettings;
4241
import org.hibernate.engine.config.spi.ConfigurationService;
4342
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
@@ -140,7 +139,12 @@ public Collection<TableInformation> getTables(Identifier catalog, Identifier sch
140139
);
141140
try {
142141
while ( resultSet.next() ) {
143-
final TableInformation tableInformation = extractTableInformation( resultSet );
142+
final TableInformation tableInformation = extractTableInformation(
143+
catalog,
144+
schema,
145+
null,
146+
resultSet
147+
);
144148
results.add( tableInformation );
145149
}
146150
}
@@ -177,20 +181,23 @@ private String determineSchemaFilter(Identifier schema) throws SQLException {
177181
return extractionContext.getJdbcEnvironment().getIdentifierHelper().toMetaDataSchemaName( identifierToUse );
178182
}
179183

180-
public TableInformation extractTableInformation(ResultSet resultSet) throws SQLException {
181-
final Identifier catalogIdentifier = identifierHelper().fromMetaDataCatalogName(
182-
resultSet.getString( "TABLE_CAT" )
183-
);
184-
final Identifier schemaIdentifier = identifierHelper().fromMetaDataSchemaName(
185-
resultSet.getString( "TABLE_SCHEM" )
186-
);
187-
final Identifier tableIdentifier = identifierHelper().fromMetaDataObjectName(
188-
resultSet.getString( "TABLE_NAME" )
189-
);
190-
final QualifiedTableName tableName = new QualifiedTableName(
191-
new Schema.Name( catalogIdentifier, schemaIdentifier ),
192-
tableIdentifier
193-
);
184+
public TableInformation extractTableInformation(
185+
Identifier catalog,
186+
Identifier schema,
187+
Identifier name,
188+
ResultSet resultSet) throws SQLException {
189+
if ( catalog == null ) {
190+
catalog = identifierHelper().fromMetaDataCatalogName( resultSet.getString( "TABLE_CAT" ) );
191+
}
192+
if ( schema == null ) {
193+
schema = identifierHelper().fromMetaDataSchemaName( resultSet.getString( "TABLE_SCHEM" ) );
194+
}
195+
if ( name == null ) {
196+
name = identifierHelper().fromMetaDataObjectName( resultSet.getString( "TABLE_NAME" ) );
197+
}
198+
199+
final QualifiedTableName tableName = new QualifiedTableName( catalog, schema, name );
200+
194201
return new TableInformationImpl(
195202
this,
196203
tableName,
@@ -218,7 +225,12 @@ public TableInformation getTable(Identifier catalog, Identifier schema, Identifi
218225
return null;
219226
}
220227

221-
final TableInformation tableInformation = extractTableInformation( resultSet );
228+
final TableInformation tableInformation = extractTableInformation(
229+
catalog,
230+
schema,
231+
tableName,
232+
resultSet
233+
);
222234

223235
if ( resultSet.next() ) {
224236
log.multipleTablesFound( tableName.render() );
@@ -265,7 +277,7 @@ public Iterable<ColumnInformation> getColumns(TableInformation tableInformation)
265277
results.add(
266278
new ColumnInformationImpl(
267279
tableInformation,
268-
Identifier.toIdentifier( columnName ),
280+
identifierHelper().fromMetaDataObjectName( columnName ),
269281
resultSet.getInt( "DATA_TYPE" ),
270282
new StringTokenizer( resultSet.getString( "TYPE_NAME" ), "() " ).nextToken(),
271283
resultSet.getInt( "COLUMN_SIZE" ),
@@ -382,14 +394,18 @@ public Iterable<IndexInformation> getIndexes(TableInformation tableInformation)
382394
continue;
383395
}
384396

385-
final Identifier indexIdentifier = Identifier.toIdentifier( resultSet.getString( "INDEX_NAME" ) );
397+
final Identifier indexIdentifier = identifierHelper().fromMetaDataObjectName(
398+
resultSet.getString(
399+
"INDEX_NAME"
400+
)
401+
);
386402
IndexInformationImpl.Builder builder = builders.get( indexIdentifier );
387403
if ( builder == null ) {
388404
builder = IndexInformationImpl.builder( indexIdentifier );
389405
builders.put( indexIdentifier, builder );
390406
}
391407

392-
final Identifier columnIdentifier = Identifier.toIdentifier( resultSet.getString( "COLUMN_NAME" ) );
408+
final Identifier columnIdentifier = identifierHelper().fromMetaDataObjectName( resultSet.getString( "COLUMN_NAME" ) );
393409
final ColumnInformation columnInformation = tableInformation.getColumn( columnIdentifier );
394410
if ( columnInformation == null ) {
395411
throw new SchemaManagementException(
@@ -434,7 +450,9 @@ public Iterable<ForeignKeyInformation> getForeignKeys(TableInformation tableInfo
434450
try {
435451
while ( resultSet.next() ) {
436452
// IMPL NOTE : The builder is mainly used to collect the column reference mappings
437-
final Identifier fkIdentifier = Identifier.toIdentifier( resultSet.getString( "FK_NAME" ) );
453+
final Identifier fkIdentifier = identifierHelper().fromMetaDataObjectName(
454+
resultSet.getString( "FK_NAME" )
455+
);
438456
ForeignKeyBuilder fkBuilder = fkBuilders.get( fkIdentifier );
439457
if ( fkBuilder == null ) {
440458
fkBuilder = generateForeignKeyBuilder( fkIdentifier );
@@ -454,8 +472,12 @@ public Iterable<ForeignKeyInformation> getForeignKeys(TableInformation tableInfo
454472
continue;
455473
}
456474

457-
final Identifier fkColumnIdentifier = Identifier.toIdentifier( resultSet.getString( "FKCOLUMN_NAME" ) );
458-
final Identifier pkColumnIdentifier = Identifier.toIdentifier( resultSet.getString( "PKCOLUMN_NAME" ) );
475+
final Identifier fkColumnIdentifier = identifierHelper().fromMetaDataObjectName(
476+
resultSet.getString( "FKCOLUMN_NAME" )
477+
);
478+
final Identifier pkColumnIdentifier = identifierHelper().fromMetaDataObjectName(
479+
resultSet.getString( "PKCOLUMN_NAME" )
480+
);
459481

460482
fkBuilder.addColumnMapping(
461483
tableInformation.getColumn( fkColumnIdentifier ),
@@ -486,10 +508,10 @@ private ForeignKeyBuilder generateForeignKeyBuilder(Identifier fkIdentifier) {
486508
return new ForeignKeyBuilderImpl( fkIdentifier );
487509
}
488510

489-
protected static interface ForeignKeyBuilder {
490-
public ForeignKeyBuilder addColumnMapping(ColumnInformation referencing, ColumnInformation referenced);
511+
protected interface ForeignKeyBuilder {
512+
ForeignKeyBuilder addColumnMapping(ColumnInformation referencing, ColumnInformation referenced);
491513

492-
public ForeignKeyInformation build();
514+
ForeignKeyInformation build();
493515
}
494516

495517
protected static class ForeignKeyBuilderImpl implements ForeignKeyBuilder {
@@ -524,11 +546,9 @@ private QualifiedTableName extractKeyTableName(ResultSet resultSet, String prefi
524546
final String incomingTableName = resultSet.getString( prefix + "TABLE_NAME" );
525547

526548
return new QualifiedTableName(
527-
new Schema.Name(
528-
Identifier.toIdentifier( incomingCatalogName ),
529-
Identifier.toIdentifier( incomingSchemaName )
530-
),
531-
Identifier.toIdentifier( incomingTableName )
549+
identifierHelper().fromMetaDataCatalogName( incomingCatalogName ),
550+
identifierHelper().fromMetaDataSchemaName( incomingSchemaName ),
551+
identifierHelper().fromMetaDataObjectName( incomingTableName )
532552
);
533553
}
534554
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.test.schemaupdate;
25+
26+
import java.util.Set;
27+
import javax.persistence.Entity;
28+
import javax.persistence.GeneratedValue;
29+
import javax.persistence.Id;
30+
import javax.persistence.ManyToMany;
31+
import javax.persistence.Table;
32+
33+
import org.hibernate.annotations.GenericGenerator;
34+
import org.hibernate.boot.MetadataSources;
35+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
36+
import org.hibernate.boot.spi.MetadataImplementor;
37+
import org.hibernate.service.ServiceRegistry;
38+
import org.hibernate.tool.hbm2ddl.SchemaExport;
39+
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
40+
41+
import org.junit.After;
42+
import org.junit.Before;
43+
import org.junit.Test;
44+
45+
/**
46+
* @author Steve Ebersole
47+
*/
48+
public class TestFkUpdating {
49+
protected ServiceRegistry serviceRegistry;
50+
protected MetadataImplementor metadata;
51+
52+
@Before
53+
public void setUp() {
54+
serviceRegistry = new StandardServiceRegistryBuilder().build();
55+
metadata = (MetadataImplementor) new MetadataSources( serviceRegistry )
56+
.addAnnotatedClass( User.class )
57+
.addAnnotatedClass(Role.class)
58+
.buildMetadata();
59+
60+
System.out.println( "********* Starting SchemaExport for START-UP *************************" );
61+
SchemaExport schemaExport = new SchemaExport( serviceRegistry, metadata );
62+
schemaExport.create( true, true );
63+
System.out.println( "********* Completed SchemaExport for START-UP *************************" );
64+
}
65+
66+
@After
67+
public void tearDown() {
68+
System.out.println( "********* Starting SchemaExport (drop) for TEAR-DOWN *************************" );
69+
SchemaExport schemaExport = new SchemaExport( serviceRegistry, metadata );
70+
schemaExport.drop( true, true );
71+
System.out.println( "********* Completed SchemaExport (drop) for TEAR-DOWN *************************" );
72+
73+
74+
StandardServiceRegistryBuilder.destroy( serviceRegistry );
75+
serviceRegistry = null;
76+
}
77+
78+
@Test
79+
public void testUpdate() {
80+
System.out.println( "********* Starting SchemaUpdate for TEST *************************" );
81+
SchemaUpdate schemaUpdate = new SchemaUpdate( serviceRegistry, metadata );
82+
schemaUpdate.execute( true, true );
83+
System.out.println( "********* Completed SchemaUpdate for TEST *************************" );
84+
}
85+
86+
87+
@Entity( name = "User" )
88+
@Table( name = "user" )
89+
public static class User {
90+
private Integer id;
91+
private Set<Role> roles;
92+
93+
@Id
94+
@GeneratedValue(generator = "increment")
95+
@GenericGenerator(name = "increment",strategy = "increment")
96+
public Integer getId() {
97+
return id;
98+
}
99+
100+
public void setId(Integer id) {
101+
this.id = id;
102+
}
103+
104+
@ManyToMany
105+
public Set<Role> getRoles() {
106+
return roles;
107+
}
108+
109+
public void setRoles(Set<Role> roles) {
110+
this.roles = roles;
111+
}
112+
}
113+
114+
@Entity( name = "Role" )
115+
@Table( name = "role" )
116+
public class Role {
117+
private Integer id;
118+
119+
@Id
120+
@GeneratedValue(generator = "increment")
121+
@GenericGenerator(name = "increment",strategy = "increment")
122+
public Integer getId() {
123+
return id;
124+
}
125+
126+
public void setId(Integer id) {
127+
this.id = id;
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)