简体   繁体   中英

Is it possible to fetch all child entities from a parent abstract entity?

I'm trying to figure out if I can make a method that fetches or finds all the entries in a central database I have.

My entities are as follows:

Nomination(Abstract) -> Type1Nomination, Type2Nomination, Type3Nomination, so on.

Nomination has a table is mapped to the DB also, and has the common properties/columns of every type of nomination. The rest is self-explanatory.

The way I see it, I would have to cast the types of nominations to the base class? And this would be done in a Service class (because you cannot make a DAO of an abstract class), am I correct?

You can do this in JPA/Hibernate and JPA inheritance. It should be as simple as querying the Nomination entity.

Select n From Nomination n

should return a list of abstract nomination objects.

I've seen folks using a type column in conjunction with the in keyword to get certain subgroups in your case something like

Select n from nomination where n.type in :types 

where types was a collection of "Type1Nomination" "Type2Nomination" but because these are sibling queries it can lead to some nasty LEFT OUTER JOIN queries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM