JPQL / HQL:选择OR失败

时间:2014-05-20 17:08:55

标签: java hql jpql

我的查询有些问题,但不知道究竟在哪里 此查询返回,例如,3 Change objects:

select c from Change c where c.entityOne.serial = 'TEST3'

此查询返回,例如,2更改对象:

select c from Change c where c.entityTwo.serial = 'TEST3'

但是这个查询返回0,当我期望3到5之间的东西时。

select c from Change c where (c.entityOne.serial = 'TEST3' or c.entityTwo.serial = 'TEST3')

我哪里错了?如果您需要更多代码,请询问。

重新测试:
很抱歉有很多代码,但需要重现此问题。

Change.java

@Entity
public class Change {
    private long id;
    private EntityOne entityOne;
    private EntityTwo entityTwo;

    @Id
    @GeneratedValue
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @ManyToOne
    public EntityOne getEntityOne() {
        return entityOne;
    }

    public void setEntityOne(EntityOne entityOne) {
        this.entityOne = entityOne;
    }

    @ManyToOne
    public EntityTwo getEntityTwo() {
        return entityTwo;
    }

    public void setEntityTwo(EntityTwo entityTwo) {
        this.entityTwo = entityTwo;
    }
}

EntityOne.java和EntityTwo.java(只需重命名该类)

@Entity
public class EntityOne {
    private long id;
    private String serial;

    @Id
    @GeneratedValue
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getSerial() {
        return serial;
    }

    public void setSerial(String serial) {
        this.serial = serial;
    }
}

Test.java - 运行它。

public class Test {
    private static EntityManagerFactory buildFactory() {
        try {
            return Persistence.createEntityManagerFactory("migration-tool");
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        EntityManagerFactory emf = buildFactory();
        EntityManager em = emf.createEntityManager();
        Query q = em.createQuery("select c from Change c where (c.entityOne.serial = 'TEST3' or c.entityTwo.serial = 'TEST3')");
        List<Change> changeRecords =  q.getResultList();
        System.out.println(changeRecords.size());
        emf.close();
    }
}

的persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
    <persistence-unit name="migration-tool" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>test.Change</class>
        <class>test.EntityOne</class>
        <class>test.EntityTwo</class>
        <properties>
            <property name="hibernate.search.autoregister_listeners" value="false"/>
            <property name="hibernate.cache.region.factory_class"
                      value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/>
            <property name="hibernate.cache.use_query_cache" value="true"/>
            <property name="hibernate.connection.url" value="jdbc:postgresql://127.0.0.1:5434/test"/>
            <property name="hibernate.connection.username" value="postgres"/>
            <property name="hibernate.connection.password" value="123456"/>
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        </properties>
    </persistence-unit>
</persistence>

pom.xml中的依赖项

<dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.1-901.jdbc4</version>
</dependency>
<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>persistence-api</artifactId>
    <version>1.0</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.4.0.GA</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>3.4.0.GA</version>
</dependency>
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-core</artifactId>
    <version>2.6.0</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.4.2</version>
</dependency>

用SQL脚本填写数据。

CREATE TABLE entityone
(
  id bigint NOT NULL,
  serial character varying(255),
  PRIMARY KEY (id)
);

CREATE TABLE entitytwo
(
  id bigint NOT NULL,
  serial character varying(255),
  PRIMARY KEY (id)
);

CREATE TABLE change
(
  id bigint NOT NULL,
  entityone_id bigint,
  entitytwo_id bigint,
  PRIMARY KEY (id),
  FOREIGN KEY (entityone_id) REFERENCES entityone(id),
  FOREIGN KEY (entitytwo_id) REFERENCES entitytwo(id)
);

insert into entityone values (1, 'TEST3');
insert into entityone values (2, 'TEST1');
insert into entityone values (3, 'TEST2');
insert into entityone values (4, 'TEST3');

insert into entitytwo values (1, 'TEST3');
insert into entitytwo values (2, 'TEST3');
insert into entitytwo values (3, 'TEST4');
insert into entitytwo values (4, 'TEST3');

insert into change values (1, 1, NULL);
insert into change values (2, 2, NULL);
insert into change values (3, 3, NULL);
insert into change values (4, 4, NULL);
insert into change values (5, NULL, 1);
insert into change values (6, NULL, 2);
insert into change values (7, NULL, 3);
insert into change values (8, NULL, 4);

1 个答案:

答案 0 :(得分:1)

写下评论的答案:

问题是NULL值。 Chapter 16.4的Hibernate文档(v 4.3)声明:

  

16.4。连接语法的形式

     

HQL支持两种形式的关联加入:隐式和显式。

     

上一节中显示的查询都使用显式形式,即在from子句中显式使用join关键字的位置。这是推荐的表格。

     

隐式表单不使用join关键字。相反,使用点符号“解除引用”关联。隐式连接可以出现在任何HQL子句中。生成的SQL语句中的内部联接中的隐式联接结果。

     
    

来自Cat as cat,其中cat.mate.name喜欢'%s%'

  

我已经把最相关的部分加粗了。这说明你的点符号将被展开到内连接中,并且由于键为空 - 你的实体不会出现在连接中。
Hibernate执行的SQL也显示了这一点(下面为了可读性而美化):

select change  
from Change change, EntityOne entityone, EntityTwo entitytwo
where 
change.entityOne_id=entityone.id and
change.entityTwo_id=entitytwo.id and  
(
    ((change.entityOne_id is not null) and entityone.serial='TEST3') or 
    ((change.entityTwo_id is not null) and entitytwo.serial='TEST3')
)

不幸的是,点符号不允许您获得所需的结果。您必须自己创建连接,如上面的文档第16.3章所述。示例:

SELECT c
FROM Change AS c
    LEFT JOIN Change.entityOne as entityOne
    LEFT JOIN Change.entityTwo as entityTwo
WHERE
    entityOne.serial = "TEST3" OR entityTwo.serial = "TEST3"

注意:我没有Hibernate的工作设置,你可能需要在上面添加额外的空值检查