How to fetch classified objects with jpa(hibernate)?
there are 3 classes here:
abstract class AbstractClass{@Column("type") public int type;......}
class A extends开发者_运维百科 AbstractClass{......}
class B extends AbstractClass{......}
in database, when type=1 means A and type=2 means B. So how can i fetch these objects as AbstractClass, and the concrete class is A when type=1 and B when type=2 ?
Thanks very much.
It depends on your database structure. If you are using one table for the whole class hierarchy, check out Hibernates documentation on table per class hierarchy. If you are using one table to store the common fields in the AbstractClass and a table for each subclass you can have a look at the documentation on Mixing table per class hierarchy with table per subclass. In both cases it comes down to defining a descriminator (in your case the type column).
You need to use the "one table per class hierarchy" inheritance mapping, described here. The explicit mapping of the discriminator column is possible, but not mandatory.
精彩评论