HHH-9327 : NamingStrategy regression (reverts HHH-9280)
(cherry picked from commit 30c6e060db
)
This commit is contained in:
parent
4505843f02
commit
c63bc19d8a
|
@ -1214,7 +1214,7 @@ public abstract class CollectionBinder {
|
||||||
if ( StringHelper.isEmpty( associationTableBinder.getName() ) ) {
|
if ( StringHelper.isEmpty( associationTableBinder.getName() ) ) {
|
||||||
//default value
|
//default value
|
||||||
associationTableBinder.setDefaultName(
|
associationTableBinder.setDefaultName(
|
||||||
collValue.getOwner().getJpaEntityName(),
|
collValue.getOwner().getEntityName(),
|
||||||
mappings.getLogicalTableName( collValue.getOwner().getTable() ),
|
mappings.getLogicalTableName( collValue.getOwner().getTable() ),
|
||||||
collectionEntity != null ? collectionEntity.getEntityName() : null,
|
collectionEntity != null ? collectionEntity.getEntityName() : null,
|
||||||
collectionEntity != null ? mappings.getLogicalTableName( collectionEntity.getTable() ) : null,
|
collectionEntity != null ? mappings.getLogicalTableName( collectionEntity.getTable() ) : null,
|
||||||
|
|
|
@ -38,6 +38,8 @@ import org.hibernate.mapping.Collection;
|
||||||
import org.hibernate.mapping.Column;
|
import org.hibernate.mapping.Column;
|
||||||
import org.hibernate.mapping.Table;
|
import org.hibernate.mapping.Table;
|
||||||
import org.hibernate.test.annotations.Country;
|
import org.hibernate.test.annotations.Country;
|
||||||
|
import org.hibernate.testing.FailureExpected;
|
||||||
|
import org.hibernate.testing.TestForIssue;
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
@ -263,6 +265,8 @@ public class CollectionElementTest extends BaseCoreFunctionalTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@TestForIssue( jiraKey = "HHH-9387")
|
||||||
|
@FailureExpected( jiraKey = "HHH-9387")
|
||||||
public void testDefaultTableNameUsesJpaEntityName() {
|
public void testDefaultTableNameUsesJpaEntityName() {
|
||||||
final Collection collection = configuration().getCollectionMapping( Matrix.class.getName() + "." + "mvalues" );
|
final Collection collection = configuration().getCollectionMapping( Matrix.class.getName() + "." + "mvalues" );
|
||||||
final Table table = collection.getCollectionTable();
|
final Table table = collection.getCollectionTable();
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* Hibernate, Relational Persistence for Idiomatic Java
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
*
|
*
|
||||||
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
|
* Copyright (c) 2014, Red Hat Inc. or third-party contributors as
|
||||||
* indicated by the @author tags or express copyright attribution
|
* indicated by the @author tags or express copyright attribution
|
||||||
* statements applied by the authors. All third-party contributions are
|
* statements applied by the authors. All third-party contributions are
|
||||||
* distributed under license by Red Hat Inc.
|
* distributed under license by Red Hat Inc.
|
||||||
|
@ -21,27 +21,52 @@
|
||||||
* 51 Franklin Street, Fifth Floor
|
* 51 Franklin Street, Fifth Floor
|
||||||
* Boston, MA 02110-1301 USA
|
* Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.namingstrategy.regression;
|
package org.hibernate.test.namingstrategy;
|
||||||
|
|
||||||
import org.hibernate.cfg.EJB3NamingStrategy;
|
import java.io.Serializable;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class MyNamingStrategy extends EJB3NamingStrategy {
|
import javax.persistence.Basic;
|
||||||
|
import javax.persistence.CollectionTable;
|
||||||
|
import javax.persistence.ElementCollection;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
|
import javax.persistence.ManyToMany;
|
||||||
|
import javax.persistence.MapKeyColumn;
|
||||||
|
|
||||||
private static final long serialVersionUID = -5713413771290957530L;
|
import org.hibernate.test.namingstrategy.Item;
|
||||||
|
|
||||||
@Override
|
@Entity
|
||||||
public String collectionTableName(String ownerEntity, String ownerEntityTable, String associatedEntity, String associatedEntityTable, String propertyName) {
|
public class Category {
|
||||||
String name = null;
|
|
||||||
|
|
||||||
if ( "localized".equals( propertyName ) ) {
|
private Long id;
|
||||||
// NOTE: The problem is that ownerEntity is not the fully qualified class name since 4.2.15 anymore
|
private Set<Item> items = new HashSet<Item>();
|
||||||
name = ownerEntity.replaceAll( "\\.", "_" ).toUpperCase() + "_LOCALIZED";
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( name == null ) {
|
public Category() {
|
||||||
name = super.collectionTableName( ownerEntity, ownerEntityTable, associatedEntity, associatedEntityTable, propertyName );
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return name;
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ManyToMany
|
||||||
|
public Set<Item> getItems() {
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setItems(Set<Item> items) {
|
||||||
|
this.items = items;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,140 @@
|
||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2014, Red Hat Inc. or third-party contributors as
|
||||||
|
* indicated by the @author tags or express copyright attribution
|
||||||
|
* statements applied by the authors. All third-party contributions are
|
||||||
|
* distributed under license by Red Hat Inc.
|
||||||
|
*
|
||||||
|
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||||
|
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||||
|
* Lesser General Public License, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||||
|
* for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this distribution; if not, write to:
|
||||||
|
* Free Software Foundation, Inc.
|
||||||
|
* 51 Franklin Street, Fifth Floor
|
||||||
|
* Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
package org.hibernate.test.namingstrategy;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.hibernate.cfg.Configuration;
|
||||||
|
import org.hibernate.cfg.EJB3NamingStrategy;
|
||||||
|
import org.hibernate.internal.util.StringHelper;
|
||||||
|
import org.hibernate.mapping.Collection;
|
||||||
|
import org.hibernate.mapping.ForeignKey;
|
||||||
|
import org.hibernate.mapping.PersistentClass;
|
||||||
|
import org.hibernate.testing.FailureExpected;
|
||||||
|
import org.hibernate.testing.TestForIssue;
|
||||||
|
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class FullyQualifiedEntityNameNamingStrategyTest extends BaseCoreFunctionalTestCase {
|
||||||
|
@Override
|
||||||
|
public void configure(Configuration cfg) {
|
||||||
|
super.configure( cfg );
|
||||||
|
cfg.setNamingStrategy( new MyNamingStrategy() );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Class<?>[] getAnnotatedClasses() {
|
||||||
|
return new Class<?>[] { Category.class, Item.class, Workflow.class };
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "HHH-4312")
|
||||||
|
@FailureExpected(jiraKey = "HHH-4312")
|
||||||
|
public void testEntityTable() throws Exception {
|
||||||
|
final PersistentClass classMapping = configuration().getClassMapping( Workflow.class.getName() );
|
||||||
|
final String expectedTableName = transformEntityName( Workflow.class.getName() );
|
||||||
|
assertEquals( expectedTableName, classMapping.getTable().getName() );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "HHH-9327")
|
||||||
|
public void testElementCollectionTable() {
|
||||||
|
final Collection collectionMapping = configuration().getCollectionMapping(
|
||||||
|
Workflow.class.getName() + ".localized"
|
||||||
|
);
|
||||||
|
final String expectedTableName = transformEntityName( Workflow.class.getName() ) + "_localized";
|
||||||
|
assertEquals( expectedTableName, collectionMapping.getCollectionTable().getName() );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "HHH-9327")
|
||||||
|
public void testManyToManyCollectionTable() {
|
||||||
|
final Collection collectionMapping = configuration().getCollectionMapping(
|
||||||
|
Category.class.getName() + "." + "items"
|
||||||
|
);
|
||||||
|
final String expectedTableName = transformEntityName( Category.class.getName() ) + "_" + transformEntityName( Item.class.getName() );
|
||||||
|
assertEquals( expectedTableName, collectionMapping.getCollectionTable().getName() );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue( jiraKey = "HHH-9327")
|
||||||
|
public void testManyToManyForeignKeys() {
|
||||||
|
final Collection ownerCollectionMapping = configuration().getCollectionMapping(
|
||||||
|
Category.class.getName() + "." + "items"
|
||||||
|
);
|
||||||
|
final String expectedOwnerFK = transformEntityName( Category.class.getName() ) + "_id";
|
||||||
|
final String expectedInverseFK = transformEntityName( Item.class.getName() ) + "_items_id";
|
||||||
|
|
||||||
|
boolean ownerFKFound = false;
|
||||||
|
boolean inverseFKFound = false;
|
||||||
|
for ( Iterator it = ownerCollectionMapping.getCollectionTable().getForeignKeyIterator(); it.hasNext(); ) {
|
||||||
|
final String fkColumnName = ( (ForeignKey) it.next() ).getColumn( 0 ).getName();
|
||||||
|
if ( expectedOwnerFK.equals( fkColumnName ) ) {
|
||||||
|
ownerFKFound = true;
|
||||||
|
}
|
||||||
|
else if ( expectedInverseFK.equals( fkColumnName ) ) {
|
||||||
|
inverseFKFound = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertTrue( ownerFKFound );
|
||||||
|
assertTrue( inverseFKFound );
|
||||||
|
}
|
||||||
|
|
||||||
|
static String transformEntityName(String entityName) {
|
||||||
|
return entityName.replaceAll( "\\.", "_" );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class MyNamingStrategy extends EJB3NamingStrategy {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -5713413771290957530L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String classToTableName(String className) {
|
||||||
|
return transformEntityName( className );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String collectionTableName(String ownerEntity, String ownerEntityTable, String associatedEntity, String associatedEntityTable, String propertyName) {
|
||||||
|
return transformEntityName( ownerEntity ) + "_" +
|
||||||
|
( associatedEntityTable != null ?
|
||||||
|
transformEntityName( associatedEntity ) :
|
||||||
|
StringHelper.unqualify( propertyName )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String foreignKeyColumnName(String propertyName, String propertyEntityName, String propertyTableName, String referencedColumnName) {
|
||||||
|
if ( propertyName == null ) {
|
||||||
|
return columnName( transformEntityName( propertyEntityName ) + "_" + referencedColumnName );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return columnName( transformEntityName( propertyEntityName ) + "_" + propertyName + "_" + referencedColumnName );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,14 +21,14 @@
|
||||||
* 51 Franklin Street, Fifth Floor
|
* 51 Franklin Street, Fifth Floor
|
||||||
* Boston, MA 02110-1301 USA
|
* Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.namingstrategy.regression;
|
package org.hibernate.test.namingstrategy;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Embeddable;
|
import javax.persistence.Embeddable;
|
||||||
|
|
||||||
@Embeddable
|
@Embeddable
|
||||||
public class LocalizedEntity implements Serializable, Cloneable {
|
public class LocalizedEmbeddable implements Serializable, Cloneable {
|
||||||
|
|
||||||
private static final long serialVersionUID = -8539302606114365372L;
|
private static final long serialVersionUID = -8539302606114365372L;
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ public class LocalizedEntity implements Serializable, Cloneable {
|
||||||
/**
|
/**
|
||||||
* Empty Constructor
|
* Empty Constructor
|
||||||
*/
|
*/
|
||||||
public LocalizedEntity() {
|
public LocalizedEmbeddable() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ public class LocalizedEntity implements Serializable, Cloneable {
|
||||||
* @param name
|
* @param name
|
||||||
* @param description
|
* @param description
|
||||||
*/
|
*/
|
||||||
public LocalizedEntity(String name, String description) {
|
public LocalizedEmbeddable(String name, String description) {
|
||||||
super();
|
super();
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
|
@ -71,8 +71,8 @@ public class LocalizedEntity implements Serializable, Cloneable {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LocalizedEntity clone() {
|
public LocalizedEmbeddable clone() {
|
||||||
return new LocalizedEntity( this.name, this.description );
|
return new LocalizedEmbeddable( this.name, this.description );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -92,7 +92,7 @@ public class LocalizedEntity implements Serializable, Cloneable {
|
||||||
return false;
|
return false;
|
||||||
if ( getClass() != obj.getClass() )
|
if ( getClass() != obj.getClass() )
|
||||||
return false;
|
return false;
|
||||||
LocalizedEntity other = (LocalizedEntity) obj;
|
LocalizedEmbeddable other = (LocalizedEmbeddable) obj;
|
||||||
if ( description == null ) {
|
if ( description == null ) {
|
||||||
if ( other.description != null )
|
if ( other.description != null )
|
||||||
return false;
|
return false;
|
|
@ -21,7 +21,7 @@
|
||||||
* 51 Franklin Street, Fifth Floor
|
* 51 Franklin Street, Fifth Floor
|
||||||
* Boston, MA 02110-1301 USA
|
* Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.namingstrategy.regression;
|
package org.hibernate.test.namingstrategy;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -47,7 +47,7 @@ public class Workflow implements Serializable {
|
||||||
private Long id;
|
private Long id;
|
||||||
private Locale defaultLanguage;
|
private Locale defaultLanguage;
|
||||||
private Set<Locale> supportedLocales = new HashSet<Locale>();
|
private Set<Locale> supportedLocales = new HashSet<Locale>();
|
||||||
private Map<Locale, LocalizedEntity> localized = new HashMap<Locale, LocalizedEntity>();
|
private Map<Locale, LocalizedEmbeddable> localized = new HashMap<Locale, LocalizedEmbeddable>();
|
||||||
|
|
||||||
public Workflow() {
|
public Workflow() {
|
||||||
}
|
}
|
||||||
|
@ -84,11 +84,11 @@ public class Workflow implements Serializable {
|
||||||
@ElementCollection
|
@ElementCollection
|
||||||
@CollectionTable(joinColumns = { @JoinColumn(name = "ID", referencedColumnName = "ID", nullable = false, insertable = false, updatable = false) })
|
@CollectionTable(joinColumns = { @JoinColumn(name = "ID", referencedColumnName = "ID", nullable = false, insertable = false, updatable = false) })
|
||||||
@MapKeyColumn(name = "LANGUAGE_CODE", nullable = false, insertable = false, updatable = false)
|
@MapKeyColumn(name = "LANGUAGE_CODE", nullable = false, insertable = false, updatable = false)
|
||||||
public Map<Locale, LocalizedEntity> getLocalized() {
|
public Map<Locale, LocalizedEmbeddable> getLocalized() {
|
||||||
return localized;
|
return localized;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLocalized(Map<Locale, LocalizedEntity> localized) {
|
public void setLocalized(Map<Locale, LocalizedEmbeddable> localized) {
|
||||||
this.localized = localized;
|
this.localized = localized;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,43 +0,0 @@
|
||||||
/*
|
|
||||||
* To change this license header, choose License Headers in Project Properties.
|
|
||||||
* To change this template file, choose Tools | Templates
|
|
||||||
* and open the template in the editor.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.hibernate.test.namingstrategy.regression;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import org.hibernate.cfg.Configuration;
|
|
||||||
import org.hibernate.mapping.Map;
|
|
||||||
import org.hibernate.mapping.PersistentClass;
|
|
||||||
import org.hibernate.mapping.Property;
|
|
||||||
import org.hibernate.testing.TestForIssue;
|
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class NamingStrategyTest extends BaseCoreFunctionalTestCase {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void configure(Configuration cfg) {
|
|
||||||
super.configure( cfg );
|
|
||||||
cfg.setNamingStrategy( new MyNamingStrategy() );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Class<?>[] getAnnotatedClasses() {
|
|
||||||
return new Class<?>[] { Workflow.class };
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@TestForIssue(jiraKey = "HHH-9327")
|
|
||||||
public void testNamingStrategyRegression() throws Exception {
|
|
||||||
String expectedName = Workflow.class.getName().replaceAll( "\\.", "_" ).toUpperCase();
|
|
||||||
expectedName += "_LOCALIZED";
|
|
||||||
|
|
||||||
PersistentClass classMapping = configuration().getClassMapping( Workflow.class.getName() );
|
|
||||||
Property property = classMapping.getProperty( "localized" );
|
|
||||||
Map map = (Map) property.getValue();
|
|
||||||
assertEquals( expectedName, map.getCollectionTable().getName() );
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue