我刚在网络应用中遇到以下MySQL错误
Too many tables; MySQL can only use 61 tables in a join
在执行Hibernate Search(版本5.5.2)查询时会发生这种情况,我不完全确定为什么需要很多连接。以下是我的实体模型的简化示例:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Profile {
Integer id;
@ManyToOne
RelatedEntityOne joinOne;
}
@Indexed
@Entity
public class BusinessProfile extends Profile {
@ManyToOne
RelatedEntityTwo joinTwo;
}
@Indexed
@Entity
public class UserProfile extends Profile {
@ManyToOne
RelatedEntityThree joinThree;
}
以下是执行查询的代码
FullTextEntityManager ftem = Search.getFullTextEntityManager(em);
FullTextQuery fullTextQuery = ftem.createFullTextQuery(myQuery, UserProfile.class);
List result = fullTextQuery.getResultList();
以下是生成的SQL
的示例SELECT *
FROM Profile root
LEFT OUTER JOIN BusinessProfile join_1 ON root.id = join_1.id
LEFT OUTER JOIN UserProfile join_2 ON root.id = join_2.id
LEFT OUTER JOIN RelatedEntityOne join_3 ON root.x = join_3.x
LEFT OUTER JOIN RelatedEntityTwo join_4 ON join_1.x = join_4.x
LEFT OUTER JOIN RelatedEntityThree join_5 ON join_2.x = join_5.x
WHERE root.id IN (...)
所以在这个简化的例子中有5个连接。如果我在父类Profile
上执行查询,那将是有意义的。但是我已经将子类UserProfile
传递给createFullTextQuery
方法,所以我希望生成的SQL看起来更像这样:
SELECT *
FROM UserProfile root
LEFT OUTER JOIN Profile join_1 ON root.id = join_1.id
LEFT OUTER JOIN RelatedEntityOne join_2 ON join_1.x = join_2.x
LEFT OUTER JOIN RelatedEntityThree join_3 ON root.x = join_3.x
WHERE root.id IN (...)
我不确定这是否是Hibernate,Hibernate Search,我自己的代码的问题,或者如果没有问题,一切都按预期运行。鉴于我们已经确定要使用哪个子表,我认为它没有任何理由加入兄弟表。
答案 0 :(得分:2)
我确认这是Hibernate Search中的一个错误。这是我刚创建的JIRA:https://hibernate.atlassian.net/browse/HSEARCH-2301。
我已经写过修复但我必须去,没有时间清理它;我今天晚些时候会发布PR。
感谢您发现此问题!
更新我们已将其修复为5.5.4.Final:http://in.relation.to/2016/06/29/Polishing-Polishing-And-More-Polishing-Hibernate-Search-5-5-4-Final/