hibernate查询问题,如此接近和难解

hibernate查询问题,如此接近和难解,hibernate,collections,hql,Hibernate,Collections,Hql,模式: (伪代码) 我有一个bean,叫做BaseEntity @Entity class BaseEntity { @OneToMany @CascadeType.ALL List [Property] properties; //the use angled braces ommited for the stackoverflow editor to show up properly } 属性是另一个bean @Entity class Property { @Ma

模式: (伪代码)

我有一个bean,叫做BaseEntity

@Entity
class BaseEntity {
   @OneToMany @CascadeType.ALL
   List [Property] properties;
   //the use angled braces ommited for the stackoverflow editor to show up properly
}
属性是另一个bean

@Entity
class Property {
   @ManyToOne
   Category category;
   @OneToOne
   Value value;
}
Value实际上是一个具有Inheritance.SingleTable和NumericalValue和DateValue等子类的抽象类,以及(在抽象值类中)一个@ManyToOne基类型

目标是: 我正在尝试编写一个查询,该查询选择具有具有特定名称类别的属性的BaseEntity对象,并选择其中的几个对象,获取具有任何给定属性的对象,并在不存在的字段中获取null

尝试: 此查询不会运行。目前,数据库中有一个BaseEntity与之匹配,我多次得到foo的正确结果,它确实包含了这个结果,但同一个实体在bar字段中有许多值

此外,它需要五分钟的时间来运行,并且会阻碍其他使用数据库的人

想法:

当然,我考虑过只使用一些不同的方法,但这并不能解决运行所需的极端时间,我只是不太明白发生了什么

我希望你们,我的同龄人,能提出一个更好的查询方法。非常感谢你


我本来会评论的,但是这段代码对于评论框来说太长了。。。我运行了一个查询,这个查询不会太长以至于挂起,但是如果有更多的连接,它会挂起而不是完成。。。执行此查询需要几分钟

我从代码中运行了查询,并在hibernate属性中打开了show_sql

from properity where  property.category.name in ( 'FOO', 'BAR' )
and property.baseEntity.priKey = :priKey 

选择baseentity0.entityId作为col\u 0\u,选择property2.value\u valueId作为col\u 1\u 0,选择property4.value\u valueId作为col\u 2\u 0\u,选择property6.value\u valueId作为col\u 3\u 0,选择property8.value\u valueId作为col\u 4\u 0,选择property10.value\u valueId作为col\u 5\u 0,选择valueId作为valueId作为col\u 9,选择property8.value\u valueId作为col\u 4\u 0,选择property10.value\u valueId作为col\u.valueId作为col\u.valueId,选择valueId作为col\u.valueId作为col\u,value14_u.valueId为valueId9_3_uu,value15_uu.valueId为valueId9_4_u,value11_u.typeId为type6_9_0,value11_u.numericalValue为Numerical3_9_0,value11_u.textValue为textValue9_0,value11_11.dateValue为dateValue9_0,value11_11.value为entityId为value7_9_0,value11.DTYPE为Value9_9_0,value11.dUu9,value12_u.numericalValue作为numerica3_u9_u1_u,value12_u.textValue作为textValue9_u1_u,value12_u.value作为dateValue9_u1_u,value12_u.entityId作为value7_u9_u1_u,value12_u.DTYPE作为DTYPE9_u1_u,value13_u.type作为typeId作为type6_9_u2,value13_u.numericalValue作为NumericalValue3_u9_u2,value13_u.Textuu.value作为Dateu9_u值,value13_.value_entityId为value7_9_2_,value13_.DTYPE为DTYPE9_2_,value14_.type_typeId为type6_9_3_,value14_.numericalValue为Numerical3_9_3_,value14_.textValue为textValue9_3_,value14_.dateValue为dateValue9_3_,value14_.value_.entityId为value7_9_9_3_3,value14_.DTYPE为Typeu,value14_9,value15_u.numericalValue作为numerica3_9_4_u,value15_u.textValue作为textValue9_4_u,value15_u.dateValue作为dateValue9_4_u,value15_.value作为value7_9_4_u实体ID,value15\u0.d从BaseEntity baseentity0\uu内部联接BaseEntity\u属性属性1\u0上键入DTYPE9\u 4\u0.entityId=properties 1\u0.BaseEntity\u entityId内部联接属性2\u1\u0.properties\uPropertyId=property2\u0.propertyId内部联接值11\u2\u2\u2.Value\uValueId=valueId内部联接值BaseEntity\u Property properties3\uon baseentity0\u0.entityId=properties3\u0.BaseEntity\u entityId内部连接属性property4\uon properties3\u0.properties\uPropertyId=property4\u4.propertyId内部连接值valueId=value12\uId内部连接BaseEntity\uProperties5\uOnbaseentity0.entityId=properties 5.BaseEntity\u entityId内部连接属性Property 5.properties\u propertyId=property6.propertyId内部连接值value13\uOn property6.Value\u valueId=value13\uValueId内部连接BaseEntity\u属性属性7\uOn baseentity0.entityId=properties 7\uUser.BaseEntityId内部连接属性8\u关于属性7.properties\u propertyId=property8.propertyId内部连接值14\u关于属性8.Value\u valueId=value14\u.valueId内部连接BaseEntity\u属性属性9\u关于baseentity0\u.entityId=properties 9\u.BaseEntity\u entityId内部连接属性10\u关于属性9\u.properties\u属性yid=property10\u内部连接值属性10上的value15。valueId=value15。valueId,类别16,类别17,类别18,类别19,Category category20其中property2.categoryId=category16.categoryId和property4.categoryId=category17.categoryId和property6.categoryId=category18.categoryId和property8.categoryId=category19.categoryId和property10.categoryId=category20.categoryId和category16.CategoryName='/应变'和类别17.name='Item#'和类别18.name='THC#U结果'和类别19.name='CBD#U结果'和类别20.name='CBN#U结果'


好的,我想继续说,用两个join语句运行得非常好,而用五个语句运行得非常慢


挑选
entity.id作为entityId,
train.value作为名称,
item.value作为itemNum,
thc.值为thc,
cbd.value作为cbd,
cbn.作为cbn的值

从BaseEntity作为实体 将实体.properties作为应变连接 将entity.properties作为项目连接 将entity.properties作为thc连接 将entity.properties作为cbd连接 将entity.properties作为cbn连接 其中train.category.name='样本名称/菌株' 和item.category.name='item#' 和thc.category.name='thc_Result' 和cbd.category.name='cbd_Result' 和cbn.category.name='cbn_Result'


有没有关于更好的方法的建议,使用我愚蠢的模式会更快?

是否覆盖关联的默认获取类型?通常这会导致问题。您可以发布Hibernate生成的查询吗?

select entity from BaseEntity as entity join entity.properties as property where property.category.name in ( 'FOO','BAR' )
from properity where  property.category.name in ( 'FOO', 'BAR' )
and property.baseEntity.priKey = :priKey 
select entity.id as entityId, entity.type as entityType, p1.value as Vendor, p2.value as Weight 
from BaseEntity as entity 
join entity.properties as p1 
join entity.properties as p2 
where p1.category.name = 'Vendor' and p2.category.name = 'Total Weight'