简体   繁体   English

在 joined inheritance 策略中查询基础实体怎么可能也获取子实体?

[英]How is it possible that querying base entity in joined inheritance strategy also fetches sub entities?

I have an abstract base class:我有一个抽象基数 class:

@Inheritance(strategy = InheritanceType.JOINED)
@Getter
@Setter
@Entity
@ToString
public abstract class BillingDetails {
 @javax.persistence.Id
 @GeneratedValue(strategy = GenerationType.SEQUENCE,
        generator = "pk_for_inheritance")
Long Id;
@NotNull
private String owner;
}

and one subclass extending base class和一个子类扩展基础 class

@Entity
@Getter
@Setter
@ToString(callSuper = true)
public class CreditCard extends BillingDetails{

 @Basic(optional = false)
 private String cardNumber;
 @Basic(optional = false)
 private LocalDate expDate;
 @Basic(optional = false)
 private String cardKey;
 } 

when I query against base Entity BillingDetails and print results like so:当我查询基本实体 BillingDetails 并像这样打印结果时:

 List<BillingDetails> details=billingDetailsRepository.findAll();
 details.forEach(System.out::println);

I get the following output:我得到以下 output:

CreditCard(super=BillingDetails(Id=1, owner=Mehmet Dogan), cardNumber=6145 1233 4577 2360, expDate=2022-05-03, cardKey=673)

My question is: Although I understand in joined strategy hibernates joins related base and sub tables,How is it possible that I can print properties of subclass CreditCard when my result list is of type BillingDetails and only Id and Owner properties are declared in my base class?我的问题是:虽然我知道在连接策略休眠中连接相关的基表和子表,但是当我的结果列表是 BillingDetails 类型并且在我的基 class 中只声明了 Id 和 Owner 属性时,我怎么可能打印子类 CreditCard 的属性?

What I have missed here was I think polymorphism.我在这里错过的是我认为多态性。 When I tried to get Class of results like当我试图获得 Class 的结果时

  List<BillingDetails> details=billingDetailsRepository.findAll();
  details.forEach(x-> System.out.println(x.getClass()));

I got the following output:我得到以下 output:

class com.rumlor.domainmodelmapping.models.inheritancemodels.CreditCard

so somehow I missed somewhere even if results are cast to List Of BillingDetails,Hibernate polymorphed every sub instance to base entity for me.所以不知何故我错过了某个地方,即使结果被投射到 BillingDetails 列表,Hibernate 为我将每个子实例变形为基础实体。 additional check:附加检查:

  CreditCard card= (CreditCard) details.get(0);
  System.out.println(card);

result:结果:

CreditCard(super=BillingDetails(Id=1, owner=Mehmet Dogan), cardNumber=6145 1233 4577 2360, expDate=2022-05-03, cardKey=673)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JPA中使用联接的子实体获取继承实体? - How to get inheritance entity by using joined sub entity in jpa? 使用JOINED策略进行JPA继承 - JPA Inheritance with JOINED strategy 加入继承策略 - Joined inheritance strategy 休眠连接策略继承和多态 - hibernate joined strategy inheritance and polymorphism Hibernate中的继承策略:使实体抽象并替换为子类 - Inheritance strategy in Hibernate: making an entity abstract and replacing with sub classes 如何在JPA /休眠继承策略Joined中删除父级和子级元素? - How to remove Parent and Child Element in JPA/hibernate inheritance strategy Joined? 模拟没有实际继承的JOINED继承策略 - Mock JOINED inheritance strategy without actual inheritance 如何使 Spring (Hibernate) 将单个表 inheritance 场景中的子实体实例化为它们的代理而不是基本实体代理? - How to make Spring (Hibernate) instantiate child entities in single table inheritance scenario as their proxies instead of base entity proxy? 使用 Hibernate JOINED 继承策略时从父实体查询/选择行 - Querying/Selecting rows from Parent Entity when using Hibernate JOINED inheritence strategy JPA与具有@Inheritance(strategy = InheritanceType.JOINED)的实体的一对多关联 - JPA one-to-many association to an entity with @Inheritance(strategy=InheritanceType.JOINED)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM