HHH-6168 : test for many-to-one AttributeBinding and misc corrections

This commit is contained in:
Gail Badner 2011-05-01 11:59:22 -07:00
parent 1ae1d4b699
commit b667767f85
15 changed files with 269 additions and 117 deletions

View File

@ -109,12 +109,20 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
this.attribute = attribute;
}
protected void initializeColumnValue(ColumnRelationalState state, boolean forceNonNullable, boolean forceUnique) {
Column columnValue = createColumn( state, forceNonNullable, forceUnique );
protected boolean forceNonNullable() {
return false;
}
protected boolean forceUnique() {
return false;
}
protected void initializeColumnValue(ColumnRelationalState state) {
Column columnValue = createColumn( state );
setValue( columnValue );
}
private Column createColumn(ColumnRelationalState state, boolean forceNonNullable, boolean forceUnique) {
private Column createColumn(ColumnRelationalState state) {
final String explicitName = state.getExplicitColumnName();
final String logicalColumnName = state.getNamingStrategy().logicalColumnName( explicitName, getAttribute().getName() );
final TableSpecification table = getEntityBinding().getBaseTable();
@ -126,8 +134,8 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
// mappings.addColumnBinding( logicalColumnName, column, table );
Column columnValue = table.createColumn( columnName );
columnValue.getSize().initialize( state.getSize() );
columnValue.setNullable( ! forceNonNullable && state.isNullable() );
columnValue.setUnique( ! forceUnique && state.isUnique() );
columnValue.setNullable( ! forceNonNullable() && state.isNullable() );
columnValue.setUnique( ! forceUnique() && state.isUnique() );
columnValue.setCheckCondition( state.getCheckCondition() );
columnValue.setDefaultValue( state.getDefault() );
columnValue.setSqlType( state.getSqlType() );
@ -143,6 +151,16 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
return columnValue;
}
public final void initialize(RelationalState state) {
if ( SingleValueRelationalState.class.isInstance( state ) ) {
initializeSingleValue( SingleValueRelationalState.class.cast( state ) );
}
else if ( SimpleTupleRelationalState.class.isInstance( state ) ) {
initializeTupleValue( SimpleTupleRelationalState.class.cast( state ).getRelationalStates() );
}
}
public final <T extends DerivedRelationalState> void initializeDerivedValue(T state) {
value = createDerivedValue( state );
}
@ -151,13 +169,13 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
return getEntityBinding().getBaseTable().createDerivedValue( state.getFormula() );
}
public final void initializeSingleValue(SingleValueRelationalState state, boolean forceNonNullable, boolean forceUnique) {
value = createSingleValue( state, forceNonNullable, forceUnique );
public final void initializeSingleValue(SingleValueRelationalState state) {
value = createSingleValue( state );
}
protected SimpleValue createSingleValue(SingleValueRelationalState state, boolean forceNonNullable, boolean forceUnique) {
protected SimpleValue createSingleValue(SingleValueRelationalState state) {
if ( state instanceof ColumnRelationalState ) {
return createColumn( ColumnRelationalState.class.cast( state ), forceNonNullable, forceUnique );
return createColumn( ColumnRelationalState.class.cast( state ) );
}
else if ( state instanceof DerivedRelationalState ) {
return createDerivedValue( DerivedRelationalState.class.cast( state ) );
@ -167,12 +185,20 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
}
}
protected final void initializeTupleValue(Set<SingleValueRelationalState> singleValueStates, boolean forceNonNullable, boolean forceUnique) {
Tuple tuple = getEntityBinding().getBaseTable().createTuple( "[" + getAttribute().getName() + "]" );
for ( SingleValueRelationalState singleValueState : singleValueStates ) {
tuple.addValue( createSingleValue( singleValueState, forceNonNullable, forceUnique ) );
protected final void initializeTupleValue(Set<SingleValueRelationalState> singleValueStates) {
if ( singleValueStates.size() == 0 ) {
throw new MappingException( "Tuple state does not contain any values." );
}
if ( singleValueStates.size() == 1 ) {
initializeSingleValue( singleValueStates.iterator().next() );
}
else {
Tuple tuple = getEntityBinding().getBaseTable().createTuple( "[" + getAttribute().getName() + "]" );
for ( SingleValueRelationalState singleValueState : singleValueStates ) {
tuple.addValue( createSingleValue( singleValueState ) );
}
value = tuple;
}
value = tuple;
}
@Override
@ -290,6 +316,10 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
entityReferencingAttributeBindings.add( referencingAttributeBinding );
}
public Set<EntityReferencingAttributeBinding> getEntityReferencingAttributeBindings() {
return Collections.unmodifiableSet( entityReferencingAttributeBindings );
}
public void validate() {
if ( ! entityReferencingAttributeBindings.isEmpty() ) {
// TODO; validate that this AttributeBinding can be a target of an entity reference
@ -298,9 +328,11 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
}
}
public static interface SingleValueRelationalState {}
public static interface RelationalState {}
public static interface ColumnRelationalState extends SimpleAttributeBinding.SingleValueRelationalState {
public static interface SingleValueRelationalState extends RelationalState {}
public static interface ColumnRelationalState extends SingleValueRelationalState {
NamingStrategy getNamingStrategy();
String getExplicitColumnName();
boolean isUnique();
@ -320,4 +352,10 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
String getFormula();
}
public static interface SimpleTupleRelationalState extends TupleRelationalState<SingleValueRelationalState> {
}
public static interface TupleRelationalState<T extends RelationalState> extends RelationalState{
Set<T> getRelationalStates();
}
}

View File

@ -24,6 +24,7 @@
package org.hibernate.metamodel.binding;
import java.util.Map;
import java.util.Set;
import org.hibernate.metamodel.domain.Attribute;
import org.hibernate.metamodel.domain.MetaAttribute;
@ -104,6 +105,7 @@ public interface AttributeBinding {
public boolean isLazy();
public void addEntityReferencingAttributeBinding(EntityReferencingAttributeBinding attributeBinding);
public Set<EntityReferencingAttributeBinding> getEntityReferencingAttributeBindings();
public void validate();
}

View File

@ -32,6 +32,7 @@ import org.hibernate.metamodel.relational.Value;
*/
public interface EntityReferencingAttributeBinding extends AttributeBinding {
boolean isReferenceResolved();
boolean isPropertyReference();
String getReferencedEntityName();
String getReferencedAttributeName();
EntityBinding getReferencedEntityBinding();

View File

@ -26,10 +26,10 @@ package org.hibernate.metamodel.binding;
import java.util.Iterator;
import org.hibernate.MappingException;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.metamodel.relational.ForeignKey;
import org.hibernate.metamodel.relational.SimpleValue;
import org.hibernate.metamodel.relational.Column;
import org.hibernate.metamodel.source.Metadata;
/**
* TODO : javadoc
@ -52,7 +52,7 @@ public class ManyToOneAttributeBinding extends SingularAttributeBinding implemen
boolean ignoreNotFound();
}
public static interface RelationalState extends SingleValueRelationalState {
public static interface ManyToOneRelationalState extends RelationalState {
boolean isLogicalOneToOne();
String getForeignKeyName();
}
@ -66,14 +66,25 @@ public class ManyToOneAttributeBinding extends SingularAttributeBinding implemen
isPropertyReference = state.getReferencedAttributeName() != null;
referencedAttributeName = state.getReferencedAttributeName();
referencedEntityName = state.getReferencedEntityName();
if ( referencedEntityName == null ) {
referencedEntityName =
ReflectHelper.reflectedPropertyClass(
getEntityBinding().getEntity().getName(),
state.getAttribute().getName()
).getName();
}
}
public final void initialize(RelationalState state) {
super.initializeSingleValue( state );
public final void initialize(ManyToOneRelationalState state) {
super.initialize( state );
isLogicalOneToOne = state.isLogicalOneToOne();
foreignKeyName = state.getForeignKeyName();
}
public final boolean isPropertyReference() {
return isPropertyReference;
}
public final String getReferencedEntityName() {
return referencedEntityName;
}

View File

@ -23,15 +23,8 @@
*/
package org.hibernate.metamodel.binding;
import java.util.Set;
import org.hibernate.MappingException;
import org.hibernate.mapping.PropertyGeneration;
import org.hibernate.metamodel.relational.Column;
import org.hibernate.metamodel.relational.DerivedValue;
import org.hibernate.metamodel.relational.SimpleValue;
import org.hibernate.metamodel.relational.TableSpecification;
import org.hibernate.metamodel.relational.Tuple;
/**
* TODO : javadoc
@ -54,15 +47,15 @@ public class SimpleAttributeBinding extends SingularAttributeBinding {
generation = state.getPropertyGeneration();
}
public final void initializeTupleValue(TupleRelationalState state) {
if ( state.getSingleValueRelationalStates().size() == 0 ) {
public final void initializeTupleValue(SimpleTupleRelationalState state) {
if ( state.getRelationalStates().size() == 0 ) {
throw new MappingException( "Tuple state does not contain any values." );
}
if ( state.getSingleValueRelationalStates().size() == 1 ) {
initializeSingleValue( state.getSingleValueRelationalStates().iterator().next() );
if ( state.getRelationalStates().size() == 1 ) {
initializeSingleValue( state.getRelationalStates().iterator().next() );
}
else {
initializeTupleValue( state.getSingleValueRelationalStates() );
initializeTupleValue( state.getRelationalStates() );
}
}
@ -83,7 +76,4 @@ public class SimpleAttributeBinding extends SingularAttributeBinding {
return generation;
}
public static interface TupleRelationalState {
Set<SingleValueRelationalState> getSingleValueRelationalStates();
}
}

View File

@ -23,13 +23,6 @@
*/
package org.hibernate.metamodel.binding;
import java.util.Set;
import org.hibernate.MappingException;
import org.hibernate.metamodel.relational.DerivedValue;
import org.hibernate.metamodel.relational.SimpleValue;
import org.hibernate.metamodel.relational.Tuple;
/**
* TODO : javadoc
*
@ -64,18 +57,6 @@ public abstract class SingularAttributeBinding extends AbstractAttributeBinding
unsavedValue = state.getUnsavedValue();
}
public final void initializeColumnValue(ColumnRelationalState state) {
initializeColumnValue( state, forceNonNullable, forceUnique );
}
public final void initializeSingleValue(SingleValueRelationalState state) {
initializeSingleValue( state, forceNonNullable, forceUnique );
}
protected void initializeTupleValue(Set<SingleValueRelationalState> singleValueStates) {
initializeTupleValue( singleValueStates, forceNonNullable, forceUnique );
}
public boolean isInsertable() {
return insertable;
}

View File

@ -315,11 +315,11 @@ public class EntityBinder {
}
public static class AnnotationSimpleAttributeRelationalState
implements SimpleAttributeBinding.TupleRelationalState {
implements SimpleAttributeBinding.SimpleTupleRelationalState {
LinkedHashSet<SimpleAttributeBinding.SingleValueRelationalState> valueStates = new LinkedHashSet<SimpleAttributeBinding.SingleValueRelationalState>();
@Override
public LinkedHashSet<SimpleAttributeBinding.SingleValueRelationalState> getSingleValueRelationalStates() {
public LinkedHashSet<SimpleAttributeBinding.SingleValueRelationalState> getRelationalStates() {
return valueStates;
}
}

View File

@ -25,28 +25,14 @@ package org.hibernate.metamodel.source.hbm.state.domain;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.Element;
import org.hibernate.FetchMode;
import org.hibernate.MappingException;
import org.hibernate.cfg.HbmBinder;
import org.hibernate.cfg.Mappings;
import org.hibernate.mapping.Fetchable;
import org.hibernate.mapping.OneToOne;
import org.hibernate.mapping.PropertyGeneration;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.binding.HibernateTypeDescriptor;
import org.hibernate.metamodel.binding.ManyToOneAttributeBinding;
import org.hibernate.metamodel.binding.MappingDefaults;
import org.hibernate.metamodel.binding.SimpleAttributeBinding;
import org.hibernate.metamodel.domain.MetaAttribute;
import org.hibernate.metamodel.source.Metadata;
import org.hibernate.metamodel.source.hbm.HbmHelper;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLManyToOneElement;
import org.hibernate.metamodel.source.util.DomHelper;
import org.hibernate.metamodel.source.util.MappingHelper;
import org.hibernate.tuple.component.Dom4jComponentTuplizer;
/**
* @author Gail Badner
@ -56,8 +42,16 @@ public class HbmManyToOneAttributeDomainState
implements ManyToOneAttributeBinding.DomainState {
private final HibernateTypeDescriptor hibernateTypeDescriptor = new HibernateTypeDescriptor();
private final XMLManyToOneElement manyToOne;
private final FetchMode fetchMode;
private final boolean isUnwrapProxy;
private final boolean isLazy;
private final String cascade;
private final boolean isEmbedded;
private final String referencedPropertyName;
private final String referencedEntityName;
private final boolean ignoreNotFound;
private final boolean isInsertable;
private final boolean isUpdateable;
public HbmManyToOneAttributeDomainState(MappingDefaults defaults,
org.hibernate.metamodel.domain.Attribute attribute,
@ -71,15 +65,29 @@ public class HbmManyToOneAttributeDomainState
HbmHelper.getPropertyAccessorName( manyToOne.getAccess(), manyToOne.isEmbedXml(), defaults.getDefaultAccess() ),
manyToOne.isOptimisticLock()
);
this.hibernateTypeDescriptor.setTypeName( getReferencedEntityName() );
this.manyToOne = manyToOne;
this.cascade = MappingHelper.getStringValue( manyToOne.getCascade(), defaults.getDefaultCascade() );
fetchMode = getFetchMode( manyToOne );
isUnwrapProxy = manyToOne.getLazy() != null && "no-proxy".equals( manyToOne.getLazy().value() );
//TODO: better to degrade to lazy="false" if uninstrumented
isLazy = manyToOne.getLazy() == null ||
isUnwrapProxy ||
"proxy".equals( manyToOne.getLazy().value() );
cascade = MappingHelper.getStringValue( manyToOne.getCascade(), defaults.getDefaultCascade() );
isEmbedded = manyToOne.isEmbedXml();
hibernateTypeDescriptor.setTypeName( getReferencedEntityName() );
referencedPropertyName = manyToOne.getPropertyRef();
referencedEntityName = (
manyToOne.getEntityName() == null ?
HbmHelper.getClassName( manyToOne.getClazz(), getDefaults().getPackageName() ) :
manyToOne.getEntityName().intern()
);
ignoreNotFound = "ignore".equals( manyToOne.getNotFound().value() );
isInsertable = manyToOne.isInsert();
isUpdateable = manyToOne.isUpdate();
}
// TODO: is this needed???
protected boolean isEmbedded() {
return MappingHelper.getBooleanValue( manyToOne.isEmbedXml(), true );
return isEmbedded;
}
public HibernateTypeDescriptor getHibernateTypeDescriptor() {
@ -87,13 +95,13 @@ public class HbmManyToOneAttributeDomainState
}
// same as for plural attributes...
public FetchMode getFetchMode() {
private static FetchMode getFetchMode(XMLManyToOneElement manyToOne) {
FetchMode fetchMode;
if ( manyToOne.getFetch() != null ) {
fetchMode = "join".equals( manyToOne.getFetch() ) ? FetchMode.JOIN : FetchMode.SELECT;
fetchMode = "join".equals( manyToOne.getFetch().value() ) ? FetchMode.JOIN : FetchMode.SELECT;
}
else {
String jfNodeValue = ( manyToOne.getOuterJoin().value() == null ? "auto" : manyToOne.getOuterJoin().value() );
String jfNodeValue = ( manyToOne.getOuterJoin() == null ? "auto" : manyToOne.getOuterJoin().value() );
if ( "auto".equals( jfNodeValue ) ) {
fetchMode = FetchMode.DEFAULT;
}
@ -107,54 +115,40 @@ public class HbmManyToOneAttributeDomainState
return fetchMode;
}
public FetchMode getFetchMode() {
return fetchMode;
}
public boolean isLazy() {
return manyToOne.getLazy() == null ||
isUnwrapProxy() ||
manyToOne.getLazy().equals( "proxy" );
//TODO: better to degrade to lazy="false" if uninstrumented
return isLazy;
}
public boolean isUnwrapProxy() {
return "no-proxy".equals( manyToOne.getLazy() );
return isUnwrapProxy;
}
public String getReferencedAttributeName() {
return manyToOne.getPropertyRef();
return referencedPropertyName;
}
public String getReferencedEntityName() {
String entityName = manyToOne.getEntityName();
return entityName == null ?
HbmHelper.getClassName( manyToOne.getClazz(), getDefaults().getPackageName() ) :
entityName.intern();
return referencedEntityName;
}
public String getCascade() {
return MappingHelper.getStringValue( manyToOne.getCascade(), getDefaults().getDefaultCascade() );
return cascade;
}
public boolean ignoreNotFound() {
return "ignore".equals( manyToOne.getNotFound() );
return ignoreNotFound;
}
/*
void junk() {
if( getReferencedPropertyName() != null && ! ignoreNotFound() ) {
mappings.addSecondPass( new ManyToOneSecondPass(manyToOne) );
}
}
*/
public boolean isInsertable() {
return MappingHelper.getBooleanValue( manyToOne.isInsert(), true );
return isInsertable;
}
public boolean isUpdateable() {
return MappingHelper.getBooleanValue( manyToOne.isUnique(), true );
}
public String getForeignkeyName() {
return manyToOne.getForeignKey();
return isUpdateable;
}
public boolean isKeyCasadeDeleteEnabled() {

View File

@ -30,10 +30,10 @@ import java.util.Set;
import org.hibernate.MappingException;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.metamodel.binding.AbstractAttributeBinding;
import org.hibernate.metamodel.binding.HibernateTypeDescriptor;
import org.hibernate.metamodel.binding.MappingDefaults;
import org.hibernate.metamodel.binding.SimpleAttributeBinding;
import org.hibernate.metamodel.relational.Size;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLColumnElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLDiscriminator;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLId;
@ -45,7 +45,7 @@ import org.hibernate.metamodel.source.hbm.xml.mapping.XMLPropertyElement;
/**
* @author Gail Badner
*/
public class HbmSimpleValueRelationalStateContainer implements SimpleAttributeBinding.TupleRelationalState {
public class HbmSimpleValueRelationalStateContainer implements AbstractAttributeBinding.SimpleTupleRelationalState {
private final MappingDefaults defaults;
private final Set<String> propertyUniqueKeys;
private final Set<String> propertyIndexes;
@ -196,7 +196,7 @@ public class HbmSimpleValueRelationalStateContainer implements SimpleAttributeBi
throw new MappingException( "unknown type of column or formula: " + columnOrFormula.getClass().getName() );
}
public Set<SimpleAttributeBinding.SingleValueRelationalState> getSingleValueRelationalStates() {
public Set<SimpleAttributeBinding.SingleValueRelationalState> getRelationalStates() {
return singleValueStates;
}

View File

@ -59,9 +59,9 @@ class EntityReferenceResolver {
"] refers to unknown entity: [" + attributeBinding.getReferencedEntityName() + "]" );
}
AttributeBinding referencedAttributeBinding =
attributeBinding.getReferencedAttributeName() == null ?
entityBinding.getEntityIdentifier().getValueBinding() :
entityBinding.getAttributeBinding( attributeBinding.getReferencedAttributeName() );
attributeBinding.isPropertyReference() ?
entityBinding.getAttributeBinding( attributeBinding.getReferencedAttributeName() ) :
entityBinding.getEntityIdentifier().getValueBinding();
if ( referencedAttributeBinding == null ) {
// TODO: does attribute name include path w/ entity name?
throw new MappingException(

View File

@ -23,7 +23,12 @@
*/
package org.hibernate.metamodel.binding;
import java.util.Iterator;
import org.hibernate.metamodel.relational.Column;
import org.hibernate.metamodel.source.Metadata;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
import org.hibernate.service.BasicServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.internal.BasicServiceRegistryImpl;
@ -34,6 +39,7 @@ import org.junit.Test;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static org.junit.Assert.assertSame;
@ -64,7 +70,10 @@ public abstract class AbstractBasicBindingTests extends BaseUnitTestCase {
@Test
public void testSimpleEntityMapping() {
EntityBinding entityBinding = buildSimpleEntityBinding();
checkSimpleEntityMaping( buildSimpleEntityBinding() );
}
protected void checkSimpleEntityMaping(EntityBinding entityBinding) {
assertNotNull( entityBinding );
assertNotNull( entityBinding.getEntityIdentifier() );
assertNotNull( entityBinding.getEntityIdentifier().getValueBinding() );
@ -105,6 +114,21 @@ public abstract class AbstractBasicBindingTests extends BaseUnitTestCase {
assertNotNull( nameBinding.getValue() );
}
@Test
public void testEntityWithManyToOneMapping() {
MetadataImplementor metadata = buildMetadataWithManyToOne();
EntityBinding entityWithManyToOneBinding = metadata.getEntityBinding( EntityWithManyToOne.class.getName() );
EntityBinding simpleEntityBinding = metadata.getEntityBinding( SimpleEntity.class.getName() );
checkSimpleEntityMaping( simpleEntityBinding );
assertTrue(
1 == simpleEntityBinding.getAttributeBinding( "id" ).getEntityReferencingAttributeBindings().size()
);
Iterator<EntityReferencingAttributeBinding> it = entityWithManyToOneBinding.getEntityReferencingAttributeBindings().iterator();
assertTrue( it.hasNext() );
assertSame( entityWithManyToOneBinding.getAttributeBinding( "simpleEntity" ), it.next() );
assertFalse( it.hasNext() );
}
/*
@Test
public void testEntityWithElementCollection() {
@ -133,5 +157,7 @@ public abstract class AbstractBasicBindingTests extends BaseUnitTestCase {
public abstract EntityBinding buildSimpleEntityBinding();
public abstract MetadataImplementor buildMetadataWithManyToOne();
//public abstract EntityBinding buildEntityWithElementCollectionBinding();
}

View File

@ -25,8 +25,10 @@ package org.hibernate.metamodel.binding;
import org.junit.Test;
import org.hibernate.metamodel.source.Metadata;
import org.hibernate.metamodel.source.MetadataSources;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.testing.FailureExpected;
@ -49,6 +51,12 @@ public class BasicAnnotationBindingTests extends AbstractBasicBindingTests {
super.testSimpleVersionedEntityMapping();
}
@FailureExpected(jiraKey = "HHH-6172", message = "Work in progress")
@Test
public void testEntityWithManyToOneMapping() {
super.testEntityWithManyToOneMapping();
}
public EntityBinding buildSimpleEntityBinding() {
MetadataSources sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
sources.addAnnotatedClass( SimpleEntity.class );
@ -64,4 +72,11 @@ public class BasicAnnotationBindingTests extends AbstractBasicBindingTests {
return metadata.getEntityBinding( SimpleVersionedEntity.class.getSimpleName() );
}
public MetadataImplementor buildMetadataWithManyToOne() {
MetadataSources sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
sources.addAnnotatedClass( EntityWithManyToOne.class );
sources.addAnnotatedClass( SimpleVersionedEntity.class );
return ( MetadataImplementor ) sources.buildMetadata();
}
}

View File

@ -23,15 +23,11 @@
*/
package org.hibernate.metamodel.binding;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.jboss.logging.Logger;
import org.hibernate.metamodel.source.MetadataSources;
import org.hibernate.metamodel.source.internal.JaxbRoot;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
import org.junit.Test;
@ -59,6 +55,14 @@ public class BasicHbmBindingTests extends AbstractBasicBindingTests {
);
}
public MetadataImplementor buildMetadataWithManyToOne() {
MetadataSources metadataSources = new MetadataSources( basicServiceRegistry() );
metadataSources.addResource( "org/hibernate/metamodel/binding/EntityWithManyToOne.hbm.xml" );
metadataSources.addResource( "org/hibernate/metamodel/binding/SimpleEntity.hbm.xml" );
assertEquals( 2, metadataSources.getJaxbRootList().size() );
return ( MetadataImplementor ) metadataSources.buildMetadata();
}
private EntityBinding getEntityBinding(String resourceName, String entityName ) {
MetadataSources metadataSources = new MetadataSources( basicServiceRegistry() );
metadataSources.addResource( resourceName );

View File

@ -0,0 +1,15 @@
<?xml version="1.0"?>
<hhh:hibernate-mapping package="org.hibernate.metamodel.binding" xmlns:hhh="http://www.hibernate.org/xsd/hibernate-mapping"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping hibernate-mapping-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<class name="EntityWithManyToOne">
<id name="id">
<generator class="increment"/>
</id>
<property name="name"/>
<many-to-one name="simpleEntity"/>
</class>
</hhh:hibernate-mapping>

View File

@ -0,0 +1,75 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, 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.binding;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import org.hibernate.annotations.Entity;
/**
* @author Gail Badner
*/
@Entity
public class EntityWithManyToOne {
@Id
private Long id;
private String theName;
SimpleEntity simpleEntity;
public EntityWithManyToOne() {
}
public EntityWithManyToOne(String name) {
this.theName = theName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return theName;
}
public void setName(String name) {
this.theName = theName;
}
@ManyToOne
public SimpleEntity getSimpleEntity() {
return simpleEntity;
}
public void setSimpleEntity(SimpleEntity simpleEntity) {
this.simpleEntity = simpleEntity;
}
}