[hibernate-commits] Hibernate SVN: r17260 - core/trunk/entitymanager/src/main/java/org/hibernate/ejb/metamodel.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Aug 10 20:53:08 EDT 2009


Author: epbernard
Date: 2009-08-10 20:53:08 -0400 (Mon, 10 Aug 2009)
New Revision: 17260

Modified:
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/metamodel/EntityTypeImpl.java
Log:
fix NPE when identifier is not a property

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-11 00:26:13 UTC (rev 17259)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/metamodel/EntityTypeImpl.java	2009-08-11 00:53:08 UTC (rev 17260)
@@ -49,18 +49,23 @@
 	}
 
 	private <A> SingularAttribute<X, A> buildIdAttribute(PersistentClass persistentClass) {
-		final Property identifierProperty = persistentClass.getIdentifierProperty();
-		@SuppressWarnings( "unchecked" )
-		Class<A> idClass = identifierProperty.getType().getReturnedClass();
-		final Type<A> attrType = new BasicTypeImpl<A>( idClass,
-														identifierProperty.isComposite() ?
-																PersistenceType.EMBEDDABLE :
-																PersistenceType.BASIC);
-		return SingularAttributeImpl.create(this, attrType )
-										.property(identifierProperty)
-										//.member( null ) //TODO member
-										.id()
-										.build();
+		if ( hasIdentifierProperty ) {
+			final Property identifierProperty = persistentClass.getIdentifierProperty();
+			@SuppressWarnings( "unchecked" )
+			Class<A> idClass = identifierProperty.getType().getReturnedClass();
+			final Type<A> attrType = new BasicTypeImpl<A>( idClass,
+															identifierProperty.isComposite() ?
+																	PersistenceType.EMBEDDABLE :
+																	PersistenceType.BASIC);
+			return SingularAttributeImpl.create(this, attrType )
+											.property(identifierProperty)
+											//.member( null ) //TODO member
+											.id()
+											.build();
+		}
+		else {
+			return null;
+		}
 	}
 
 	private <A> SingularAttribute<X, A> buildVersionAttribute(PersistentClass persistentClass) {
@@ -101,11 +106,16 @@
 
 	public <Y> SingularAttribute<? super X, Y> getId(Class<Y> type) {
 		//TODO check that type and id.getJavaType() are related
+		checkId();
 		@SuppressWarnings( "unchecked")
 		final SingularAttribute<? super X, Y> result = ( SingularAttribute<? super X, Y> ) id;
 		return result;
 	}
 
+	private void checkId() {
+		if ( ! hasSingleIdAttribute() ) throw new IllegalArgumentException("This is an @IdClass");
+	}
+
 	public <Y> SingularAttribute<? super X, Y> getVersion(Class<Y> type) {
 		//TODO check that type and version.getJavaType() are related
 		@SuppressWarnings( "unchecked")
@@ -114,6 +124,7 @@
 	}
 
 	public <Y> SingularAttribute<X, Y> getDeclaredId(Class<Y> type) {
+		checkId();
 		//TODO check that type and id.getJavaType() are related
 		@SuppressWarnings("unchecked")
 		final SingularAttribute<X, Y> result = ( SingularAttribute<X, Y> ) id;
@@ -147,6 +158,7 @@
 	}
 
 	public Type<?> getIdType() {
+		checkId();
 		return id.getType();
 	}
 



More information about the hibernate-commits mailing list