哪种hibernate查询语言更有效率

时间:2014-11-16 13:23:20

标签: java hibernate

根据hibernate doc

来自cat as cat,其中cat.id = 123

来自cat as cat,cat.mate.id = 69

第二个查询是有效的,不需要表连接。

我认为第一个查询不需要表连接,第二个查询需要内连接。 如果我错了,请纠正我。

1 个答案:

答案 0 :(得分:0)

这些是将从您的HQL生成的SQL示例

第一个(from Cat as cat where cat.id = 123):

select c.* from Cat c where c.id=123;
// no joins - OK

第二个(from Cat as cat where cat.mate.id = 69):

select c.* from Cat c where c.mate_id=69;
// no joins (because cat.mate.id is the identifier in Mate)
// that's why it's efficient

如果你有这个HQL:"from Cat as cat where cat.mate.name = 'mate'" SQL看起来像:

select c.* from Cat c join Mate m on c.mate_id = m.id where m.name='mate';

c.mate_id是一个foregn键,引用了Mate的id

相关问题