diff --git a/entitymanager/src/main/java/org/hibernate/ejb/metamodel/EntityTypeImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/metamodel/EntityTypeImpl.java index c4103ff172..e08de30125 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/metamodel/EntityTypeImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/metamodel/EntityTypeImpl.java @@ -94,7 +94,7 @@ public class EntityTypeImpl extends ManagedTypeImpl implements EntityType< Set> attributes = new HashSet>(); while ( properties.hasNext() ) { attributes.add( - (SingularAttribute) MetamodelFactory.getAttribute( this, properties.next(), context ) + (SingularAttribute) MetamodelFactory.getAttribute( this, properties.next(), context, true ) ); } return attributes; diff --git a/entitymanager/src/main/java/org/hibernate/ejb/metamodel/MetamodelFactory.java b/entitymanager/src/main/java/org/hibernate/ejb/metamodel/MetamodelFactory.java index d6dd913189..52a8e88433 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/metamodel/MetamodelFactory.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/metamodel/MetamodelFactory.java @@ -20,6 +20,10 @@ import org.hibernate.mapping.Value; class MetamodelFactory { static Attribute getAttribute(ManagedType ownerType, Property property, MetadataContext metadataContext) { + return getAttribute( ownerType, property, metadataContext, false ); + } + + static Attribute getAttribute(ManagedType ownerType, Property property, MetadataContext metadataContext, boolean isId) { AttributeContext attrContext = getAttributeContext( property ); final Attribute attribute; if ( attrContext.isCollection() ) { @@ -44,11 +48,12 @@ class MetamodelFactory { } else { final Type attrType = getType( attrContext.getElementTypeStatus(), attrContext.getElementValue(), metadataContext ); - attribute = SingularAttributeImpl.create( ownerType, attrType ) + final SingularAttributeImpl.Builder 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; } diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/FoodItem.java b/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/FoodItem.java index 45593f066f..5580181286 100644 --- a/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/FoodItem.java +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/FoodItem.java @@ -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; } diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/MetadataTest.java b/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/MetadataTest.java index 003d28e60c..7a045ba73d 100644 --- a/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/MetadataTest.java +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/MetadataTest.java @@ -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 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 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 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 personType = factory.getMetamodel().entity( Person.class ); + assertFalse( personType.hasSingleIdAttribute() ); + final Set> ids = personType.getIdClassAttributes(); + assertNotNull( ids ); + assertEquals( 2, ids.size() ); + for (SingularAttribute localId : ids) { + assertTrue( localId.isId() ); + } + + final EntityType foodType = factory.getMetamodel().entity( FoodItem.class ); + assertTrue( foodType.hasVersionAttribute() ); + final SingularAttribute version = foodType.getVersion( Long.class ); + assertNotNull( version ); + assertTrue( version.isVersion() ); + + } + + public void testBasic() throws Exception { + final EntityType entityType = factory.getMetamodel().entity( Fridge.class ); final SingularAttribute 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 }; } diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Person.java b/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Person.java new file mode 100644 index 0000000000..87939ade90 --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Person.java @@ -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; + } + } +}