HHH-6251 : Create CollectionEement subclasses

This commit is contained in:
Gail Badner 2011-05-24 13:45:25 -07:00
parent 9b48cd993e
commit 6d5ed5fc5e
10 changed files with 227 additions and 13 deletions

View File

@ -31,8 +31,8 @@ import org.hibernate.metamodel.binding.state.PluralAttributeBindingState;
* @author Steve Ebersole
*/
public class BagBinding extends PluralAttributeBinding {
protected BagBinding(EntityBinding entityBinding) {
super( entityBinding );
protected BagBinding(EntityBinding entityBinding, CollectionElementType collectionElementType) {
super( entityBinding, collectionElementType );
}
public BagBinding initialize(PluralAttributeBindingState bindingState) {

View File

@ -30,17 +30,22 @@ import org.hibernate.metamodel.relational.Value;
*
* @author Steve Ebersole
*/
public class CollectionElement {
public abstract class CollectionElement {
private final HibernateTypeDescriptor hibernateTypeDescriptor = new HibernateTypeDescriptor();
private final PluralAttributeBinding collectionBinding;
private final CollectionElementType collectionElementType;
private String nodeName;
private Value elementValue;
CollectionElement(PluralAttributeBinding collectionBinding) {
CollectionElement(PluralAttributeBinding collectionBinding, CollectionElementType collectionElementType) {
this.collectionBinding = collectionBinding;
this.collectionElementType = collectionElementType;
}
public final CollectionElementType getCollectionElementType() {
return collectionElementType;
}
/* package-protected */
@ -52,4 +57,13 @@ public class CollectionElement {
void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
public boolean isOneToMany() {
return collectionElementType.isOneToMany();
}
public boolean isManyToMany() {
return collectionElementType.isManyToMany();
}
}

View File

@ -0,0 +1,104 @@
/*
* 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;
/**
* @author Gail Badner
*/
public enum CollectionElementType {
ELEMENT( "element" ) {
public CollectionElement createCollectionElementInternal(PluralAttributeBinding attributeBinding) {
return new ElementCollectionElement( attributeBinding );
}
},
COMPOSITE_ELEMENT( "composite-element" ) {
public CollectionElement createCollectionElementInternal(PluralAttributeBinding attributeBinding) {
return new ElementCollectionElement( attributeBinding );
}
},
ONE_TO_MANY( "one-to-many" ) {
public boolean isOneToMany() {
return true;
}
public CollectionElement createCollectionElementInternal(PluralAttributeBinding attributeBinding) {
return new OneToManyCollectionElement( attributeBinding );
}
},
MANY_TO_MANY( "many-to-many" ) {
public boolean isManyToMany() {
return true;
}
public CollectionElement createCollectionElementInternal(PluralAttributeBinding attributeBinding) {
return new ManyToManyCollectionElement( attributeBinding );
}
},
MANY_TO_ANY( "many-to-any" ) {
//TODO: should isManyToMany() return true?
public boolean isManyToAny() {
return true;
}
public CollectionElement createCollectionElementInternal(PluralAttributeBinding attributeBinding) {
return new ManyToAnyCollectionElement( attributeBinding );
}
};
private final String name;
private CollectionElementType(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString() {
return super.toString() + "[" + getName() + "]";
}
public boolean isOneToMany() {
return false;
}
public boolean isManyToMany() {
return false;
}
public boolean isManyToAny() {
return false;
}
protected abstract CollectionElement createCollectionElementInternal(PluralAttributeBinding attributeBinding);
/* package-protected */
CollectionElement createCollectionElement(PluralAttributeBinding attributeBinding) {
CollectionElement collectionElement = createCollectionElementInternal( attributeBinding );
if ( collectionElement.getCollectionElementType() != this ) {
throw new IllegalStateException( "Collection element has unexpected type nature: actual=[" +
collectionElement.getCollectionElementType() + "; expected=[" + this + "]" );
}
return collectionElement;
}
}

View File

@ -30,6 +30,6 @@ import org.dom4j.Element;
*/
public class ElementCollectionElement extends CollectionElement {
public ElementCollectionElement(PluralAttributeBinding binding) {
super( binding );
super( binding, CollectionElementType.ELEMENT );
}
}

View File

@ -270,8 +270,8 @@ public class EntityBinding {
return binding;
}
public BagBinding makeBagAttributeBinding(String attributeName) {
final BagBinding binding = new BagBinding( this );
public BagBinding makeBagAttributeBinding(String attributeName, CollectionElementType collectionElementType) {
final BagBinding binding = new BagBinding( this, collectionElementType );
registerAttributeBinding( attributeName, binding );
binding.setAttribute( entity.getAttribute( attributeName ) );
return binding;

View File

@ -0,0 +1,37 @@
/*
* 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.HashMap;
import org.dom4j.Element;
/**
* @author Gail Badner
*/
public class ManyToAnyCollectionElement extends CollectionElement {
ManyToAnyCollectionElement(PluralAttributeBinding binding) {
super( binding, CollectionElementType.MANY_TO_ANY );
}
}

View File

@ -36,8 +36,8 @@ public class ManyToManyCollectionElement extends CollectionElement {
private String manyToManyOrderBy;
ManyToManyCollectionElement(Element collectionNode, PluralAttributeBinding binding) {
super( binding );
ManyToManyCollectionElement(PluralAttributeBinding binding) {
super( binding, CollectionElementType.MANY_TO_MANY );
}
public void fromHbmXml(Element node){

View File

@ -0,0 +1,34 @@
/*
* 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;
/**
* @author Gail Badner
*/
public class OneToManyCollectionElement extends CollectionElement {
OneToManyCollectionElement(PluralAttributeBinding binding) {
super( binding, CollectionElementType.ONE_TO_MANY );
}
}

View File

@ -79,9 +79,9 @@ public abstract class PluralAttributeBinding extends AbstractAttributeBinding {
private String loaderName;
protected PluralAttributeBinding(EntityBinding entityBinding) {
protected PluralAttributeBinding(EntityBinding entityBinding, CollectionElementType collectionElementType) {
super( entityBinding );
collectionElement = new CollectionElement( this );
collectionElement = collectionElementType.createCollectionElement( this );
}
protected void initializeBinding(PluralAttributeBindingState state) {

View File

@ -25,6 +25,7 @@ package org.hibernate.metamodel.source.hbm;
import org.dom4j.Attribute;
import org.hibernate.AssertionFailure;
import org.hibernate.EntityMode;
import org.hibernate.MappingException;
import org.hibernate.cfg.NamingStrategy;
@ -32,6 +33,7 @@ import org.hibernate.engine.internal.Versioning;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.binding.AttributeBinding;
import org.hibernate.metamodel.binding.BagBinding;
import org.hibernate.metamodel.binding.CollectionElementType;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.binding.ManyToOneAttributeBinding;
import org.hibernate.metamodel.binding.SimpleAttributeBinding;
@ -489,13 +491,36 @@ PrimitiveArray
entityBinding.getMetaAttributes()
);
BagBinding collectionBinding = entityBinding.makeBagAttributeBinding( bindingState.getAttributeName() )
BagBinding collectionBinding = entityBinding.makeBagAttributeBinding(
bindingState.getAttributeName(),
getCollectionElementType( collection ) )
.initialize( bindingState );
// todo : relational model binding
return collectionBinding;
}
private CollectionElementType getCollectionElementType(XMLBagElement collection) {
if ( collection.getElement() != null ) {
return CollectionElementType.ELEMENT;
}
else if ( collection.getCompositeElement() != null ) {
return CollectionElementType.COMPOSITE_ELEMENT;
}
else if ( collection.getManyToMany() != null ) {
return CollectionElementType.MANY_TO_MANY;
}
else if ( collection.getOneToMany() != null ) {
return CollectionElementType.ONE_TO_MANY;
}
else if ( collection.getManyToAny() != null ) {
return CollectionElementType.MANY_TO_ANY;
}
else {
throw new AssertionFailure( "Unknown collection element type: " + collection );
}
}
private ManyToOneAttributeBinding makeManyToOneAttributeBinding(XMLManyToOneElement manyToOne,
EntityBinding entityBinding) {
ManyToOneAttributeBindingState bindingState =