HHH-7517 - Make sure new metamodel bindings create FK for joined inheritance
This commit is contained in:
parent
5c986ab59f
commit
c782548172
|
@ -47,6 +47,7 @@ import org.hibernate.id.IdentifierGenerator;
|
|||
import org.hibernate.id.IdentityGenerator;
|
||||
import org.hibernate.id.PersistentIdentifierGenerator;
|
||||
import org.hibernate.id.factory.IdentifierGeneratorFactory;
|
||||
import org.hibernate.internal.jaxb.mapping.hbm.JaxbJoinedSubclassElement;
|
||||
import org.hibernate.internal.util.ReflectHelper;
|
||||
import org.hibernate.internal.util.StringHelper;
|
||||
import org.hibernate.internal.util.ValueHolder;
|
||||
|
@ -1703,9 +1704,22 @@ public class Binder {
|
|||
if ( discriminatorValue != null ) {
|
||||
entityBinding.setDiscriminatorMatchValue( discriminatorValue );
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
bindPrimaryTable( entityBinding, entitySource );
|
||||
}
|
||||
|
||||
if ( inheritanceType == InheritanceType.JOINED && superEntityBinding != null ) {
|
||||
ForeignKey fk = entityBinding.getPrimaryTable().createForeignKey(
|
||||
superEntityBinding.getPrimaryTable(),
|
||||
( (SubclassEntitySource) entitySource ).getJoinedForeignKeyName()
|
||||
);
|
||||
// explicitly maps to target table pk
|
||||
for ( Column column : entityBinding.getPrimaryTable().getPrimaryKey().getColumns() ) {
|
||||
fk.addColumn( column );
|
||||
}
|
||||
}
|
||||
|
||||
// todo: deal with joined and unioned subclass bindings
|
||||
// todo: bind fetch profiles
|
||||
// Configure rest of binding
|
||||
|
|
|
@ -40,15 +40,15 @@ public class SubclassEntitySourceImpl extends EntitySourceImpl implements Subcla
|
|||
this.container = container;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @see org.hibernate.metamodel.spi.source.SubclassEntitySource#superclassEntitySource()
|
||||
*/
|
||||
@Override
|
||||
public EntitySource superclassEntitySource() {
|
||||
return container;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getJoinedForeignKeyName() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
package org.hibernate.metamodel.internal.source.hbm;
|
||||
|
||||
import org.hibernate.internal.jaxb.mapping.hbm.EntityElement;
|
||||
import org.hibernate.internal.jaxb.mapping.hbm.JaxbJoinedSubclassElement;
|
||||
import org.hibernate.internal.jaxb.mapping.hbm.JaxbSubclassElement;
|
||||
import org.hibernate.internal.jaxb.mapping.hbm.TableInformationSource;
|
||||
import org.hibernate.metamodel.spi.source.EntitySource;
|
||||
|
@ -66,4 +67,12 @@ public class SubclassEntitySourceImpl extends AbstractEntitySourceImpl implement
|
|||
public EntitySource superclassEntitySource() {
|
||||
return container;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getJoinedForeignKeyName() {
|
||||
if ( JaxbJoinedSubclassElement.class.isInstance( entityElement() ) ) {
|
||||
return ( (JaxbJoinedSubclassElement) entityElement() ).getKey().getForeignKey();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,9 +27,16 @@ package org.hibernate.metamodel.spi.source;
|
|||
* @author Steve Ebersole
|
||||
*/
|
||||
public interface SubclassEntitySource extends EntitySource {
|
||||
|
||||
/**
|
||||
* @return the entity source for the entity that is a superclass of the entity for which this is a source
|
||||
*/
|
||||
EntitySource superclassEntitySource();
|
||||
|
||||
/**
|
||||
* Not sure the best way to model this. We need access to the underlying <key/> mapping for the joined-subclass
|
||||
* mapping. Not sure what capabilities jpa/annotations exposes for this.
|
||||
*
|
||||
* @return The configured FK name.
|
||||
*/
|
||||
public String getJoinedForeignKeyName();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2012, 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.metamodel.spi.binding;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.persistence.InheritanceType;
|
||||
|
||||
import org.hibernate.metamodel.MetadataSources;
|
||||
import org.hibernate.metamodel.internal.MetadataImpl;
|
||||
import org.hibernate.service.ServiceRegistryBuilder;
|
||||
import org.hibernate.service.internal.StandardServiceRegistryImpl;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class JoinedSubclassBindingTests extends BaseUnitTestCase {
|
||||
private StandardServiceRegistryImpl serviceRegistry;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
serviceRegistry = (StandardServiceRegistryImpl) new ServiceRegistryBuilder().buildServiceRegistry();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
serviceRegistry.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinedSubclassBindingGeneratesForeignKey() {
|
||||
MetadataSources sources = new MetadataSources( serviceRegistry );
|
||||
sources.addAnnotatedClass( Sub.class );
|
||||
MetadataImpl metadata = (MetadataImpl) sources.buildMetadata();
|
||||
|
||||
EntityBinding entityBinding = metadata.getEntityBinding( Sub.class.getName() );
|
||||
assertTrue( entityBinding.getPrimaryTable().getForeignKeys().iterator().hasNext() );
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Inheritance( strategy = InheritanceType.JOINED )
|
||||
public static class Base {
|
||||
@Id
|
||||
private Long id;
|
||||
}
|
||||
|
||||
@Entity
|
||||
public static class Sub extends Base {
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue