-->
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: Simple Update - Doesn't work? Why?
PostPosted: Tue May 23, 2006 3:59 pm 
Newbie

Joined: Fri May 12, 2006 3:39 pm
Posts: 13
I'm here with quite a noob-ish type question. I don't understand why hibernate will not update my database. I am debugging it in Eclipse, and really, everything appears to be going fine, but the update does not actually happen, and I am left with the same data. No errors are present... please help, and thank you...

Hibernate version:
3.13

Mapping documents:

Code:
<?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 default-lazy="false">
<!-- Auto-generated mapping file from the hibernate.org cfg2hbm engine -->
    <class name="com.adsmack.smacklet.news.domain.News" table="news" >
        <id name="id" type="integer">
            <column name="ID" />
            <generator class="increment" />
        </id>
        <discriminator column="newstype" />
        <property name="article" type="string">
            <column name="article" />
        </property>         
      <property name="affiliateID" type="string">
            <column name="affiliateid" />
        </property>
         
      <subclass name="com.adsmack.smacklet.news.domain.AffiliateDailyNews"
           discriminator-value="DAILYNEWS" >
           <property name="dateSubmitted" type="timestamp">
               <column name="dateSubmitted" />
           </property>
           <property name="title" type="string">
               <column name="title" />
           </property>
           <property name="articleLink" type="string">
               <column name="articlelink" />
           </property>
           <property name="linkText" type="string">
               <column name="linktext" />
           </property>
        </subclass>
   
      <subclass name="com.adsmack.smacklet.news.domain.AffiliateGeneralNews"
         discriminator-value="GENERALNEWS" />      
    </class>
</hibernate-mapping>


Java Method:

Code:
public void updateDailyStory(int id, String title, String articleText, String link, String linkText) throws Exception {
      
   AffiliateDailyNews story = new AffiliateDailyNews();
   Session s = null;
   s = factory.openSession();
      
   Query q = s.createQuery("from AffiliateDailyNews adn where adn.id = :id");
   q.setInteger("id", id);
      
   story = (AffiliateDailyNews) q.uniqueResult();
      
   story.setTitle(title);
   story.setArticle(articleText);
   story.setArticleLink(link);
   story.setLinkText(linkText);
      
   s.update(story);
   
} // updateDailyStory




Again I thank you!

--LD


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 4:21 pm 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
include a transaction...
don´t forget s.flush();
only after the flush will hibernate execute the updates, inserts etc...

You can also specify auto commit in your hibernate configuration

hibernate.connection.autocommit = true

in that case you don't need to use transactions

Good luck!

_________________
Don´t forget to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 5:59 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
You need to get a Transaction from the Session, begin the transaction, then commit() the transaction at the end. You don't need to explicitly flush the session, as that is done by commit().


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 8:42 am 
Newbie

Joined: Fri May 12, 2006 3:39 pm
Posts: 13
Hey guys,

Thanks for the help.

I added a transaction, and I am now getting a classCastException on GregorianCalendar even though I am not trying to change the date... here is the new code.

Code:
   
public void updateDailyStory(int id, String title, String articleText, String link, String linkText) throws Exception {
      
   AffiliateDailyNews story = new AffiliateDailyNews();
   Session s = null;
   s = factory.openSession();
   Transaction t = s.beginTransaction();
   
   Query q = s.createQuery("from AffiliateDailyNews adn where adn.id = :id");
   q.setInteger("id", id);
      
   story = (AffiliateDailyNews) q.uniqueResult();
      
   story.setTitle(title);
   story.setArticle(articleText);
   story.setArticleLink(link);
   story.setLinkText(linkText);
      
   s.update(story);
   t.commit();
      
} // updateDailyStory


and the error...

Code:
java.lang.ClassCastException: java.util.GregorianCalendar



I am thinking that because I have a datetime object in my database, when I try to do this update, it is attempting to change that datetime... which I do not want.

Any help is a great help... thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 11:34 am 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
I think a full stack trace on the exception would help. There isn't enough information to do anything but take a wild guess. I can see that the dateSubmitted property is a timestamp type, so it may have something to do with that, but I would expect a compile-time error if you were setting the wrong type on that property, not a run-time error.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 12:48 pm 
Newbie

Joined: Fri May 12, 2006 3:39 pm
Posts: 13
Here you go

Thanks a lot.

Code:

