java - Query update using hibernate runs into QueryException : Unable to resolve path -
i have following entities, , need update field in specific field , removeddate null. following code returns exception.
@entity public class cart implements serializable { @id @generatedvalue private long id; @onetomany(cascade = cascadetype.all) @lazycollection(lazycollectionoption.false) private list<cartitem> items; public cart() { } getters , setters } @entity public class cartitem { @id @generatedvalue private long id; @manytoone private product pro; @temporal(javax.persistence.temporaltype.timestamp) private date addeddate; @temporal(javax.persistence.temporaltype.timestamp) private date removeddate; getters , setters }
hibernate code 1
query query = session.createquery("update cartitem set removeddate = :currentdatetime " + " cartitem.id in (select cart.items.id cart" + " cart.id = :cartid" + " , cart.items.pro.id = :pro" + " , cart.items.removeddate null)"); query.setparameter("currentdatetime", dt.getcurrentdatetime()); query.setparameter("cartid", cartid); query.setparameter("pro", proid); int result = query.executeupdate();
exception of code 1
severe: org.hibernate.queryexception: unable resolve path [cartitem.id], unexpected token [cartitem] [update com.myproject.cartitem set removeddate = :currentdatetime cartitem.id in (select cart.items.id com.myproject.cart cart.id = :cartid , cart.items.pro.id = :proid , cart.items.removeddate null))] @ org.hibernate.hql.internal.ast.tree.identnode.resolveasnakedcomponentpropertyreflhs(identnode.java:245) @ org.hibernate.hql.internal.ast.tree.identnode.resolve(identnode.java:110) @ org.hibernate.hql.internal.ast.tree.dotnode.resolvefirstchild(dotnode.java:177) @ org.hibernate.hql.internal.ast.hqlsqlwalker.lookupproperty(hqlsqlwalker.java:577) @ org.hibernate.hql.internal.antlr.hqlsqlbasewalker.addrexpr(hqlsqlbasewalker.java:4719)
hibernate code 2
query query = session.createquery("update cartitem set removeddate = :currentdatetime " + " id in (select items.id cart" + " id = :cartid" + " , items.pro.id = :pro" + " , items.removeddate null)");
exception of code 2
severe: org.hibernate.queryexception: illegal attempt dereference collection [{synthetic-alias}{non-qualified-property-ref}items] element property reference [id] [update com.myproject.cartitem set removeddate = :currentdatetime id in (select items.id com.myproject.cart id = :cartid , items.pro.id = :pro , items.removeddate null)] @ org.hibernate.hql.internal.ast.tree.dotnode$1.buildillegalcollectiondereferenceexception(dotnode.java:68) @ org.hibernate.hql.internal.ast.tree.dotnode.checklhsisnotcollection(dotnode.java:550) @ org.hibernate.hql.internal.ast.tree.dotnode.resolve(dotnode.java:246) @ org.hibernate.hql.internal.ast.tree.fromreferencenode.resolve(fromreferencenode.java:118) @ org.hibernate.hql.internal.ast.tree.fromreferencenode.resolve(fromreferencenode.java:114)
why don't make association bidirectional?
add cartitem
entity:
@manytoone private cart cart;
set mappedby
on cartitem
fied in cart
:
@onetomany(cascade = cascadetype.all, mappedby="cart") @lazycollection(lazycollectionoption.false) private list<cartitem> items;
the resulting hql simpler (and should work):
"update cartitem c set c.removeddate = :currentdatetime " + " c.cart.id = :cartid" + " , c.pro.id = :pro" + " , c.removeddate null"
Comments
Post a Comment