简体   繁体   English

Hibernate生成错误SQL,例如“。=”。

[英]Hibernate generates error SQL like “.=.”

I have 3 tables in Oracle DB which relationship is @ManyToMany. 我在Oracle DB中有3个表,它们之间的关系是@ManyToMany。 So I have 2 significant tables and one for mappings. 因此,我有2个重要的表和一个用于映射的表。

I create a classes with name (if you want I can show my classes) named Entities, Keywords (I understand that naming is not correct but this is not my project I only do optimizations). 我创建了一个名称为Entities,Keywords(如果您愿意的话,可以显示我的班级)的类(我知道命名不正确,但这不是我的项目,我只做优化)。

I use hibernate version 4.3.4. 我使用的是休眠版本4.3.4。

I write query like this: 我写这样的查询:

        session = HibernateUtil.getSessionFactory().openSession();
        String sql = "SELECT DISTINCT r FROM Rules r, Entities e " +
                " WHERE r.entities = e.rules " +
                "  AND e IN :entities ";

        Query query = session.createQuery(sql);
        query.setParameterList("entities", entitiesList);

        List<Rules> rulesList = query.list();

BUT! 但! Hibernate generate strange SQL Hibernate生成奇怪的SQL

Hibernate: 
select
    rules0_.rule_id as rule_id1_11_,
    rules0_.rule as rule2_11_ 
from
    rules rules0_,
    entities entities1_,
    rules_entities entities2_,
    entities entities3_,
    rules_entities rules4_,
    rules rules5_ 
where
    rules0_.rule_id=entities2_.rule_id 
    and entities2_.entity_id=entities3_.entity_id 
    and entities1_.entity_id=rules4_.entity_id 
    and rules4_.rule_id=rules5_.rule_id 
    and .=.
    and (
        entities1_.entity_id in (
            ? , ? , ? , ?
        )
    )

When I try to execute this query I receive that error: java.sql.SQLException: ORA-00936: missing expression 当我尝试执行此查询时,我收到该错误:java.sql.SQLException:ORA-00936:缺少表达式

When I copy this query to OracleDevepoler he didn`t like this expression " and .=. ". 当我将此查询复制到OracleDevepoler时,他不喜欢该表达式“ 和。=。 ”。 Without that query executes correct. 没有该查询,将执行正确。

What am I doing wrong ? 我究竟做错了什么 ?

Maybe you used bad join in your query? 也许您在查询中使用了错误连接? From context i conclude that you should use something like that: 根据上下文,我得出结论,您应该使用类似的方法:

"SELECT DISTINCT r FROM Rules r inner join r.entities e " +
                "  WHERE e IN :entities ";

I think the correct query could be 我认为正确的查询可能是

select distinct e.rules from Entities where e.entityId in :entities

This is if Keywords is your join table and you have a collection of rules in Entities 这是如果“关键字”是您的联接表,并且您在“实体”中有一组规则

If it isn't, can you show the mappings please, it could help. 如果不是,请显示映射,这可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM