HHH-8862 bind @CollectionTable @ForeignKey explicit name

This commit is contained in:
Brett Meyer 2014-02-14 11:02:44 -05:00
parent 357fd11f2f
commit b5553a2485
4 changed files with 24 additions and 14 deletions

View File

@ -25,6 +25,7 @@ package org.hibernate.metamodel.internal;
import java.beans.BeanInfo;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.Iterator;
import java.util.List;
@ -71,7 +72,6 @@ import org.hibernate.type.CompositeType;
import org.hibernate.type.EntityType;
import org.hibernate.type.Type;
import org.hibernate.type.TypeFactory;
import org.jboss.logging.Logger;
/**
@ -858,8 +858,9 @@ class HibernateTypeHelper {
java.lang.reflect.Type collectionAttributeType = null;
if ( beanInfo.getPropertyDescriptors() == null || beanInfo.getPropertyDescriptors().length == 0 ) {
// we need to look for the field and look at it...
//TODO this only works when the field is public accessable or NoSuchElementException will be thrown
collectionAttributeType = ownerClass.getField( attributeName ).getGenericType();
Field field = ownerClass.getDeclaredField( attributeName );
field.setAccessible( true );
collectionAttributeType = field.getGenericType();
}
else {
for ( PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors() ) {

View File

@ -215,10 +215,13 @@ public class PluralAssociationAttribute extends AssociationAttribute {
final ClassLoaderService cls = context.getServiceRegistry().getService( ClassLoaderService.class );
// TODO: This is UGLY -- pull into a util and find a better pattern?
AnnotationInstance foreignKey = JandexHelper.getSingleAnnotation(
annotations(), HibernateDotNames.FOREIGN_KEY );
AnnotationInstance joinTable = JandexHelper.getSingleAnnotation(
annotations(), JPADotNames.JOIN_TABLE );
AnnotationInstance collectionTable = JandexHelper.getSingleAnnotation(
annotations(), JPADotNames.COLLECTION_TABLE );
if (foreignKey != null) {
explicitForeignKeyName = JandexHelper.getValue( foreignKey, "name", String.class, cls );
String temp = JandexHelper.getValue( foreignKey, "inverseName", String.class, cls );
@ -230,6 +233,12 @@ public class PluralAssociationAttribute extends AssociationAttribute {
explicitForeignKeyName = jpaFkAnnotation != null
? JandexHelper.getValue( jpaFkAnnotation, "name", String.class, cls ) : null;
}
if (collectionTable != null && StringHelper.isEmpty( explicitForeignKeyName )) {
final AnnotationInstance jpaFkAnnotation = JandexHelper.getValue(
collectionTable, "foreignKey", AnnotationInstance.class, cls );
explicitForeignKeyName = jpaFkAnnotation != null
? JandexHelper.getValue( jpaFkAnnotation, "name", String.class, cls ) : null;
}
if (joinTable != null && StringHelper.isEmpty( inverseForeignKeyName )) {
final AnnotationInstance jpaFkAnnotation = JandexHelper.getValue(
joinTable, "inverseForeignKey", AnnotationInstance.class, cls );

View File

@ -22,6 +22,7 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.collection.map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@ -45,7 +46,6 @@ import javax.persistence.OneToMany;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.collection.internal.PersistentMap;
import org.hibernate.testing.FailureExpectedWithNewMetamodel;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
@ -56,7 +56,6 @@ import org.junit.Test;
* @author Steve Ebersole
* @author Brett Meyer
*/
@FailureExpectedWithNewMetamodel
public class PersistentMapTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {

View File

@ -43,7 +43,6 @@ import org.hibernate.metamodel.spi.relational.ForeignKey;
import org.hibernate.metamodel.spi.relational.TableSpecification;
import org.hibernate.metamodel.spi.relational.UniqueKey;
import org.hibernate.test.util.SchemaUtil;
import org.hibernate.testing.FailureExpectedWithNewMetamodel;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
@ -51,7 +50,6 @@ import org.junit.Test;
/**
* @author Brett Meyer
*/
@FailureExpectedWithNewMetamodel
public class ConstraintTest extends BaseCoreFunctionalTestCase {
private static final int MAX_NAME_LENGTH = 30;
@ -67,6 +65,7 @@ public class ConstraintTest extends BaseCoreFunctionalTestCase {
private static final String EXPLICIT_JOINTABLE_NAME_JPA_M2M = "EXPLICIT_JOINTABLE_NAME_JPA_M2M";
private static final String EXPLICIT_COLUMN_NAME_JPA_M2M = "EXPLICIT_COLUMN_NAME_JPA_M2M";
private static final String EXPLICIT_FK_NAME_JPA_M2M = "EXPLICIT_FK_NAME_JPA_M2M";
private static final String EXPLICIT_COLLECTIONTABLE_NAME_JPA_ELEMENT = "EXPLICIT_COLLECTIONTABLE_NAME_JPA_ELEMENT";
private static final String EXPLICIT_COLUMN_NAME_JPA_ELEMENT = "EXPLICIT_COLUMN_NAME_JPA_ELEMENT";
private static final String EXPLICIT_FK_NAME_JPA_ELEMENT = "EXPLICIT_FK_NAME_JPA_ELEMENT";
private static final String INDEX_1 = "INDEX_1";
@ -123,11 +122,12 @@ public class ConstraintTest extends BaseCoreFunctionalTestCase {
TableSpecification table2 = SchemaUtil.getTable( DataPoint2.class, metadata() );
TableSpecification joinTable = SchemaUtil.getTable( EXPLICIT_JOINTABLE_NAME_JPA_M2M, metadata() );
TableSpecification collectionTable = SchemaUtil.getTable( EXPLICIT_COLLECTIONTABLE_NAME_JPA_ELEMENT, metadata() );
assertTrue( SchemaUtil.hasForeignKey( table2, EXPLICIT_FK_NAME_NATIVE, EXPLICIT_COLUMN_NAME_NATIVE ) );
assertTrue( SchemaUtil.hasForeignKey( table2, EXPLICIT_FK_NAME_JPA_O2O, EXPLICIT_COLUMN_NAME_JPA_O2O ) );
assertTrue( SchemaUtil.hasForeignKey( table2, EXPLICIT_FK_NAME_JPA_M2O, EXPLICIT_COLUMN_NAME_JPA_M2O ) );
assertTrue( SchemaUtil.hasForeignKey( joinTable, EXPLICIT_FK_NAME_JPA_M2M, EXPLICIT_COLUMN_NAME_JPA_M2M ) );
assertTrue( SchemaUtil.hasForeignKey( table2, EXPLICIT_FK_NAME_JPA_ELEMENT, EXPLICIT_COLUMN_NAME_JPA_ELEMENT ) );
assertTrue( SchemaUtil.hasForeignKey( collectionTable, EXPLICIT_FK_NAME_JPA_ELEMENT, EXPLICIT_COLUMN_NAME_JPA_ELEMENT ) );
testConstraintLength( table1 );
testConstraintLength( table2 );
@ -152,7 +152,7 @@ public class ConstraintTest extends BaseCoreFunctionalTestCase {
@Index(columnList = "foo5,foo6", name = INDEX_4),
@Index(columnList = "foo6,foo5", name = INDEX_5),
@Index(columnList = "foo5", name = INDEX_6) } )
public static class DataPoint {
private static class DataPoint {
@Id
@GeneratedValue
public long id;
@ -175,7 +175,7 @@ public class ConstraintTest extends BaseCoreFunctionalTestCase {
@Entity
@Table( name = "DataPoint2" )
public static class DataPoint2 {
private static class DataPoint2 {
@Id
@GeneratedValue
public long id;
@ -196,18 +196,19 @@ public class ConstraintTest extends BaseCoreFunctionalTestCase {
@ManyToOne
@JoinColumn(name = EXPLICIT_COLUMN_NAME_JPA_M2O,
foreignKey = @javax.persistence.ForeignKey(name = EXPLICIT_FK_NAME_JPA_M2O))
private DataPoint explicit_jpa_m2o;
public DataPoint explicit_jpa_m2o;
@ManyToMany
@JoinTable(name = EXPLICIT_JOINTABLE_NAME_JPA_M2M,
joinColumns = @JoinColumn(name = EXPLICIT_COLUMN_NAME_JPA_M2M),
foreignKey = @javax.persistence.ForeignKey(name = EXPLICIT_FK_NAME_JPA_M2M))
private Set<DataPoint> explicit_jpa_m2m;
public Set<DataPoint> explicit_jpa_m2m;
@ElementCollection
@CollectionTable(joinColumns = @JoinColumn(name = EXPLICIT_COLUMN_NAME_JPA_ELEMENT),
@CollectionTable(name = EXPLICIT_COLLECTIONTABLE_NAME_JPA_ELEMENT,
joinColumns = @JoinColumn(name = EXPLICIT_COLUMN_NAME_JPA_ELEMENT),
foreignKey = @javax.persistence.ForeignKey(name = EXPLICIT_FK_NAME_JPA_ELEMENT))
private Set<String> explicit_jpa_element;
public Set<String> explicit_jpa_element;
}
public static enum SimpleEnum {