Meaningless Drivel is fun!*
The moose likes Object Relational Mapping and the fly likes why not ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "why not ?" Watch "why not ?" New topic
Author

why not ?

Nanda Suraj
Greenhorn

Joined: May 04, 2005
Posts: 8
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,
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: May 04, 2005
Posts: 8
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

Joined: May 04, 2005
Posts: 8
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why not ?
 
Similar Threads
Hibernate Configurations
Read/Write BLOB in byte[] with Hibernate in MySQL..
EVENTS is not mapped [FROM EVENTS]
Design benefits of creating Sessionfactory in an utility class
Hibernate Maping error