I tried to achieve Generation Gap Pattern with JPA entities. Here is the solution we choose ( <-- 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 ?
Thanks