Hibernate implementation of the javax.persistence.metamodel.SingularAttribute interface (org.hibernate.ejb.metamodel.SingularAttributeImpl) always return false in the isAssociation method (hibernate 3.6.1).
So the JPA metamodel is unusable in some cases. Eg.:
Standard parent-child relation:
Code:
@Entity
public class ChildEntity {
private Long id;
private String name;
private ParentEntity parent;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column( name = "ID" )
public ID getId() { return this.id; }
public void setId( ID id ) { this.id = id; }
@Column( name = "NAME", nullable = false, length = 50 )
public String getName() { return this.name; }
public void setName( String name ) { this.name = name; }
@ManyToOne
@JoinColumn( name = "PARENT_ID", nullable = false )
public ParentEntity getParent() { return this.parent; }
public void setParent( ParentEntity parent ) { this.parent = parent; }
}
@Entity
public class ParentEntity {
private Long id;
private String name;
private Set<ChildEntity> childs = new HashSet<ChildEntity>();
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column( name = "ID" )
public ID getId() { return this.id; }
public void setId( ID id ) { this.id = id; }
@Column( name = "NAME", nullable = false, length = 50 )
public String getName() { return this.name; }
public void setName( String name ) { this.name = name; }
@OneToMany( mappedBy = "parent", cascade = { CascadeType.ALL } )
public Set<ChildEntity> getChilds() { return this.childs; }
public void setChilds( Set<ChildEntity> childs ) { this.childs = childs; }
}
Testing the parent-child relation through the metamodel:
Code:
...
EntityManager entityManager = this.entityManagerFactory.createEntityManager();
ManagedType< ? > managedTypePE = entityManager.getMetamodel().entity( ParentEntity.class );
Attribute<?, ?> attrPEChilds = managedTypePE.getAttribute( "childs" );
Assert.assertTrue( attrPEChilds.isCollection() ); // its OK
ManagedType< ? > managedTypeCE = entityManager.getMetamodel().entity( ChildEntity.class );
Attribute<?, ?> attrCEParent = managedTypeCE.getAttribute( "parent" ); // <<== it will be a SingularAttribute
Assert.assertTrue( attrCEParent.isAssociation() ); // <<== THIS WILL FAIL!!!
...