如何使用mysql空安全相等运算符< =>在HQL中?
我想获取 Patient (POJO类)的联系号码不为空的记录.因此,我引用了此帖子.
在答案中指定了两种方式
I wanted to get records of Patient(POJO class) who's contact number is not null. 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);
从上面我写了下面可以成功运行的hql
From above I wrote below hql which runs successfully
from Patient p where p.contactNo is not null
但是,对于hql的第二种类型
But, for 2nd type of hql
from Patient p where not (p.contactNo <=> null)
引发异常
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: >
如何使用mysql 空安全相等运算符中的a> < => ?
How can I use mysql null safe equality operator <=> in HQL?
HQL是与MySQL不同的语言.HQL中不一定提供MySQL运算符.
HQL is a different language than MySQL. MySQL operators are not necessarily available in HQL.
话虽如此,您可以给Hibernate MySQL查询(前提是您的数据库是MySQL):
This being said, you can given Hibernate MySQL queries (provided your database is MySQL):
Query query = entityMangager.createNativeQuery("Some MySQL code");
List results = query.getResultList();
EntityManager 是一个接口来自 Java持久性API .Hibernate具有有关使用JPA的教程,但这是要点:
EntityManager is an interface from the Java Persistence API. Hibernate has a tutorial about using the JPA, but here are the main points:
要拥有一个实体管理器,您需要在类路径中的 META-INF/persistence.xml 文件.然后,在Java EE容器内,您将获得带有 @PersistenceContext 批注的此接口的实例:
In order to have an entity manager, you need META-INF/persistence.xml file in your classpath. Then, inside a Java EE container, you get an instance of this interface with the @PersistenceContext annotation:
@PersistenceContext(unitName = "persistenceUnit")
private EntityManager em;
在Java EE容器之外,您可以使用持久性类:
Outside a Java EE container, you can get one with the Persistence class:
EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
EntityManager em = factory.createEntityManager();
在两种情况下,"persistenceUnit" 必须是在 persistence.xml 文件中定义的持久性单元的名称.
In both case, "persistenceUnit" must be the name of a persistence unit defined in your persistence.xml file.