users@jpa-spec.java.net

[jpa-spec users] Question about JPA metamodel, Inheritance and PluralAttribute

From: Yann Le Moigne <ylemoigne_at_javatic.fr>
Date: Sun, 04 Nov 2012 18:50:28 +0100

I tried to achieve Generation Gap Pattern with JPA entities. Here is
the solution I setup ( <-- are inheritance)

BaseEntity <-- EntityGenerated <-- Entity

The EntityGenerated type is abstract and mapped with @MappedSuperclass,
all field are generated with correct mapping annotation, relation point
to the concrete subclass, not the Generated one.

The Entity is a concrete type, generated only if the class doesn't
exist, initially there is just the class declaration annotated with
@Entity. Other mapping attributes such as the @Table, etc are in a
generated orm.xml.

Now, when we generate the jpa static metamodel (using hibernate or
openjpa metamodel generator), the generated classes look like :

public class BaseEntity_ {
     public static volatile
SingularAttribute<PersistentDomainObject,Long> id;
     public static volatile
SingularAttribute<PersistentDomainObject,Long> timeStamp;
}

public class UserGenerated_ extends BaseEntity_ {
     public static volatile SetAttribute<UserGenerated,Group> groups;
}

public class User_ extends UserGenerated_ {
}

If I want to use User_ in a jpa criteria query, I'll do something like
:

CriteriaQuery<User> query = criteriaBuilder.createQuery(User.class);
Root<User> root = query.from(User.class);
query.where(root.get(User_.groups).in(paramGroups));
But It won't compile.... User_.groups is of type SetAttribute and the
jpa path api for the get method is : "<E, C extends
java.util.Collection<E>> Expression<C> get(PluralAttribute<X, C, E>
collection);"

(In comparaison, the get method for the singular attribute is "<Y>
Path<Y> get(SingularAttribute<? super X, Y> attribute)" witch work
better)

So, now, the questions are :

Why the metamodel generators generate the class for MappedSuperclass as
there is no way to query it directly ?, attribute and relations for
superclass should be defined in each subclass (where X is of subclass
type) ?

Why the jpa criteria Path api doesn't define the get method for plural
attribut as "get(PluralAttribute<? super X, C, E> collection)" ?

How can I achieve the Generation Gap Pattern on JPA entity without
giving up criteria query ?

(For now, the User_ is generated by our domain generator with something
like : "public static volatile SetAttribute<User, Group> userGroups =
(SetAttribute) UserGenerated_.userGroups;")

Thanks for any answer.