-->
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.  [ 3 posts ] 
Author Message
 Post subject: Update based on key only
PostPosted: Mon Mar 27, 2006 7:29 am 
Newbie

Joined: Mon Mar 27, 2006 7:16 am
Posts: 8
I'm new to Hibernate, so I'm sure this is a simple question...

I'm using Hibernate version 3.1.2.

I have a simple Person object which contains three fields: id, name and age. Id is primary key. This Person object maps to a Person table in the db which contains the same fields.

I want to update the age of an existing person in the db by just giving id and the age.

Code:
Code:
Person thePerson = new Person();
thePerson.setId(38);
thePerson.setAge(22);
// thePerson.setName("test"); This line is commented out

session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
session.update(thePerson);
transaction.commit();
session.close();



When I run this code I get an error (see stack trace below). The problem is that i MUST call thePerson.setName("test") for the code to work. Why?? I don't want that....

I only want to set the id and the age so that Hibernate simply generates SQL (i'm using mysql) like this: update person set age=22 where id=38

It looks like that when I don't set either the age or the name attribute in the thePerson object, Hibernate tries to set the fields to NULL in the db by default. How can I avoid this?

Here's the stacktrace:
Quote:
2006-03-27 13:14:25,074 | ERROR | HandleDB.java | editPerson() | 263 | Transaction failed!
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1009)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:356)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at db.HandleDB.editPerson(HandleDB.java:260)
at web.DbServlet.editPerson(DbServlet.java:230)
at web.DbServlet.doGet(DbServlet.java:71)
at web.DbServlet.doPost(DbServlet.java:50)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.BatchUpdateException: Data truncation: Column set to default value; NULL supplied to NOT NULL column 'name' at row 1
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:647)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 27 more



Here's my mapping file:

Quote:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="logic.Person" table="Person">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" type="string" column="name"/>
<property name="age" type="integer" column="age"/>
</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 27, 2006 12:08 pm 
Newbie

Joined: Tue Mar 21, 2006 1:42 pm
Posts: 14
How is Hibernate supposed to know that you only want to update the age and not the name (set it to null)? It doesn't. Either 1) set the name, 2) load the persisted Person and change the age and then update, or 3) use straight JDBC and SQL to perform your update.

_________________
Mike Ringrose


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 27, 2006 3:16 pm 
Newbie

Joined: Mon Mar 27, 2006 7:16 am
Posts: 8
space.ghost wrote:
How is Hibernate supposed to know that you only want to update the age and not the name (set it to null)? It doesn't. Either 1) set the name, 2) load the persisted Person and change the age and then update, or 3) use straight JDBC and SQL to perform your update.


Well I thought that if I just gave it the id and the attribute to update, Hibernate would be smart enough to leave the other attributes alone given that they cannot be null in the db.

Are you sure it's just not a parameter I need to set in the mapping file or something? It seems like such a simple task to accomplish.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.