问题
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);
From above I wrote below hql which runs successfully
from Patient p where p.contactNo is not null
But, for 2nd type of 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?
回答1:
HQL is a different language than MySQL. MySQL operators are not necessarily available in HQL.
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 is an interface from the Java Persistence API. Hibernate has a tutorial about using the JPA, but here are the main points:
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;
Outside a Java EE container, you can get one with the Persistence class:
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.
来源:https://stackoverflow.com/questions/19245502/how-can-i-use-mysql-null-safe-equality-operator-in-hql