Skip to content

Commit 14fc885

Browse files
committed
HHH-9745 - ClassCastException in hbm2ddl update and validate
1 parent 6d0549b commit 14fc885

File tree

3 files changed

+111
-8
lines changed

3 files changed

+111
-8
lines changed

hibernate-core/src/main/java/org/hibernate/engine/jdbc/dialect/internal/DialectFactoryImpl.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,42 @@ public void setDialectResolver(DialectResolver dialectResolver) {
4646

4747
@Override
4848
public Dialect buildDialect(Map configValues, DialectResolutionInfoSource resolutionInfoSource) throws HibernateException {
49-
final String dialectName = (String) configValues.get( AvailableSettings.DIALECT );
50-
if ( !StringHelper.isEmpty( dialectName ) ) {
51-
return constructDialect( dialectName );
49+
final Object dialectReference = configValues.get( AvailableSettings.DIALECT );
50+
if ( !isEmpty( dialectReference ) ) {
51+
return constructDialect( dialectReference );
5252
}
5353
else {
5454
return determineDialect( resolutionInfoSource );
5555
}
5656
}
5757

58-
private Dialect constructDialect(String dialectName) {
58+
@SuppressWarnings("SimplifiableIfStatement")
59+
private boolean isEmpty(Object dialectReference) {
60+
if ( dialectReference != null ) {
61+
// the referenced value is not null
62+
if ( dialectReference instanceof String ) {
63+
// if it is a String, it might still be empty though...
64+
return StringHelper.isEmpty( (String) dialectReference );
65+
}
66+
return false;
67+
}
68+
return true;
69+
}
70+
71+
private Dialect constructDialect(Object dialectReference) {
5972
final Dialect dialect;
6073
try {
61-
dialect = strategySelector.resolveStrategy( Dialect.class, dialectName );
74+
dialect = strategySelector.resolveStrategy( Dialect.class, dialectReference );
6275
if ( dialect == null ) {
63-
throw new HibernateException( "Unable to construct requested dialect [" + dialectName+ "]" );
76+
throw new HibernateException( "Unable to construct requested dialect [" + dialectReference + "]" );
6477
}
6578
return dialect;
6679
}
6780
catch (HibernateException e) {
6881
throw e;
6982
}
7083
catch (Exception e) {
71-
throw new HibernateException( "Unable to construct requested dialect [" + dialectName+ "]", e );
84+
throw new HibernateException( "Unable to construct requested dialect [" + dialectReference + "]", e );
7285
}
7386
}
7487

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ public class SequenceInformationExtractorNoOpImpl implements SequenceInformation
2525
@Override
2626
@SuppressWarnings("unchecked")
2727
public Iterable<SequenceInformation> extractMetadata(ExtractionContext extractionContext) throws SQLException {
28-
return (Iterable<SequenceInformation>) Collections.emptyList().iterator();
28+
return Collections.emptyList();
2929
}
3030
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.schemaupdate;
8+
9+
import javax.persistence.Entity;
10+
import javax.persistence.Id;
11+
import javax.persistence.Table;
12+
13+
import org.hibernate.boot.MetadataSources;
14+
import org.hibernate.boot.registry.StandardServiceRegistry;
15+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
16+
import org.hibernate.boot.spi.MetadataImplementor;
17+
import org.hibernate.cfg.AvailableSettings;
18+
import org.hibernate.dialect.H2Dialect;
19+
import org.hibernate.tool.hbm2ddl.SchemaExport;
20+
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
21+
import org.hibernate.tool.hbm2ddl.Target;
22+
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorNoOpImpl;
23+
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;
24+
25+
import org.hibernate.testing.RequiresDialect;
26+
import org.hibernate.testing.TestForIssue;
27+
import org.junit.Test;
28+
29+
/**
30+
* Regression test fr a bug in org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorNoOpImpl
31+
*
32+
* @author Steve Ebersole
33+
*
34+
* @see
35+
*/
36+
@TestForIssue( jiraKey = "HHH-9745" )
37+
public class SequenceReadingTest {
38+
39+
@Test
40+
public void testSequenceReading() {
41+
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder()
42+
.applySetting( AvailableSettings.DIALECT, MyExtendedH2Dialect.class )
43+
.build();
44+
try {
45+
final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr )
46+
.addAnnotatedClass( MyEntity.class )
47+
.buildMetadata();
48+
metadata.validate();
49+
50+
try {
51+
// try to update the schema
52+
new SchemaUpdate( metadata ).execute( Target.EXPORT );
53+
}
54+
finally {
55+
try {
56+
// clean up
57+
new SchemaExport( metadata ).drop( Target.EXPORT );
58+
}
59+
catch (Exception ignore) {
60+
}
61+
}
62+
}
63+
finally {
64+
StandardServiceRegistryBuilder.destroy( ssr );
65+
}
66+
}
67+
68+
/**
69+
* An integral piece of the bug is Dialects which to not support sequence, so lets trick the test
70+
*/
71+
public static class MyExtendedH2Dialect extends H2Dialect {
72+
@Override
73+
public SequenceInformationExtractor getSequenceInformationExtractor() {
74+
return SequenceInformationExtractorNoOpImpl.INSTANCE;
75+
}
76+
77+
@Override
78+
public String getQuerySequencesString() {
79+
return null;
80+
}
81+
}
82+
83+
@Entity(name = "MyEntity")
84+
@Table(name = "my_entity")
85+
public static class MyEntity {
86+
@Id
87+
public Integer id;
88+
}
89+
90+
}

0 commit comments

Comments
 (0)