Hibernate Search - MySQL 连接继承模型连接错误太多

Hibernate Search - MySQL error too many joins with Joined Inheritance model

我刚刚在我的网络应用程序中遇到以下 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 个连接。如果我在 parent class Profile 上执行查询,这将是有意义的。但是我已经将 child class 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、我自己的代码的问题,或者是否没有问题并且一切都按预期运行。鉴于我们已经确定要使用哪个 child table,我看不出有任何理由让它加入兄弟 table。

我确认这是 Hibernate Search 中的错误。这是我刚刚创建的 JIRA:https://hibernate.atlassian.net/browse/HSEARCH-2301 .

我已经编写了修复程序,但我必须离开并且没有时间清理它;今天晚些时候我会post PR。

感谢您发现这个问题!

更新 我们在 5.5 中修复了它。4.Final:http://in.relation.to/2016/06/29/Polishing-Polishing-And-More-Polishing-Hibernate-Search-5-5-4-Final/