-->
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.  [ 2 posts ] 
Author Message
 Post subject: Could not update ERROR
PostPosted: Wed Nov 03, 2004 5:48 am 
Newbie

Joined: Wed Nov 03, 2004 5:42 am
Posts: 2
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 5:57 am 
Newbie

Joined: Wed Nov 03, 2004 5:42 am
Posts: 2
The MOST INTERESTING THING is that if I use the syntax:

princess.setName("some string");

IT WORKS. If I use the syntax:

princess.setName(nn);

where nn is a string, IT DOES NOT WORK!!


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