-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: how to update only some columns of a table?
PostPosted: Tue Nov 29, 2005 9:44 am 
Newbie

Joined: Tue Nov 29, 2005 3:56 am
Posts: 4
Hi
I am using hibernate 3 with WSAD IE 5.1 and DB2 on S/390 7.1.1
I have a table with around 30 columns with most of them being not null. It has a composite key made of 3 columns.

I need to update some columns of the table which user enters in a JSP. I am using struts and the action class calls the updateEdit() function in the DAO from the the service proxy class SomeDetailsDAOproxy for the SomeDetailsDAO class.

I want to execute a query something similar to this using hibernate :

update SOME_DETAILS set ENTERPRISENUMBER=?, STATUS=?, GROUPRESPONSIBLE=? where COUNTRY=? and LEDGERCODE=? and NUMBER=?
But when i use the saveOrUpdate() it expects me to provide it with the values for all the columns of the table. This results in the error "[IBM][CLI Driver][DB2] SQL0407N Assignment of a NULL value to a NOT NULL column "" is not allowed. SQLSTATE=23502".
Is there a way i can avoid it? i dont want to be passing all 30 values to update just a few columns. Is there any other way to do this? any help is apprecited, Tx!

============================


The SomeDetails.hbm.xml looks like this :

<hibernate-mapping>
<class name="myproj.dao.SomeDetailsDAO" table="SOME_DETAILS">
<composite-id>
<key-property name="country" column="COUNTRY" type="string"/>
<key-property name="ledgerCode" column="LEDGERCODE" type="string"/>
<key-property name="number" column="NUMBER" type="string"/>
</composite-id>

<property name="enterpriseNumber" column="ENTERPRISENUMBER" type="string"/>
<property name="status" column="STATUS" type="string"/>
<property name="groupResponsible" column="GROUPRESPONSIBLE" type="string"/>
...
...
</class>
</hibernate-mapping>

=====================
the code for updateEdit in SomeDetailsDAO.java is (the variables on the right hand side are passed to the function) -

SomeDetailsDAO someDAO = new SomeDetailsDAO();
someDAO.country = country;
someDAO.ledgerCode = ledgercode;
someDAO.Number = number;
someDAO.status = status;
session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(someDAO);
tx.commit();
session.close();
======================

Full stack trace of the exception that occurs:

[11/29/05 18:27:35:666 IST] 7c127ab5 SystemOut O Hibernate: update SOME_DETAILS set ENTERPRISENUMBER=?, STATUS=?, GROUPRESPONSIBLE=?, ..... where COUNTRY=? and LEDGERCODE=? and NUMBER=?
[11/29/05 18:27:36:628 IST] 7c127ab5 JDBCException W org.hibernate.util.JDBCExceptionReporter SQL Error: -407, SQLState: 23502
[11/29/05 18:27:36:648 IST] 7c127ab5 JDBCException E org.hibernate.util.JDBCExceptionReporter [IBM][CLI Driver][DB2] SQL0407N Assignment of a NULL value to a NOT NULL column "" is not allowed. SQLSTATE=23502

[11/29/05 18:27:36:658 IST] 7c127ab5 AbstractFlush E org.hibernate.event.def.AbstractFlushingEventListener Could not synchronize database state with session
[11/29/05 18:27:36:668 IST] 7c127ab5 AbstractFlush E org.hibernate.event.def.AbstractFlushingEventListener TRAS0014I: The following exception was logged org.hibernate.exception.ConstraintViolationException: could not update: [myproj.dao.SomeDetailsDAO#component[country,ledgerCode,Number]{country=788, Number=00013 , ledgerCode=01 }]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:2000)
at org.hibernate.persister.entity.BasicEntityPersister.updateOrInsert(BasicEntityPersister.java:1909)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:2149)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:75)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:137)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at myproj.dao.BaseDAO.saveOrUpdateTable(BaseDAO.java:65)
at myproj.dao.SomeDetailsDAO.updateEdit(SomeDetailsDAO.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java(Compiled Code))
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java(Compiled Code))
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
at org.apache.wsif.providers.java.WSIFOperation_Java.executeRequestResponseOperation(WSIFOperation_Java.java:1208)
at myproj.db2.proxy.SomeDetailsDAOProxy.execute(SomeDetailsDAOProxy.java:2386)
at myproj.db2.proxy.SomeDetailsDAOProxy.updateEdit(SomeDetailsDAOProxy.java:2655)
at myproj.actions.EditSubmitAction.execute(EditSubmitAction.java:52)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java:110)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java:174)
at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:313)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java:116)
at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)
at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java:42)
at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java:40)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:76)
at myproj.filters.UserValidationFilter.doFilter(UserValidationFilter.java:59)
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:132)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:71)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:1010)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:592)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:204)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java:125)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:286)
at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:71)
at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:182)
at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java:334)
at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java:56)
at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:615)
at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:439)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java(Compiled Code))
Caused by: COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2] SQL0407N Assignment of a NULL value to a NOT NULL column "" is not allowed. SQLSTATE=23502

