HHH-4720 - Improve javax.persistence.metamodel.Attribute support

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18310 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2009-12-21 23:34:10 +00:00
parent 3fd7b88524
commit bc0144f2eb
7 changed files with 926 additions and 325 deletions

View File

@ -218,6 +218,9 @@ public abstract class AbstractManagedType<X>
String name,
Class<Y> javaType) {
if ( attribute == null || ( javaType != null && !attribute.getBindableJavaType().equals( javaType ) ) ) {
if ( isPrimitiveVariant( attribute, javaType ) ) {
return;
}
throw new IllegalArgumentException(
attributeType + " named " + name
+ ( javaType != null ? " and of type " + javaType.getName() : "" )
@ -226,6 +229,38 @@ public abstract class AbstractManagedType<X>
}
}
@SuppressWarnings({ "SimplifiableIfStatement" })
protected <Y> boolean isPrimitiveVariant(SingularAttribute<?,?> attribute, Class<Y> javaType) {
if ( attribute == null ) {
return false;
}
Class declaredType = attribute.getBindableJavaType();
if ( declaredType.isPrimitive() ) {
return ( Boolean.class.equals( javaType ) && Boolean.TYPE.equals( declaredType ) )
|| ( Character.class.equals( javaType ) && Character.TYPE.equals( declaredType ) )
|| ( Byte.class.equals( javaType ) && Byte.TYPE.equals( declaredType ) )
|| ( Short.class.equals( javaType ) && Short.TYPE.equals( declaredType ) )
|| ( Integer.class.equals( javaType ) && Integer.TYPE.equals( declaredType ) )
|| ( Long.class.equals( javaType ) && Long.TYPE.equals( javaType ) )
|| ( Float.class.equals( javaType ) && Float.TYPE.equals( declaredType ) )
|| ( Double.class.equals( javaType ) && Double.TYPE.equals( declaredType ) );
}
if ( javaType.isPrimitive() ) {
return ( Boolean.class.equals( declaredType ) && Boolean.TYPE.equals( javaType ) )
|| ( Character.class.equals( declaredType ) && Character.TYPE.equals( javaType ) )
|| ( Byte.class.equals( declaredType ) && Byte.TYPE.equals( javaType ) )
|| ( Short.class.equals( declaredType ) && Short.TYPE.equals( javaType ) )
|| ( Integer.class.equals( declaredType ) && Integer.TYPE.equals( javaType ) )
|| ( Long.class.equals( declaredType ) && Long.TYPE.equals( javaType ) )
|| ( Float.class.equals( declaredType ) && Float.TYPE.equals( javaType ) )
|| ( Double.class.equals( declaredType ) && Double.TYPE.equals( javaType ) );
}
return false;
}
/**
* {@inheritDoc}
*/

View File

@ -41,7 +41,7 @@ public class EmbeddedTypeTest extends TestCase {
};
}
public void testSingularAttributeAccessByNameFailureExpected() {
public void testSingularAttributeAccessByName() {
// HHH-4702
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();

View File

@ -26,6 +26,7 @@ package org.hibernate.ejb.metamodel;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@ -112,7 +113,7 @@ public class Info implements java.io.Serializable {
zip = v;
}
@OneToOne(mappedBy = "info")
@OneToOne(mappedBy = "info") @JoinTable
public Spouse getSpouse() {
return spouse;
}

View File

@ -0,0 +1,60 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2009, 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.ejb.test.metadata;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
/**
* An entity that defines a @ManyToOne @JoinTable
* <p/>
* See HHH-4720 for details
*
* @author Steve Ebersole
*/
@Entity
public class JoinedManyToOneOwner {
private Long id;
private House house;
@Id
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToOne @JoinTable( name = "SOME_OTHER_TABLE" )
public House getHouse() {
return house;
}
public void setHouse(House house) {
this.house = house;
}
}

View File

@ -36,7 +36,6 @@ import javax.persistence.metamodel.PluralAttribute;
import javax.persistence.metamodel.MapAttribute;
import javax.persistence.metamodel.ListAttribute;
import javax.persistence.metamodel.MappedSuperclassType;
import javax.persistence.metamodel.CollectionAttribute;
import javax.persistence.metamodel.IdentifiableType;
import org.hibernate.ejb.test.TestCase;
@ -53,11 +52,23 @@ public class MetadataTest extends TestCase {
assertNotNull( entityType );
}
public void testLogicalManyToOne() throws Exception {
final EntityType<JoinedManyToOneOwner> entityType = factory.getMetamodel().entity( JoinedManyToOneOwner.class );
final SingularAttribute attr = entityType.getDeclaredSingularAttribute( "house" );
assertEquals( Attribute.PersistentAttributeType.MANY_TO_ONE, attr.getPersistentAttributeType() );
assertEquals( House.class, attr.getBindableJavaType() );
final EntityType<House> houseType = factory.getMetamodel().entity( House.class );
assertEquals( houseType.getBindableJavaType(), attr.getBindableJavaType() );
}
public void testEntity() throws Exception {
final EntityType<Fridge> fridgeType = factory.getMetamodel().entity( Fridge.class );
assertEquals( Fridge.class, fridgeType.getBindableJavaType() );
assertEquals( Bindable.BindableType.ENTITY_TYPE, fridgeType.getBindableType() );
assertNotNull( fridgeType.getDeclaredSingularAttribute( "temperature", Integer.class ) );
SingularAttribute<Fridge,Integer> wrapped = fridgeType.getDeclaredSingularAttribute( "temperature", Integer.class );
assertNotNull( wrapped );
SingularAttribute<Fridge,Integer> primitive = fridgeType.getDeclaredSingularAttribute( "temperature", int.class );
assertNotNull( primitive );
assertNotNull( fridgeType.getDeclaredSingularAttribute( "temperature" ) );
assertNotNull( fridgeType.getDeclaredAttribute( "temperature" ) );
final SingularAttribute<Fridge, Long> id = fridgeType.getDeclaredId( Long.class );
@ -112,19 +123,22 @@ public class MetadataTest extends TestCase {
"temperature",
Integer.class
);
assertEquals( Integer.class, singularAttribute.getBindableJavaType() );
// assertEquals( Integer.class, singularAttribute.getBindableJavaType() );
// assertEquals( Integer.class, singularAttribute.getType().getJavaType() );
assertEquals( int.class, singularAttribute.getBindableJavaType() );
assertEquals( int.class, singularAttribute.getType().getJavaType() );
assertEquals( Bindable.BindableType.SINGULAR_ATTRIBUTE, singularAttribute.getBindableType() );
assertFalse( singularAttribute.isId() );
assertFalse( singularAttribute.isOptional() );
assertFalse( entityType.getDeclaredSingularAttribute( "brand", String.class ).isOptional() );
assertEquals( Integer.class, singularAttribute.getType().getJavaType() );
assertEquals( Type.PersistenceType.BASIC, singularAttribute.getType().getPersistenceType() );
final Attribute<? super Fridge, ?> attribute = entityType.getDeclaredAttribute( "temperature" );
assertNotNull( attribute );
assertEquals( "temperature", attribute.getName() );
assertEquals( Fridge.class, attribute.getDeclaringType().getJavaType() );
assertEquals( Attribute.PersistentAttributeType.BASIC, attribute.getPersistentAttributeType() );
assertEquals( Integer.class, attribute.getJavaType() );
// assertEquals( Integer.class, attribute.getJavaType() );
assertEquals( int.class, attribute.getJavaType() );
assertFalse( attribute.isAssociation() );
assertFalse( attribute.isCollection() );
@ -306,7 +320,8 @@ public class MetadataTest extends TestCase {
Cattish.class,
Feline.class,
Garden.class,
Flower.class
Flower.class,
JoinedManyToOneOwner.class
};
}

View File

@ -53,7 +53,8 @@ public class StaticMetadataTest extends TestCase {
assertTrue( Animal_.id.isId() );
assertEquals( Long.class, Animal_.id.getJavaType() );
assertNotNull( Animal_.legNbr );
assertEquals( Integer.class, Animal_.legNbr.getJavaType() );
// assertEquals( Integer.class, Animal_.legNbr.getJavaType() );
assertEquals( int.class, Animal_.legNbr.getJavaType() );
// Cat (hierarchy)
assertNotNull( Cat_.id );
@ -72,9 +73,12 @@ public class StaticMetadataTest extends TestCase {
assertNotNull( Fridge_.temperature );
assertEquals( "temperature", Fridge_.temperature.getName() );
assertEquals( Fridge.class, Fridge_.temperature.getDeclaringType().getJavaType() );
assertEquals( Integer.class, Fridge_.temperature.getJavaType() );
assertEquals( Integer.class, Fridge_.temperature.getBindableJavaType() );
assertEquals( Integer.class, Fridge_.temperature.getType().getJavaType() );
// assertEquals( Integer.class, Fridge_.temperature.getJavaType() );
// assertEquals( Integer.class, Fridge_.temperature.getBindableJavaType() );
// assertEquals( Integer.class, Fridge_.temperature.getType().getJavaType() );
assertEquals( int.class, Fridge_.temperature.getJavaType() );
assertEquals( int.class, Fridge_.temperature.getBindableJavaType() );
assertEquals( int.class, Fridge_.temperature.getType().getJavaType() );
assertEquals( Bindable.BindableType.SINGULAR_ATTRIBUTE, Fridge_.temperature.getBindableType() );
assertEquals( Type.PersistenceType.BASIC, Fridge_.temperature.getType().getPersistenceType() );
assertEquals( Attribute.PersistentAttributeType.BASIC, Fridge_.temperature.getPersistentAttributeType() );