HHH-14230 Fix generics to avoid ClassCastException
This commit is contained in:
parent
9934baf90e
commit
23fdfa4d6a
|
@ -39,7 +39,7 @@ import org.jboss.logging.Logger;
|
|||
*
|
||||
* @author Gavin King
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings("deprecation")
|
||||
public class Table implements RelationalModel, Serializable, Exportable {
|
||||
private static final Logger log = Logger.getLogger( Table.class );
|
||||
|
||||
|
@ -50,14 +50,14 @@ public class Table implements RelationalModel, Serializable, Exportable {
|
|||
/**
|
||||
* contains all columns, including the primary key
|
||||
*/
|
||||
private Map columns = new LinkedHashMap();
|
||||
private Map<String, Column> columns = new LinkedHashMap<>();
|
||||
private KeyValue idValue;
|
||||
private PrimaryKey primaryKey;
|
||||
private Map<ForeignKeyKey, ForeignKey> foreignKeys = new LinkedHashMap<ForeignKeyKey, ForeignKey>();
|
||||
private Map<String, Index> indexes = new LinkedHashMap<String, Index>();
|
||||
private Map<String,UniqueKey> uniqueKeys = new LinkedHashMap<String,UniqueKey>();
|
||||
private Map<ForeignKeyKey, ForeignKey> foreignKeys = new LinkedHashMap<>();
|
||||
private Map<String, Index> indexes = new LinkedHashMap<>();
|
||||
private Map<String,UniqueKey> uniqueKeys = new LinkedHashMap<>();
|
||||
private int uniqueInteger;
|
||||
private List<String> checkConstraints = new ArrayList<String>();
|
||||
private List<String> checkConstraints = new ArrayList<>();
|
||||
private String rowId;
|
||||
private String subselect;
|
||||
private boolean isAbstract;
|
||||
|
@ -246,7 +246,7 @@ public class Table implements RelationalModel, Serializable, Exportable {
|
|||
}
|
||||
|
||||
public Column getColumn(int n) {
|
||||
Iterator iter = columns.values().iterator();
|
||||
Iterator<Column> iter = columns.values().iterator();
|
||||
for ( int i = 0; i < n - 1; i++ ) {
|
||||
iter.next();
|
||||
}
|
||||
|
@ -280,7 +280,7 @@ public class Table implements RelationalModel, Serializable, Exportable {
|
|||
return columns.size();
|
||||
}
|
||||
|
||||
public Iterator getColumnIterator() {
|
||||
public Iterator<Column> getColumnIterator() {
|
||||
return columns.values().iterator();
|
||||
}
|
||||
|
||||
|
@ -288,7 +288,7 @@ public class Table implements RelationalModel, Serializable, Exportable {
|
|||
return indexes.values().iterator();
|
||||
}
|
||||
|
||||
public Iterator getForeignKeyIterator() {
|
||||
public Iterator<ForeignKey> getForeignKeyIterator() {
|
||||
return foreignKeys.values().iterator();
|
||||
}
|
||||
|
||||
|
@ -410,7 +410,7 @@ public class Table implements RelationalModel, Serializable, Exportable {
|
|||
}
|
||||
|
||||
public void validateColumns(Dialect dialect, Mapping mapping, TableMetadata tableInfo) {
|
||||
Iterator iter = getColumnIterator();
|
||||
Iterator<Column> iter = getColumnIterator();
|
||||
while ( iter.hasNext() ) {
|
||||
Column col = (Column) iter.next();
|
||||
|
||||
|
@ -438,7 +438,7 @@ public class Table implements RelationalModel, Serializable, Exportable {
|
|||
|
||||
}
|
||||
|
||||
public Iterator sqlAlterStrings(
|
||||
public Iterator<String> sqlAlterStrings(
|
||||
Dialect dialect,
|
||||
Metadata metadata,
|
||||
TableInformation tableInfo,
|
||||
|
@ -460,8 +460,8 @@ public class Table implements RelationalModel, Serializable, Exportable {
|
|||
.append( ' ' )
|
||||
.append( dialect.getAddColumnString() );
|
||||
|
||||
Iterator iter = getColumnIterator();
|
||||
List results = new ArrayList();
|
||||
Iterator<Column> iter = getColumnIterator();
|
||||
List<String> results = new ArrayList<>();
|
||||
|
||||
while ( iter.hasNext() ) {
|
||||
final Column column = (Column) iter.next();
|
||||
|
@ -538,7 +538,7 @@ public class Table implements RelationalModel, Serializable, Exportable {
|
|||
pkname = ( (Column) getPrimaryKey().getColumnIterator().next() ).getQuotedName( dialect );
|
||||
}
|
||||
|
||||
Iterator iter = getColumnIterator();
|
||||
Iterator<Column> iter = getColumnIterator();
|
||||
while ( iter.hasNext() ) {
|
||||
Column col = (Column) iter.next();
|
||||
|
||||
|
@ -667,7 +667,7 @@ public class Table implements RelationalModel, Serializable, Exportable {
|
|||
return uniqueKey;
|
||||
}
|
||||
|
||||
public UniqueKey createUniqueKey(List keyColumns) {
|
||||
public UniqueKey createUniqueKey(List<Column> keyColumns) {
|
||||
String keyName = Constraint.generateName( "UK_", this, keyColumns );
|
||||
UniqueKey uk = getOrCreateUniqueKey( keyName );
|
||||
uk.addColumns( keyColumns.iterator() );
|
||||
|
@ -693,16 +693,16 @@ public class Table implements RelationalModel, Serializable, Exportable {
|
|||
public void createForeignKeys() {
|
||||
}
|
||||
|
||||
public ForeignKey createForeignKey(String keyName, List keyColumns, String referencedEntityName, String keyDefinition) {
|
||||
public ForeignKey createForeignKey(String keyName, List<Column> keyColumns, String referencedEntityName, String keyDefinition) {
|
||||
return createForeignKey( keyName, keyColumns, referencedEntityName, keyDefinition, null );
|
||||
}
|
||||
|
||||
public ForeignKey createForeignKey(
|
||||
String keyName,
|
||||
List keyColumns,
|
||||
List<Column> keyColumns,
|
||||
String referencedEntityName,
|
||||
String keyDefinition,
|
||||
List referencedColumns) {
|
||||
List<Column> referencedColumns) {
|
||||
final ForeignKeyKey key = new ForeignKeyKey( keyColumns, referencedEntityName, referencedColumns );
|
||||
|
||||
ForeignKey fk = foreignKeys.get( key );
|
||||
|
@ -826,14 +826,14 @@ public class Table implements RelationalModel, Serializable, Exportable {
|
|||
return checkConstraints.iterator();
|
||||
}
|
||||
|
||||
public Iterator sqlCommentStrings(Dialect dialect, String defaultCatalog, String defaultSchema) {
|
||||
List comments = new ArrayList();
|
||||
public Iterator<String> sqlCommentStrings(Dialect dialect, String defaultCatalog, String defaultSchema) {
|
||||
List<String> comments = new ArrayList<>();
|
||||
if ( dialect.supportsCommentOn() ) {
|
||||
String tableName = getQualifiedName( dialect, defaultCatalog, defaultSchema );
|
||||
if ( comment != null ) {
|
||||
comments.add( "comment on table " + tableName + " is '" + comment + "'" );
|
||||
}
|
||||
Iterator iter = getColumnIterator();
|
||||
Iterator<Column> iter = getColumnIterator();
|
||||
while ( iter.hasNext() ) {
|
||||
Column column = (Column) iter.next();
|
||||
String columnComment = column.getComment();
|
||||
|
@ -861,19 +861,19 @@ public class Table implements RelationalModel, Serializable, Exportable {
|
|||
|
||||
public static class ForeignKeyKey implements Serializable {
|
||||
String referencedClassName;
|
||||
List columns;
|
||||
List referencedColumns;
|
||||
List<Column> columns;
|
||||
List<Column> referencedColumns;
|
||||
|
||||
ForeignKeyKey(List columns, String referencedClassName, List referencedColumns) {
|
||||
ForeignKeyKey(List<Column> columns, String referencedClassName, List<Column> referencedColumns) {
|
||||
this.referencedClassName = referencedClassName;
|
||||
this.columns = new ArrayList();
|
||||
this.columns = new ArrayList<>();
|
||||
this.columns.addAll( columns );
|
||||
if ( referencedColumns != null ) {
|
||||
this.referencedColumns = new ArrayList();
|
||||
this.referencedColumns = new ArrayList<>();
|
||||
this.referencedColumns.addAll( referencedColumns );
|
||||
}
|
||||
else {
|
||||
this.referencedColumns = Collections.EMPTY_LIST;
|
||||
this.referencedColumns = Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -889,16 +889,16 @@ public class Table implements RelationalModel, Serializable, Exportable {
|
|||
@Override
|
||||
public String toString() {
|
||||
return "ForeignKeyKey{" +
|
||||
"columns=" + String.join( ",", columns ) +
|
||||
"columns=" + columns +
|
||||
", referencedClassName='" + referencedClassName + '\'' +
|
||||
", referencedColumns=" + String.join( ",", referencedColumns ) +
|
||||
", referencedColumns=" + referencedColumns +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
public void addInitCommand(InitCommand command) {
|
||||
if ( initCommands == null ) {
|
||||
initCommands = new ArrayList<InitCommand>();
|
||||
initCommands = new ArrayList<>();
|
||||
}
|
||||
initCommands.add( command );
|
||||
}
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.foreignkeys;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.mapping.Table;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Yanming Zhou
|
||||
*/
|
||||
@TestForIssue(jiraKey = "HHH-14230")
|
||||
public class HHH14230 {
|
||||
|
||||
private static final String TABLE_NAME = "test_entity";
|
||||
private static final String JOIN_COLUMN_NAME = "parent";
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Metadata metadata = new MetadataSources(new StandardServiceRegistryBuilder().build())
|
||||
.addAnnotatedClass(TestEntity.class).buildMetadata();
|
||||
Table table = StreamSupport.stream(metadata.getDatabase().getNamespaces().spliterator(), false)
|
||||
.flatMap(namespace -> namespace.getTables().stream())
|
||||
.filter(t -> t.getName().equals(TABLE_NAME)).findFirst().orElse(null);
|
||||
assertNotNull(table);
|
||||
assertEquals(1, table.getForeignKeys().size());
|
||||
|
||||
// ClassCastException before HHH-14230
|
||||
assertTrue(table.getForeignKeys().keySet().iterator().next().toString().contains(JOIN_COLUMN_NAME));
|
||||
}
|
||||
|
||||
@Entity
|
||||
@javax.persistence.Table(name = TABLE_NAME)
|
||||
public static class TestEntity {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = JOIN_COLUMN_NAME)
|
||||
private TestEntity parent;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public TestEntity getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(TestEntity parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue