简体   繁体   English

如何在HQL中使用mysql空安全相等运算符<=>?

[英]How can I use mysql null safe equality operator <=> in HQL?

I wanted to get records of Patient (POJO class) who's contact number is not null. 我想获取Patient (POJO类)的联系号码不为空的记录。 So, I referred this post . 所以,我提到了这篇文章
In the answer two ways are specified 在答案中指定了两种方式

SELECT * 
FROM table 
WHERE YourColumn IS NOT NULL;   

SELECT *
FROM table 
WHERE NOT (YourColumn <=> NULL); 

From above I wrote below hql which runs successfully 从上面我写了下面的hql,它可以成功运行

from Patient p where p.contactNo is not null    

But, for 2nd type of hql 但是,对于第二种类型的hql

from Patient p where not (p.contactNo <=> null)

throws Exception 抛出异常

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: >  

How can I use mysql null safe equality operator <=> in HQL? 如何在HQL中使用mysql 空安全相等运算符 <=>

HQL is a different language than MySQL. HQL是与MySQL不同的语言。 MySQL operators are not necessarily available in HQL. HQL中不一定提供MySQL运算符。

This being said, you can given Hibernate MySQL queries (provided your database is MySQL): 话虽如此,您可以给Hibernate MySQL查询(前提是您的数据库是MySQL):

Query query = entityMangager.createNativeQuery("Some MySQL code");
List results = query.getResultList();

EntityManager is an interface from the Java Persistence API . EntityManagerJava Persistence API的接口 Hibernate has a tutorial about using the JPA , but here are the main points: Hibernate提供了有关使用JPA教程 ,但主要要点如下:

In order to have an entity manager, you need META-INF/persistence.xml file in your classpath. 为了拥有实体管理器,您需要在类路径中使用META-INF/persistence.xml文件。 Then, inside a Java EE container, you get an instance of this interface with the @PersistenceContext annotation: 然后,在Java EE容器内,您将获得带有@PersistenceContext批注的此接口的实例:

@PersistenceContext(unitName = "persistenceUnit")
private EntityManager em;

Outside a Java EE container, you can get one with the Persistence class: 在Java EE容器之外,可以使用Persistence类获得一个:

EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
EntityManager em = factory.createEntityManager();

In both case, "persistenceUnit" must be the name of a persistence unit defined in your persistence.xml file. 在这两种情况下, "persistenceUnit"必须是在persistence.xml文件中定义的持久性单元的名称。

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

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