EclipseLink: Query to MappedSuperclass fails

半腔热情 提交于 2019-12-05 19:44:21

A mapped superclass is not an entity and does not allow querying, persisting, or relationships to the superclass (see Mapped Superclasses).

This is confirmed in Apache OpenJPA 2.0 User's Guide:

1.3. Mapped Superclass

A mapped superclass is a non-entity class that can define persistent state and mapping information for entity subclasses. Mapped superclasses are usually abstract. Unlike true entities, you cannot query a mapped superclass, pass a mapped superclass instance to any EntityManager or Query methods, or declare a persistent relation with a mapped superclass target. You denote a mapped superclass with the MappedSuperclass marker annotation.

...

Note

OpenJPA allows you to query on mapped superclasses. A query on a mapped superclass will return all matching subclass instances. OpenJPA also allows you to declare relations to mapped superclass types; however, you cannot query across these relations.

So, while OpenJPA allows your query, this is beyond JPA. Don't expect it to work with any JPA provider.

Can you confirm that you simply want a separate query run for every entity subclass and the results aggregated together?

This is similar to TABLE_PER_CLASS querying.

I have built an extension for EclipseLink in the past that can handle such querying and we can investigate adding it to the project if there is community interest and we can nail down the requirements.

Doug

That code also works fine with DataNucleus as a JPA implementation. Being able to query for instances of a mapped superclass, whilst being beyond the JPA spec, is a very reasonable thing to require, and we believe in providing such things. After all the persistence provider knows which subclasses there are of that mapped superclass so makes little sense to not allow the user such a facility.

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