开发者

How to fetch classified objects with jpa(hibernate)?

开发者 https://www.devze.com 2023-03-25 21:25 出处:网络
there are 3 classes here: abstract class AbstractClass{@Column(\"type\") public int type;......} class A extends开发者_运维百科 AbstractClass{......}

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消