EJB-456 add more test and fix isId false on IdClass attributes

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17272 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2009-08-12 04:22:23 +00:00
parent b1d17f31dd
commit 6bf5ed7302
5 changed files with 154 additions and 10 deletions

View File

@ -94,7 +94,7 @@ public class EntityTypeImpl<X> extends ManagedTypeImpl<X> implements EntityType<
Set<SingularAttribute<? super X, ?>> attributes = new HashSet<SingularAttribute<? super X, ?>>();
while ( properties.hasNext() ) {
attributes.add(
(SingularAttribute<? super X, ?>) MetamodelFactory.getAttribute( this, properties.next(), context )
(SingularAttribute<? super X, ?>) MetamodelFactory.getAttribute( this, properties.next(), context, true )
);
}
return attributes;

View File

@ -20,6 +20,10 @@ import org.hibernate.mapping.Value;
class MetamodelFactory {
static<X, Y, V, K> Attribute<X, Y> getAttribute(ManagedType<X> ownerType, Property property, MetadataContext metadataContext) {
return getAttribute( ownerType, property, metadataContext, false );
}
static<X, Y, V, K> Attribute<X, Y> getAttribute(ManagedType<X> ownerType, Property property, MetadataContext metadataContext, boolean isId) {
AttributeContext attrContext = getAttributeContext( property );
final Attribute<X, Y> attribute;
if ( attrContext.isCollection() ) {
@ -44,11 +48,12 @@ class MetamodelFactory {
}
else {
final Type<Y> attrType = getType( attrContext.getElementTypeStatus(), attrContext.getElementValue(), metadataContext );
attribute = SingularAttributeImpl.create( ownerType, attrType )
final SingularAttributeImpl.Builder<X, Y> xyBuilder = SingularAttributeImpl.create( ownerType, attrType )
// .member( ); //TODO member
.property( property )
.persistentAttributeType( attrContext.getElementAttributeType() )
.build();
.persistentAttributeType( attrContext.getElementAttributeType() );
if (isId) xyBuilder.id();
attribute = xyBuilder.build();
}
return attribute;
}

View File

@ -3,6 +3,7 @@ package org.hibernate.ejb.test.metadata;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Version;
/**
* @author Emmanuel Bernard
@ -11,6 +12,7 @@ import javax.persistence.GeneratedValue;
public class FoodItem {
private Long id;
private String name;
private Long version;
@Id @GeneratedValue
public Long getId() {
@ -21,6 +23,15 @@ public class FoodItem {
this.id = id;
}
@Version
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public String getName() {
return name;
}

View File

@ -1,9 +1,11 @@
package org.hibernate.ejb.test.metadata;
import java.util.Set;
import javax.persistence.EntityManagerFactory;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Bindable;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.Type;
import org.hibernate.ejb.test.TestCase;
@ -19,11 +21,42 @@ public class MetadataTest extends TestCase {
assertNotNull( entityType );
}
public void testBindable() throws Exception {
EntityManagerFactory emf = factory;
final EntityType<Fridge> entityType = emf.getMetamodel().entity( Fridge.class );
assertEquals( Fridge.class, entityType.getBindableJavaType() );
assertEquals( Bindable.BindableType.ENTITY_TYPE, entityType.getBindableType() );
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 ) );
assertNotNull( fridgeType.getDeclaredSingularAttribute( "temperature" ) );
assertNotNull( fridgeType.getDeclaredAttribute( "temperature" ) );
final SingularAttribute<Fridge, Long> id = fridgeType.getDeclaredId( Long.class );
assertNotNull( id );
assertTrue( id.isId() );
assertEquals( Fridge.class.getName(), fridgeType.getName() );
assertEquals( Long.class, fridgeType.getIdType().getJavaType() );
assertTrue( fridgeType.hasSingleIdAttribute() );
assertFalse( fridgeType.hasVersionAttribute() );
assertEquals( Type.PersistenceType.ENTITY, fridgeType.getPersistenceType() );
//TODO IdClass
final EntityType<Person> personType = factory.getMetamodel().entity( Person.class );
assertFalse( personType.hasSingleIdAttribute() );
final Set<SingularAttribute<? super Person,?>> ids = personType.getIdClassAttributes();
assertNotNull( ids );
assertEquals( 2, ids.size() );
for (SingularAttribute<? super Person,?> localId : ids) {
assertTrue( localId.isId() );
}
final EntityType<FoodItem> foodType = factory.getMetamodel().entity( FoodItem.class );
assertTrue( foodType.hasVersionAttribute() );
final SingularAttribute<? super FoodItem, Long> version = foodType.getVersion( Long.class );
assertNotNull( version );
assertTrue( version.isVersion() );
}
public void testBasic() throws Exception {
final EntityType<Fridge> entityType = factory.getMetamodel().entity( Fridge.class );
final SingularAttribute<? super Fridge,Integer> singularAttribute = entityType.getDeclaredSingularAttribute(
"temperature",
Integer.class
@ -39,7 +72,8 @@ public class MetadataTest extends TestCase {
public Class[] getAnnotatedClasses() {
return new Class[]{
Fridge.class,
FoodItem.class
FoodItem.class,
Person.class
};
}

View File

@ -0,0 +1,94 @@
package org.hibernate.ejb.test.metadata;
import java.io.Serializable;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Entity;
/**
* @author Emmanuel Bernard
*/
@IdClass(Person.PersonPK.class)
@Entity
public class Person {
private String firstName;
private String lastName;
private Short age;
@Id
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Id
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Short getAge() {
return age;
}
public void setAge(Short age) {
this.age = age;
}
public static class PersonPK implements Serializable {
private String firstName;
private String lastName;
@Id
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Id
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
PersonPK personPK = ( PersonPK ) o;
if ( firstName != null ? !firstName.equals( personPK.firstName ) : personPK.firstName != null ) {
return false;
}
if ( lastName != null ? !lastName.equals( personPK.lastName ) : personPK.lastName != null ) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = firstName != null ? firstName.hashCode() : 0;
result = 31 * result + ( lastName != null ? lastName.hashCode() : 0 );
return result;
}
}
}