Author: epbernard
Date: 2009-08-12 00:22:23 -0400 (Wed, 12 Aug 2009)
New Revision: 17272
Added:
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Person.java
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/metamodel/EntityTypeImpl.java
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/metamodel/MetamodelFactory.java
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/FoodItem.java
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/MetadataTest.java
Log:
EJB-456 add more test and fix isId false on IdClass attributes
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/metamodel/EntityTypeImpl.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/metamodel/EntityTypeImpl.java 2009-08-12
03:05:57 UTC (rev 17271)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/metamodel/EntityTypeImpl.java 2009-08-12
04:22:23 UTC (rev 17272)
@@ -94,7 +94,7 @@
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;
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/metamodel/MetamodelFactory.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/metamodel/MetamodelFactory.java 2009-08-12
03:05:57 UTC (rev 17271)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/metamodel/MetamodelFactory.java 2009-08-12
04:22:23 UTC (rev 17272)
@@ -20,6 +20,10 @@
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 @@
}
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;
}
Modified:
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/FoodItem.java
===================================================================
---
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/FoodItem.java 2009-08-12
03:05:57 UTC (rev 17271)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/FoodItem.java 2009-08-12
04:22:23 UTC (rev 17272)
@@ -3,6 +3,7 @@
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
+import javax.persistence.Version;
/**
* @author Emmanuel Bernard
@@ -11,6 +12,7 @@
public class FoodItem {
private Long id;
private String name;
+ private Long version;
@Id @GeneratedValue
public Long getId() {
@@ -21,6 +23,15 @@
this.id = id;
}
+ @Version
+ public Long getVersion() {
+ return version;
+ }
+
+ public void setVersion(Long version) {
+ this.version = version;
+ }
+
public String getName() {
return name;
}
Modified:
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/MetadataTest.java
===================================================================
---
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/MetadataTest.java 2009-08-12
03:05:57 UTC (rev 17271)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/MetadataTest.java 2009-08-12
04:22:23 UTC (rev 17272)
@@ -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 @@
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[] getAnnotatedClasses() {
return new Class[]{
Fridge.class,
- FoodItem.class
+ FoodItem.class,
+ Person.class
};
}
Added: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Person.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Person.java
(rev 0)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Person.java 2009-08-12
04:22:23 UTC (rev 17272)
@@ -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
+ */
+(a)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;
+ }
+ }
+}