• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

why not ?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

I am a newbie to hibernate. I desperately need to sort a simple example.

I am using oracle as a database. I am trying to work out the example (Event) specified in www.hibernate.org. I cleared of exceptions or errors. But the values giver is not inserting into the table.

The hbm.xml file I wrote is as :

<hibernate-mapping>
<class name="events.Event" table="EVENTS">
<id name="id" column="EVENT_ID" type="integer" unsaved-value="0">
<generator class="increment"/>
</id>
<property name="dt" type="timestamp" column="EVENT_DATE"/>
<property name="title" column="TITLE"/>
</class>
</hibernate-mapping>

and the class that does the transactions is :

/*
* EventManager.java
*
* Created on 12 January 2006, 10:33
*/

package events;

import org.hibernate.Session;

import java.util.Date;
import java.util.List;
import util.HibernateUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class EventManager {
private static Log log = LogFactory.getLog(HibernateUtil.class);
public static void main(String[] args) {

EventManager mgr = new EventManager();
long id = 101;
Event eve = new Event();
System.out.println("INSIDE LIST... ");
if (args[0].equals("store")) {

mgr.createAndStoreEvent("My Event", new Date());
}
else if (args[0].equals("list")) {
List events = mgr.listEvents();
for (int i = 0; i < events.size(); i++) {
Event theEvent = (Event) events.get(i);
System.out.println("Event: " + theEvent.getTitle() +
" Time: " + theEvent.getDt());
}
}
HibernateUtil.getSessionFactory().close();
}

private void createAndStoreEvent(String title, Date theDate) {
try {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();

Event theEvent = new Event();

theEvent.setTitle(title);
theEvent.setDt(theDate);

session.save(theEvent);
session.update(theEvent);
System.out.println("event id : "+theEvent.getId());
System.out.println("event title : "+theEvent.getTitle());
session.getTransaction().commit();
session.connection().commit();

//session.connection().createStatement().execute("SHUTDOWN");
}catch(Exception e){}
}


private List listEvents() {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();

session.beginTransaction();

List result = session.createQuery("from Event").list();

session.getTransaction().commit();

return result;
}

}

Anybody have an idea why the values not storing into ?

Thanks,
 
Sheriff
Posts: 26777
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Possibly if you didn't ignore exceptions like this:then you might have an idea. At least print the stack trace when an exception occurs:
 
Nanda Suraj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are correct. When I printed the exception I got :
" identifier of an instance of events.Event was altered from 1 to 1 "

I don't understand why this exception occures ?

Please help me ..

Thanks,
 
Nanda Suraj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it right. Thanks for the help.

I gave wrong type in the hbm.xml file. Instead of long , i gave integer.

Not its working and stored in the table.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic