在连接继承策略中查询基础实体怎么可能也获取子实体?
How is it possible that querying base entity in joined inheritance strategy also fetches sub entities?
我有一个抽象基础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;
}
和一个子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;
}
当我查询基本实体 BillingDetails 并像这样打印结果时:
List<BillingDetails> details=billingDetailsRepository.findAll();
details.forEach(System.out::println);
我得到以下输出:
CreditCard(super=BillingDetails(Id=1, owner=Mehmet Dogan), cardNumber=6145 1233 4577 2360, expDate=2022-05-03, cardKey=673)
我的问题是:
虽然我在加入策略休眠中了解加入相关的基表和子表,但是当我的结果列表的类型为 BillingDetails 并且在我的基中只声明了 Id 和 Owner 属性时,我怎么可能打印 subclass CreditCard 的属性class ?
我在这里错过的是我认为多态性。当我试图获得 Class 的结果时,例如
List<BillingDetails> details=billingDetailsRepository.findAll();
details.forEach(x-> System.out.println(x.getClass()));
我得到以下输出:
class com.rumlor.domainmodelmapping.models.inheritancemodels.CreditCard
因此,即使将结果转换为 List Of BillingDetails,Hibernate 也将每个子实例多态化为我的基本实体,但我还是错过了某个地方。
附加检查:
CreditCard card= (CreditCard) details.get(0);
System.out.println(card);
结果:
CreditCard(super=BillingDetails(Id=1, owner=Mehmet Dogan), cardNumber=6145 1233 4577 2360, expDate=2022-05-03, cardKey=673)
我有一个抽象基础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;
}
和一个子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;
}
当我查询基本实体 BillingDetails 并像这样打印结果时:
List<BillingDetails> details=billingDetailsRepository.findAll();
details.forEach(System.out::println);
我得到以下输出:
CreditCard(super=BillingDetails(Id=1, owner=Mehmet Dogan), cardNumber=6145 1233 4577 2360, expDate=2022-05-03, cardKey=673)
我的问题是: 虽然我在加入策略休眠中了解加入相关的基表和子表,但是当我的结果列表的类型为 BillingDetails 并且在我的基中只声明了 Id 和 Owner 属性时,我怎么可能打印 subclass CreditCard 的属性class ?
我在这里错过的是我认为多态性。当我试图获得 Class 的结果时,例如
List<BillingDetails> details=billingDetailsRepository.findAll();
details.forEach(x-> System.out.println(x.getClass()));
我得到以下输出:
class com.rumlor.domainmodelmapping.models.inheritancemodels.CreditCard
因此,即使将结果转换为 List Of BillingDetails,Hibernate 也将每个子实例多态化为我的基本实体,但我还是错过了某个地方。 附加检查:
CreditCard card= (CreditCard) details.get(0);
System.out.println(card);
结果:
CreditCard(super=BillingDetails(Id=1, owner=Mehmet Dogan), cardNumber=6145 1233 4577 2360, expDate=2022-05-03, cardKey=673)