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>