java.lang.ClassCastException: java.util.GregorianCalendar
   at org.hibernate.type.TimestampType.isEqual(TimestampType.java:77)
   at org.hibernate.type.NullableType.isEqual(NullableType.java:160)
   at org.hibernate.type.AbstractType.isSame(AbstractType.java:104)
   at org.hibernate.type.AbstractType.isDirty(AbstractType.java:70)
   at org.hibernate.type.NullableType.isDirty(NullableType.java:186)
   at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:476)
   at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:2803)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:467)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:190)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:113)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:195)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   at com.adsmack.smacklet.news.persistence.hibernateDAO.HibernateNewsDAO.updateDailyStory(HibernateNewsDAO.java:117)
   at com.adsmack.smacklet.news.business.NewsHelper.updateDailyNews(NewsHelper.java:40)
   at com.adsmack.smacklet.news.presentation.NewsSmacklet.processAction(NewsSmacklet.java:153)
   at org.apache.jetspeed.factory.JetspeedPortletInstance.processAction(JetspeedPortletInstance.java:96)
   at org.apache.jetspeed.container.JetspeedContainerServlet.doGet(JetspeedContainerServlet.java:221)
   at org.apache.jetspeed.container.JetspeedContainerServlet.doPost(JetspeedContainerServlet.java:344)
   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.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
   at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:574)
   at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:499)
   at org.apache.jetspeed.container.invoker.ServletPortletInvoker.invoke(ServletPortletInvoker.java:213)
   at org.apache.jetspeed.container.invoker.ServletPortletInvoker.action(ServletPortletInvoker.java:133)
   at org.apache.pluto.PortletContainerImpl.processPortletAction(PortletContainerImpl.java:164)
   at org.apache.jetspeed.container.JetspeedPortletContainerWrapper.processPortletAction(JetspeedPortletContainerWrapper.java:132)
   at org.apache.jetspeed.pipeline.valve.impl.ActionValveImpl.invoke(ActionValveImpl.java:87)
   at org.apache.jetspeed.pipeline.JetspeedPipeline$Invocation.invokeNext(JetspeedPipeline.java:166)
   at org.apache.jetspeed.container.ContainerValve.invoke(ContainerValve.java:76)
   at org.apache.jetspeed.pipeline.JetspeedPipeline$Invocation.invokeNext(JetspeedPipeline.java:166)
   at org.apache.jetspeed.profiler.impl.ProfilerValveImpl.invoke(ProfilerValveImpl.java:255)
   at org.apache.jetspeed.pipeline.JetspeedPipeline$Invocation.invokeNext(JetspeedPipeline.java:166)
   at org.apache.jetspeed.security.impl.LoginValidationValveImpl.invoke(LoginValidationValveImpl.java:159)
   at org.apache.jetspeed.pipeline.JetspeedPipeline$Invocation.invokeNext(JetspeedPipeline.java:166)
   at org.apache.jetspeed.security.impl.PasswordCredentialValveImpl.invoke(PasswordCredentialValveImpl.java:148)
   at org.apache.jetspeed.pipeline.JetspeedPipeline$Invocation.invokeNext(JetspeedPipeline.java:166)
   at org.apache.jetspeed.localization.impl.LocalizationValveImpl.invoke(LocalizationValveImpl.java:169)
   at org.apache.jetspeed.pipeline.JetspeedPipeline$Invocation.invokeNext(JetspeedPipeline.java:166)
   at org.apache.jetspeed.security.impl.AbstractSecurityValve$1.run(AbstractSecurityValve.java:117)
   at java.security.AccessController.doPrivileged(Native Method)
   at javax.security.auth.Subject.doAsPrivileged(Unknown Source)
   at org.apache.jetspeed.security.impl.AbstractSecurityValve.invoke(AbstractSecurityValve.java:111)
   at org.apache.jetspeed.pipeline.JetspeedPipeline$Invocation.invokeNext(JetspeedPipeline.java:166)
   at org.apache.jetspeed.container.url.impl.PortalURLValveImpl.invoke(PortalURLValveImpl.java:67)
   at org.apache.jetspeed.pipeline.JetspeedPipeline$Invocation.invokeNext(JetspeedPipeline.java:166)
   at org.apache.jetspeed.capabilities.impl.CapabilityValveImpl.invoke(CapabilityValveImpl.java:128)
   at org.apache.jetspeed.pipeline.JetspeedPipeline$Invocation.invokeNext(JetspeedPipeline.java:166)
   at org.apache.jetspeed.pipeline.JetspeedPipeline.invoke(JetspeedPipeline.java:145)
   at org.apache.jetspeed.engine.JetspeedEngine.service(JetspeedEngine.java:214)
   at org.apache.jetspeed.engine.JetspeedServlet.doGet(JetspeedServlet.java:232)
   at org.apache.jetspeed.engine.JetspeedServlet.doPost(JetspeedServlet.java:259)
   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.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
   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:869)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
   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(Unknown Source)



Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 1:04 pm 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
Hi you can´t map your type as timestamp
if you use Calendar as a type in your class!

Code:
           <property name="dateSubmitted" type="timestamp">
               <column name="dateSubmitted" />
           </property>


try type="java.util.Calendar"
or chage the type of the property in your class
I am not sure if you should usa java.sql.Timestamp
or if you can use java.util.Date...

Good Luck!

_________________
Don´t forget to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 2:13 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
Yes, you have a type mismatch between the actual persistent object definition and the database. The mapping says it's a timestamp, but the object contains a GregorianCalender instance.


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.