带有鉴别器列的对象的 Eclipselink 延迟加载问题
我们的应用程序中有以下层次结构:
@MappedSuperclass
public abstract class AbstractDemandOrMeasureBE {
}
@Entity
@Inheritance
@DiscriminatorColumn(name = "DISCRIMINATOR", discriminatorType = DiscriminatorType.INTEGER)
@Table(name = "V_VIEW2")
public abstract class AbstractDemandOrConcreteMeasureBE extends AbstractDemandOrMeasureBE {
@Column(name = "VC_ID")
private Long vcId;
}
@Entity
@DiscriminatorValue("2")
public class MinimalDemandBE extends AbstractDemandOrConcreteMeasureBE {
..
}
@Entity
@DiscriminatorValue("1")
@HasRelationsAnnotatedAsLazyLoaded
public class ValidationMeasureBE extends AbstractDemandOrConcreteMeasureBE {
..
}
在其他对象中,我尝试加载这样的实体:
@Table(name = "V_VIEW2")
public class VCBE extends SomeVeryAbstractBE {
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "VC_ID")
private List<ValidationMeasureBE> validationMeasures;
public transient static final String ATTRIBUTE_VALIDATION_MEASURES = "validationMeasures";
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "VC_ID")
private List<MinimalDemandBE> minimalDemands;
public transient static final String ATTRIBUTE_MINIMAL_DEMANDS = "minimalDemands";
有一个预编译查询来加载所有层次结构,它加载一些其他父对象。还有一个查询提示 - eclipselink.left-join-fetch=PP.VCBE.validationMeasures (如果将其更改为 eclipselink.left-join-fetch=PP.VCBE.minimalDemands,则加载最小需求,但验证措施(具有鉴别器 1 的条目)也被加载到最低需求集合中 - 但不应加载这些)。
现在,当执行查询时,validationMeasures 集合如果填充了对象,但所有这些对象实际上都是最小需求,并且在数据库中具有 2 作为鉴别器值。
执行的查询如下:
SELECT * FROM V_VIEW1 t1
LEFT OUTER JOIN V_VIEW0 t0 ON (t0.PP_D = t1.ID)
LEFT OUTER JOIN V_VIEW2 t2 ON (t2.VC_ID = t0.ID)
WHERE (((t1.ID = ?) AND (t1.HP_ID = ?))
AND t1.HP_IS IN (SELECT t3.ID FROM V_VIEW t3 WHERE (t3.HWPG_ID = ?)))
bind => [3 parameters bound]
正如我所看到的,查询中没有 DISCRIMINATOR 约束,为什么?
对这种行为有什么想法吗?我如何告诉 eclipselink 根据鉴别器值加载集合?
We have following hierarchy in our application:
@MappedSuperclass
public abstract class AbstractDemandOrMeasureBE {
}
@Entity
@Inheritance
@DiscriminatorColumn(name = "DISCRIMINATOR", discriminatorType = DiscriminatorType.INTEGER)
@Table(name = "V_VIEW2")
public abstract class AbstractDemandOrConcreteMeasureBE extends AbstractDemandOrMeasureBE {
@Column(name = "VC_ID")
private Long vcId;
}
@Entity
@DiscriminatorValue("2")
public class MinimalDemandBE extends AbstractDemandOrConcreteMeasureBE {
..
}
@Entity
@DiscriminatorValue("1")
@HasRelationsAnnotatedAsLazyLoaded
public class ValidationMeasureBE extends AbstractDemandOrConcreteMeasureBE {
..
}
In other object I am trying to load those entities like that:
@Table(name = "V_VIEW2")
public class VCBE extends SomeVeryAbstractBE {
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "VC_ID")
private List<ValidationMeasureBE> validationMeasures;
public transient static final String ATTRIBUTE_VALIDATION_MEASURES = "validationMeasures";
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "VC_ID")
private List<MinimalDemandBE> minimalDemands;
public transient static final String ATTRIBUTE_MINIMAL_DEMANDS = "minimalDemands";
There is a precompiled query to load all hierarchy, which load some other parent objects. There is also a hint for the query - eclipselink.left-join-fetch=PP.VCBE.validationMeasures (if this is changed to eclipselink.left-join-fetch=PP.VCBE.minimalDemands, then minimal demands are loaded, but validation measures (entries with discriminator 1) are also loaded into the minimal demands collection - but those should not be loaded).
Now, when query is executed validationMeasures collection if filled with objects, but all those object are actually minimal demands and have 2 as a discriminator value in the database.
The query, which gets executed is following:
SELECT * FROM V_VIEW1 t1
LEFT OUTER JOIN V_VIEW0 t0 ON (t0.PP_D = t1.ID)
LEFT OUTER JOIN V_VIEW2 t2 ON (t2.VC_ID = t0.ID)
WHERE (((t1.ID = ?) AND (t1.HP_ID = ?))
AND t1.HP_IS IN (SELECT t3.ID FROM V_VIEW t3 WHERE (t3.HWPG_ID = ?)))
bind => [3 parameters bound]
As I can see there is no DISCRIMINATOR constraint in the query, why?
Any ideas of such a behavior? And how can I tell eclipselink to load collection, depending on discriminator value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能否包含用于获取此 SQL 的 JPQL 查询和提示。
那么,您是说当您使用 join-fetch 而不是 left-join-fetch 时它可以工作?
这看起来可能是一个错误,即使用外连接时不包含继承鉴别器表达式。如果是这种情况,请为此记录错误并投票。
不过你的模型很奇怪。为什么要把两个子类分成两个独立的关系呢?拥有一个会更有效率。或者,如果您确实拆分它们,则应该使用不同的外键,而不是相同的外键。为两个不同的关系共享相同的外键可能不是一个好主意。
Can you include the JPQL query and hints you use to get this SQL.
So, you are saying it works when you use a join-fetch, but not a left-join-fetch?
This seems like it may be a bug, that the inheritance discriminator expression is not being included when using an outer join. If this is the case, please log a bug for this and vote for it.
Your model is very odd though. Why split the two subclasses into two separate relationships? Having one would be much more efficient. Or if you do split them, you should be using different foreign keys, not the same one. Sharing the same foreign key for two different relationships is probably not a good idea.