fix NPE when identifier is not a property

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17260 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2009-08-11 00:53:08 +00:00
parent 0f8a36a269
commit 558fcd635a
1 changed files with 24 additions and 12 deletions

View File

@ -49,18 +49,23 @@ public class EntityTypeImpl<X> extends ManagedTypeImpl<X> implements EntityType<
} }
private <A> SingularAttribute<X, A> buildIdAttribute(PersistentClass persistentClass) { private <A> SingularAttribute<X, A> buildIdAttribute(PersistentClass persistentClass) {
final Property identifierProperty = persistentClass.getIdentifierProperty(); if ( hasIdentifierProperty ) {
@SuppressWarnings( "unchecked" ) final Property identifierProperty = persistentClass.getIdentifierProperty();
Class<A> idClass = identifierProperty.getType().getReturnedClass(); @SuppressWarnings( "unchecked" )
final Type<A> attrType = new BasicTypeImpl<A>( idClass, Class<A> idClass = identifierProperty.getType().getReturnedClass();
identifierProperty.isComposite() ? final Type<A> attrType = new BasicTypeImpl<A>( idClass,
PersistenceType.EMBEDDABLE : identifierProperty.isComposite() ?
PersistenceType.BASIC); PersistenceType.EMBEDDABLE :
return SingularAttributeImpl.create(this, attrType ) PersistenceType.BASIC);
.property(identifierProperty) return SingularAttributeImpl.create(this, attrType )
//.member( null ) //TODO member .property(identifierProperty)
.id() //.member( null ) //TODO member
.build(); .id()
.build();
}
else {
return null;
}
} }
private <A> SingularAttribute<X, A> buildVersionAttribute(PersistentClass persistentClass) { private <A> SingularAttribute<X, A> buildVersionAttribute(PersistentClass persistentClass) {
@ -101,11 +106,16 @@ public class EntityTypeImpl<X> extends ManagedTypeImpl<X> implements EntityType<
public <Y> SingularAttribute<? super X, Y> getId(Class<Y> type) { public <Y> SingularAttribute<? super X, Y> getId(Class<Y> type) {
//TODO check that type and id.getJavaType() are related //TODO check that type and id.getJavaType() are related
checkId();
@SuppressWarnings( "unchecked") @SuppressWarnings( "unchecked")
final SingularAttribute<? super X, Y> result = ( SingularAttribute<? super X, Y> ) id; final SingularAttribute<? super X, Y> result = ( SingularAttribute<? super X, Y> ) id;
return result; return result;
} }
private void checkId() {
if ( ! hasSingleIdAttribute() ) throw new IllegalArgumentException("This is an @IdClass");
}
public <Y> SingularAttribute<? super X, Y> getVersion(Class<Y> type) { public <Y> SingularAttribute<? super X, Y> getVersion(Class<Y> type) {
//TODO check that type and version.getJavaType() are related //TODO check that type and version.getJavaType() are related
@SuppressWarnings( "unchecked") @SuppressWarnings( "unchecked")
@ -114,6 +124,7 @@ public class EntityTypeImpl<X> extends ManagedTypeImpl<X> implements EntityType<
} }
public <Y> SingularAttribute<X, Y> getDeclaredId(Class<Y> type) { public <Y> SingularAttribute<X, Y> getDeclaredId(Class<Y> type) {
checkId();
//TODO check that type and id.getJavaType() are related //TODO check that type and id.getJavaType() are related
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final SingularAttribute<X, Y> result = ( SingularAttribute<X, Y> ) id; final SingularAttribute<X, Y> result = ( SingularAttribute<X, Y> ) id;
@ -147,6 +158,7 @@ public class EntityTypeImpl<X> extends ManagedTypeImpl<X> implements EntityType<
} }
public Type<?> getIdType() { public Type<?> getIdType() {
checkId();
return id.getType(); return id.getType();
} }