【发布时间】:2017-12-25 22:20:42
【问题描述】:
我的域模型中有一个层次结构,由类描述:
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class BaseEntity {
@Id
private Long id;
// other fields
}
@DiscriminatorValue(value = "Individual")
public class IndividualEntity extends BaseEntity {
// fields
}
@DiscriminatorValue(value = "Branch")
public class BranchEntity extends BaseEntity {
// fields
}
我正在获取这样的对象:
Specification<BaseEntity> specification = createSpecification();
BaseEntity entity = baseRepository.findOne(specification);
(我正在使用弹簧数据)
问题是Hibernate返回代理对象(我理解),但代理是BaseEntity,而不是正确的子类(它的类是BaseEntity_$$_jvsted9_26,因此entity instanceof IndividualEntity是假的)。
有趣的是,并非所有对象都作为代理返回。
我在循环中获取实体(普通事务),其中一些以正常形式返回(即IndividualEntity/BranchEntity),一些作为代理返回。
如果我更改机制,以便每次提取都在单独的事务中完成 - 根本不会返回任何代理对象。
我知道我可以解开该代理(例如 here),但是这种行为的原因是什么(对我来说有点奇怪),我可以避免吗?
【问题讨论】:
-
我在直接使用 spring-data Repository 类时遇到了同样的问题。
-
你为什么在乎?
标签: java hibernate orm spring-data-jpa