HHH-9327 : NamingStrategy regression (reverts HHH-9280)

(cherry picked from commit 30c6e060db)
This commit is contained in:
Gail Badner 2014-09-10 12:57:55 -07:00
parent 4505843f02
commit c63bc19d8a
7 changed files with 197 additions and 71 deletions

View File

@ -1214,7 +1214,7 @@ public abstract class CollectionBinder {
if ( StringHelper.isEmpty( associationTableBinder.getName() ) ) {
//default value
associationTableBinder.setDefaultName(
collValue.getOwner().getJpaEntityName(),
collValue.getOwner().getEntityName(),
mappings.getLogicalTableName( collValue.getOwner().getTable() ),
collectionEntity != null ? collectionEntity.getEntityName() : null,
collectionEntity != null ? mappings.getLogicalTableName( collectionEntity.getTable() ) : null,

View File

@ -38,6 +38,8 @@ import org.hibernate.mapping.Collection;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Table;
import org.hibernate.test.annotations.Country;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
@ -263,6 +265,8 @@ public class CollectionElementTest extends BaseCoreFunctionalTestCase {
}
@Test
@TestForIssue( jiraKey = "HHH-9387")
@FailureExpected( jiraKey = "HHH-9387")
public void testDefaultTableNameUsesJpaEntityName() {
final Collection collection = configuration().getCollectionMapping( Matrix.class.getName() + "." + "mvalues" );
final Table table = collection.getCollectionTable();

View File

@ -1,7 +1,7 @@
/*
* 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
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
@ -21,27 +21,52 @@
* 51 Franklin Street, Fifth Floor
* 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
public String collectionTableName(String ownerEntity, String ownerEntityTable, String associatedEntity, String associatedEntityTable, String propertyName) {
String name = null;
@Entity
public class Category {
if ( "localized".equals( propertyName ) ) {
// NOTE: The problem is that ownerEntity is not the fully qualified class name since 4.2.15 anymore
name = ownerEntity.replaceAll( "\\.", "_" ).toUpperCase() + "_LOCALIZED";
}
private Long id;
private Set<Item> items = new HashSet<Item>();
if ( name == null ) {
name = super.collectionTableName( ownerEntity, ownerEntityTable, associatedEntity, associatedEntityTable, propertyName );
}
public Category() {
}
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;
}
}

View File

@ -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 );
}
}
}
}

View File

@ -21,14 +21,14 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.namingstrategy.regression;
package org.hibernate.test.namingstrategy;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class LocalizedEntity implements Serializable, Cloneable {
public class LocalizedEmbeddable implements Serializable, Cloneable {
private static final long serialVersionUID = -8539302606114365372L;
@ -38,7 +38,7 @@ public class LocalizedEntity implements Serializable, Cloneable {
/**
* Empty Constructor
*/
public LocalizedEntity() {
public LocalizedEmbeddable() {
super();
}
@ -46,7 +46,7 @@ public class LocalizedEntity implements Serializable, Cloneable {
* @param name
* @param description
*/
public LocalizedEntity(String name, String description) {
public LocalizedEmbeddable(String name, String description) {
super();
this.name = name;
this.description = description;
@ -71,8 +71,8 @@ public class LocalizedEntity implements Serializable, Cloneable {
}
@Override
public LocalizedEntity clone() {
return new LocalizedEntity( this.name, this.description );
public LocalizedEmbeddable clone() {
return new LocalizedEmbeddable( this.name, this.description );
}
@Override
@ -92,7 +92,7 @@ public class LocalizedEntity implements Serializable, Cloneable {
return false;
if ( getClass() != obj.getClass() )
return false;
LocalizedEntity other = (LocalizedEntity) obj;
LocalizedEmbeddable other = (LocalizedEmbeddable) obj;
if ( description == null ) {
if ( other.description != null )
return false;

View File

@ -21,7 +21,7 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.namingstrategy.regression;
package org.hibernate.test.namingstrategy;
import java.io.Serializable;
import java.util.HashMap;
@ -47,7 +47,7 @@ public class Workflow implements Serializable {
private Long id;
private Locale defaultLanguage;
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() {
}
@ -84,11 +84,11 @@ public class Workflow implements Serializable {
@ElementCollection
@CollectionTable(joinColumns = { @JoinColumn(name = "ID", referencedColumnName = "ID", 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;
}
public void setLocalized(Map<Locale, LocalizedEntity> localized) {
public void setLocalized(Map<Locale, LocalizedEmbeddable> localized) {
this.localized = localized;
}

View File

@ -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() );
}
}