I am faced with very strange problem, and can't find a way to resolve it. I wrote the following HQL in intellij's HQL Console
update NewsEntity ne set ne.main=false where ne.main=true
StackTrace
java.lang.IllegalArgumentException: node to traverse cannot be null!
at org.hibernate.hql.ast.util.NodeTraverser.traverseDepthFirst(NodeTraverser.java:31)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:254)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
in RemoteSessionImpl.createQuery(RemoteSessionImpl.java:50)
in RemoteUtil.executeWithClassLoader(RemoteUtil.java:122)
in RemoteUtil$2$1.invoke(RemoteUtil.java:81)
in HibernateEngine.createQuery(HibernateEngine.java:142)
When I write a delete query I don't have the same problem.
I have based this query around the following article HQL update query. But, can't seem to figure out why I am still having this problem.
The idea of an ORM like Hibernate is to avoid such update queries.
You've just to load the object from the db, change the field and then Hibernate will update the object for out.
Query query = session.createQuery("from NewsEntity ne where ne.main = true");
NewsEntity newsEntity = (NewsEntity) query.uniqueResult();
newsEntity.setMain=false;
//Hibernate will update the object
If there're more objects:
List<NewsEntity> newsEntities = (List<NewsEntity>) query.list();
for(NewsEntity entity : newsEntities)
{
entity.setMain=false;
}
Hibernate also supports DML statements, so to do an update query you should do:
Query query = session.createQuery("your update query");
int result = query.executeUpdate();
Related
I have defined this method
#Query("select cs from CenterStudy cs where cs.id in (?1)")
#EntityGraph(value = "centerStudyAndNotifiedUsers", type = EntityGraph.EntityGraphType.LOAD)
List<CenterStudy> findAllByIdsWithCenterAndUsers(List<Long> ids);
The list with the ids is not null, nor empty, but I always get the following exception:
java.lang.NullPointerException
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:67)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:613)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1900)
I'm pretty sure it has something to do with the way the IN clause it's defined.
Any suggestions are well welcomed!
It's a common issue, a bug in Hibernate which you can find at :
NullPointer when combining JPQL query with in clause and #NamedEntityGraph where it says that:
When you combine a JPQL query with an in clause and a #NamedEntityGraph
you get a NullPointerException in org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:67) when you execute the query.
So you would have to remove the #EntityGraph annotation here in order to fix this problem.
public List<DealInfo> getDealInfos(List<String> dealIds) {
String queryStr = "SELECT NEW com.admin.entity.DealInfo(deal.url, deal.url, deal.url, deal.url, deal.price, deal.value) " + "FROM Deal AS deal where deal.id in :inclList";
TypedQuery<DealInfo> query = em.createQuery(queryStr, DealInfo.class);
query.setParameter("inclList", dealIds);
return query.getResultList();
}
Works with JPA 2
I've faced this issue before. I fixed it using named parameteres instead of positional parameters.
Example:
"Select p from product where p.id in(?)" -- not working
replaced by:
"Select p from product where p.id in(:idList)" -- OK
I would like to know how to use prepared statement for insert query. Generally for select query I am using in following way.
Query query = JPA.em()
.createNativeQuery("select item_status from item_details where box_id=:boxnumber");
query.setParameter("boxnumber", boxNumber);
But when I am using insert query I am unable to use in the above way.
Query query = JPA.em()
.createNativeQuery("insert into item_details values(':item_status')");
query.setParameter("item_status", itemstat);
I am getting error like
java.lang.IllegalArgumentException:
org.hibernate.QueryParameterException: could not locate named parameter [item_status]
at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:368) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:72) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
please any one help me to sort out this issue. Thanks in advance
I could't tested but could you try this;
Query query = JPA.em().createNativeQuery("insert into item_details(item_status) values(?)") .setParameter(1, itemstat).executeUpdate();
To combine multiple columns as one,
I found one answer
SELECT id,CONCAT_WS(',', field_1, field_2, field_3, field_4) list
FROM `table`;
This query working fine in SQL but it gives me error in HQL:
Error is .
(java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode )
please help me to find out what wrong i did, help me to know how to use CONCAT_WS() IN HQL
below how i written my HQL query
SELECT C1._URI,C1.HEALTH_FACILITY,C1.DISTRICT,CONCAT_WS(',', C1.BLOCKS_OF_BHUBRI, C1.BLOCKS_OF_GOLAGHAT, C1.BLOCKS_OF_HAILAKANDI) as Block_name
FROM GapAnalysisWashInHealthFacilitiesCore C1
any help will appreciate
CONCAT_WS is a function specific to mySql. HQL is a generic language and not aware of native SQL functions and syntax. If you really need the function, then you should use Hibernate's API for native SQL.
Session session = ...;
Query query = session.createSQLQuery("
SELECT id,CONCAT_WS(',', field_1, field_2, field_3, field_4) Block_name FROM `table`");
List result = query.list();
Then you may like to have a look at Result Transformers to get result as list of GapAnalysisWashInHealthFacilitiesCore objects.
I'm pretty new to hibernate and I was trying it out in one of my applications. I chose to use annotation session factory bean and my editor generated entity classes for each table from the DB which had named queries. hibernateTemplate.findByAll worked fine. But when I tried hibernateTemplate.findByNamedQuery("findById", "<some_id>"), it gave an error: java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based. After a bit of googling, tried out multiple solutions:
Changed the namedQuery that was generated by editor from : #NamedQuery(name = "Table.findById", query = "SELECT u FROM Table t WHERE t.id = :id"), to : #NamedQuery(name = "Table.findById", query = "SELECT u FROM Table t WHERE t.id = ?") but got the same error.
Tried using hibernateTemplate.findByNamedParam but ended up getting error: java.lang.IllegalArgumentException: node to traverse cannot be null!
I can use hibernateTemplate.find() to achieve this but how do I use findByNamedQuery/Param methods to achieve the same since the documentation says these methods may be used to fetch records based on a field?
I am trying to join multiple table using hibernate but its not working for me can someone please help me out.
I tried Criteria that was not working then thought of using query even that is not working
My code looks like
final Session session = getSession();
String query = "SELECT r.REFERRER_ID from REFERRAL_PAYMENT_INFO r, SIGNUP_REFERRAL s";
Query q = session.createQuery(query);
List list = q.list();
I am getting this error -
"Caused by: org.hibernate.hql.ast.QuerySyntaxException:
REFERRAL_PAYMENT_INFO is not mapped [SELECT r.REFERRER_ID from REFERRAL_PAYMENT_INFO
r, SIGNUP_REFERRAL s]"
You must use the classes (entities) you mapped in HQL queries. If you want to use normal SQL, then you have to call session.createSQLQuery().
Look at the documentation for hibernate session:
http://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/Session.html