Hi guys,
I have an entity with a parent/children relation. One of the requirements was to promote the children whenever the parent is being deleted so the new parent of the children will be their grandfather.
When I try to use the bellow approach an error is being raised:
Error:
Code:
Hibernate: update ITEMS, set PARENT_ITEM_ID=PARENT_ITEM_ID where PARENT_ITEM_ID=?
WARN [SocketListener0-8] - JDBCExceptionReporter - SQL Error: 1064, SQLState: 42000
ERROR [SocketListener0-8] - JDBCExceptionReporter - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set PARENT_ITEM_ID=PARENT_ITEM_ID where PARENT_ITEM_ID=16' at line 1
ERROR [SocketListener0-8] - RequestCycle - could not execute update query
org.hibernate.exception.SQLGrammarException: could not execute update query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:84)
at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:396)
at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:259)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1141)
at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:94)
at ca.infonium.tasks.model.dao.impl.HibernateItemDao.promoteChildren(HibernateItemDao.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:280)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:187)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:154)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:210)
at $Proxy34.promoteChildren(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.wicket.proxy.LazyInitProxyFactory$JdkHandler.invoke(LazyInitProxyFactory.java:376)
at org.apache.wicket.proxy.$Proxy43.promoteChildren(Unknown Source)
at ca.infonium.tasks.wicket.items.panels.tree.ItemBox.deleteAndPromoteChildren(ItemBox.java:202)
at ca.infonium.tasks.wicket.items.panels.tree.ItemBoxActionPanel$DeleteChildrenConfirmationWindow.onCancel(ItemBoxActionPanel.java:116)
at ca.infonium.tasks.wicket.utils.confirmation.ConfirmationWindow.cancel(ConfirmationWindow.java:111)
at ca.infonium.tasks.wicket.utils.confirmation.ConfirmationWindow$ConfirmationPanel$2.onEvent(ConfirmationWindow.java:74)
at org.apache.wicket.ajax.AjaxEventBehavior.respond(AjaxEventBehavior.java:165)
at org.apache.wicket.ajax.AbstractDefaultAjaxBehavior.onRequest(AbstractDefaultAjaxBehavior.java:210)
at org.apache.wicket.request.target.component.listener.BehaviorRequestTarget.processEvents(BehaviorRequestTarget.java:97)
at org.apache.wicket.request.AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:90)
at org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:975)
at org.apache.wicket.RequestCycle.step(RequestCycle.java:1048)
at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1127)
at org.apache.wicket.RequestCycle.request(RequestCycle.java:489)
at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:248)
at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:122)
at org.mortbay.jetty.servlet.WebApplicationHandler$CachedChain.doFilter(WebApplicationHandler.java:821)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
at org.mortbay.jetty.servlet.WebApplicationHandler$CachedChain.doFilter(WebApplicationHandler.java:821)
at org.mortbay.jetty.servlet.WebApplicationHandler.dispatch(WebApplicationHandler.java:471)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:568)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1530)
at org.mortbay.jetty.servlet.WebApplicationContext.handle(WebApplicationContext.java:633)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1482)
at org.mortbay.http.HttpServer.service(HttpServer.java:909)
at org.mortbay.http.HttpConnection.service(HttpConnection.java:820)
at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:986)
at org.mortbay.http.HttpConnection.handle(HttpConnection.java:837)
at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:245)
at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:357)
at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:534)
Caused by: java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set PARENT_ITEM_ID=PARENT_ITEM_ID where PARENT_ITEM_ID=16' at line 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2994)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:936)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1166)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1082)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1067)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:101)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:75)
... 52 more
NamedQuery:
Code:
@org.hibernate.annotations.NamedQuery(
name = "promoteChildren",
query = "UPDATE Item " +
"SET parentItem = parentItem.parentItem " +
"WHERE parentItem = :oldParent "
)
NamedQuery usage:
Code:
this.getSession()
.getNamedQuery("promoteChildren")
.setEntity("oldParent", oldParent)
.executeUpdate();
Entity Class:
Code:
@Entity
@Table(
name="ITEMS",
uniqueConstraints =
@UniqueConstraint(columnNames = {"NAME"})
)
@org.hibernate.annotations.NamedQueries(
{
@org.hibernate.annotations.NamedQuery(
name = "promoteChildren",
query = "UPDATE Item " +
"SET parentItem = parentItem.parentItem " +
"WHERE parentItem = :oldParent "
)
}
)
public class Item {
private long _id;
private String _name;
private String _description;
private Item _parentItem;
private List<Item> _childItems = new ArrayList<Item>();
// ********************** Accessor Methods ********************** //
@Id @GeneratedValue
@Column(name = "ITEM_ID")
public long getId() {
return _id;
}
public void setId(long id) {
_id = id;
}
@Column(name = "NAME", length = 50, nullable = false)
public String getName() {
return _name;
}
public void setName(String name) {
_name = name;
}
@Column(name = "DESCRIPTION", length = 4000, nullable = true)
public String getDescription() {
return _description;
}
public void setDescription(String description) {
_description = description;
}
@OneToMany(mappedBy = "parentItem", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@org.hibernate.annotations.OrderBy(clause = "ITEM_ID asc")
public List<Item> getChildItems() {
return _childItems;
}
public void setChildItems(List<Item> childItems) {
_childItems = childItems;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PARENT_ITEM_ID", nullable = true)
@org.hibernate.annotations.ForeignKey(name = "FK_ITEM_PARENT_ID")
public Item getParentItem() {
return _parentItem;
}
public void setParentItem(Item parentItem) {
_parentItem = parentItem;
}
}
[/code]