Hi,
I can't update a record, using a simple example code:
Transaction tx= s.beginTransaction();
Cat princess = (Cat) s.load(Cat.class, new Integer(7)); // 7 exists on the DB
String nn = princess.getName();
nn = nn.concat(" I");
princess.setName(nn);
tx.commit();
How is it possible??
Thanks!!
Hibernate version: 2.1
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="quickstart.Cat" table="CAT">
<id name="id" column="cat_id">
<generator class="increment"/>
</id>
<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="name">
<column name="NAME" length="16" not-null="true"/>
</property>
<property name="sex"/>
<property name="weight"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
ThreadLocal sexion = new ThreadLocal();
Session s = (Session) sexion.get();
if (s == null) {
s = sessionFactory.openSession();
sexion.set(s);
}
//*******************************************
// UPDATE
//*******************************************
//List cats = s.find("from Cat as c order by c.id");
Transaction tx= s.beginTransaction();
//int id = (int)(Math.random() * cats.size()); // Uno a caso
//Cat currUp = (Cat) cats.get(id);
Cat princess = (Cat) s.load(Cat.class, new Integer(7)); // Se si conosce l'id
out.println("<p>Selected ID for update: " + princess.getId() + "</p>");
String nn = princess.getName();
out.println("<p>Curr Name: " + nn + "</p>");
out.println("<p>Curr ID: " + princess.getId() + "</p>");
nn = nn.concat(" I");
out.println("<p>New Name: " + nn + "</p>");
princess.setName(nn);
tx.commit();
//*******************************************
// CLOSE SESSION
//*******************************************
sexion.set(null);
if (s != null) {
s.close();
}
Full stack trace of any exception that occurs:
net.sf.hibernate.JDBCException: could not update: [quickstart.Cat#7]
net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:707)
net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:642)
net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2418)
net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2372)
net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)
net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
org.apache.jsp.goon_jsp._jspService(goon_jsp.java:89)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
Name and version of the database you are using: MS Sql Sever
|