JPA static metamodel with @MappedSuperclass (with a bit of Generation Gap Pattern)

不问归期 提交于 2020-01-03 21:03:11

问题


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

来源:https://stackoverflow.com/questions/13048743/jpa-static-metamodel-with-mappedsuperclass-with-a-bit-of-generation-gap-patter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!