at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(Unknown Source)
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(Unknown Source)
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.check_return_code(Unknown Source)
at COM.ibm.db2.jdbc.app.DB2PreparedStatement.execute2(Unknown Source)
at COM.ibm.db2.jdbc.app.DB2PreparedStatement.executeUpdate(Unknown Source)
at com.ibm.ws.rsadapter.jdbc.WSJdbcPreparedStatement.executeUpdate(WSJdbcPreparedStatement.java:471)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:1982)
... 51 more
.
org.hibernate.exception.ConstraintViolationException: could not update: [myproj.dao.SomeDetailsDAO#component[country,ledgerCode,number]{country=788, number=00013 , ledgerCode=01 }]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:2000)
at org.hibernate.persister.entity.BasicEntityPersister.updateOrInsert(BasicEntityPersister.java:1909)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:2149)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:75)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:137)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at myproj.dao.BaseDAO.saveOrUpdateTable(BaseDAO.java:65)
at myproj.dao.SomeDetailsDAO.updateEdit(SomeDetailsDAO.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java(Compiled Code))
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java(Compiled Code))
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
at org.apache.wsif.providers.java.WSIFOperation_Java.executeRequestResponseOperation(WSIFOperation_Java.java:1208)
at myproj.db2.proxy.SomeDetailsDAOProxy.execute(SomeDetailsDAOProxy.java:2386)
at myproj.db2.proxy.SomeDetailsDAOProxy.updateEdit(SomeDetailsDAOProxy.java:2655)
at myproj.actions.EditSubmitAction.execute(EditSubmitAction.java:52)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java:110)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java:174)
at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:313)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java:116)
at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)
at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java:42)
at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java:40)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:76)
at myproj.filters.UserValidationFilter.doFilter(UserValidationFilter.java:59)
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:132)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:71)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:1010)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:592)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:204)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java:125)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:286)
at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:71)
at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:182)
at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java:334)
at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java:56)
at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:615)
at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:439)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java(Compiled Code))
Caused by: COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2] SQL0407N Assignment of a NULL value to a NOT NULL column "" is not allowed. SQLSTATE=23502

at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(Unknown Source)
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(Unknown Source)
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.check_return_code(Unknown Source)
at COM.ibm.db2.jdbc.app.DB2PreparedStatement.execute2(Unknown Source)
at COM.ibm.db2.jdbc.app.DB2PreparedStatement.executeUpdate(Unknown Source)
at com.ibm.ws.rsadapter.jdbc.WSJdbcPreparedStatement.executeUpdate(WSJdbcPreparedStatement.java:471)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:1982)
... 51 more

[11/29/05 18:27:36:748 IST] 7c127ab5 SystemOut O HibernateException : org.hibernate.exception.ConstraintViolationException: could not update: [myproj.dao.SomeDetailsDAO#component[country,ledgerCode,number]{country=788, number=00013 , ledgerCode=01 }]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 9:50 am 
Senior
Senior

Joined: Mon Aug 22, 2005 5:45 am
Posts: 146
you need to specify this info in your mapping config:

<property update="false" ....

_________________
Please don't forget to give credit, if my posting helped to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 9:53 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
You can also use the dynamic-update attribute on the class mapping, which [is supposed to] update only the columns you changed since you loaded the object. I haven't used it myself so can't comment. Check the documentation, because there are some important notes about setting this particular attribute.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 4:45 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
You could just use the HQL UPDATE statement

http://www.hibernate.org/hib_docs/v3/re ... tch-direct

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject: i cant get dynamic-update="true" to work
PostPosted: Wed Nov 30, 2005 6:03 am 
Newbie

Joined: Tue Nov 29, 2005 3:56 am
Posts: 4
Hi
thanks for the answers.. i also checked the documentation (which i could have done before posting the question :-/ ) but it does not seem to be working for me.
When i add the property dynamic-update="true" to the class, there is no change, the update query still shows with all the columns. And when i add update="false" for the columns i dont want to update, it does not update it at all. I dont see the update query in the console. In any case, using the update="false" is not an option for me since i will be updating different sets of the 30 columns with different queries. am i missing something?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 6:12 am 
Newbie

Joined: Tue Nov 29, 2005 3:56 am
Posts: 4
I did try the HQL update. It gives me this error

11/28/05 11:38:14:070 IST] 3a12b99b PARSER E org.hibernate.hql.PARSER *** ERROR: SOME_DETAILS is not mapped.
[11/28/05 11:38:14:871 IST] 3a12b99b WebGroup E SRVE0026E: [Servlet Error]-[]: java.lang.NullPointerException

i tried to create an object of the DAO class and used that instead of the table name SOME_DETAILS in the query, get the same error.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 7:22 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi

Can you expose the HQL Query?

I have done specific Column Update using HQL.

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 7:44 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
How about this?

String hql = "update SomeDetailsDAO set enterpriseNumber = :newenterpriseNumber, status = :newstatus, groupResponsible = :newgroupResponsible where country = :pkName, ledgerCode = :pkledgerCode, number = :pknumber";

int result = s.createQuery( hql )
.setString( "newenterpriseNumber", "Your JSP value")
.setString( "newstatus", "Your JSP value")
.setString( "newgroupResponsible", "Your JSP value")
.setString( "pkName", "Where clause value")
.setString( "pkledgerCode", "Where clause value")
.setString( "pknumber", "Where clause value")
.executeUpdate();

----------------------or------------------------------------------
String hql = "update SomeDetailsDAO set enterpriseNumber = "+JSPenterpriseNumber ", status = "+JSPstatus+", groupResponsible = "+JSPewgroupResponsible+" where country = "+pkName+", ledgerCode = "+pkledgerCode+"number = "+pknumber;

int result = s.createQuery( hql ).executeUpdate();
------------------or------------------------------------------------
Load the value with the Composite Keys and update it(This is not good way to do)

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.