-->
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.  [ 6 posts ] 
Author Message
 Post subject: why it is not ?
PostPosted: Mon Jan 16, 2006 10:59 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 8:07 am
Posts: 29
Hi,

I desperately need help. I couldn't work out a simple example of hibernate .

I am using oracle as a database. Here is my hbm.xml file

<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>


the class file doing transactions :

/*
* 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;
}

}

I am not getting any exceptions. But the values not been stored into the table. Please help me with it. Anybody have an idea ?

Thanks,
JSV.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 3:23 pm 
Newbie

Joined: Tue Jan 10, 2006 1:30 pm
Posts: 14
You aren't doing anything in your catch statement. Are you sure you aren't getting an error?

_________________
Brian


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 8:24 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
I had this one, once before in a application I took over. It was very funny to find out the problem.

Never catch all exceptions without outputting the message!!!

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject: altered from 1 to 1
PostPosted: Tue Jan 17, 2006 6:02 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 8:07 am
Posts: 29
Yes you are right. I printed the exception. I got the exception :

identifier of an instance of events.Event was altered from 1 to 1

I couldn't find out why this exception happens. Please help me...


Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 6:08 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
you cannot change the primary key of an object. Hibernate identifies the objects in the session by the primary key.
If you could change this, you would pull the rug out from under Hibernate.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 6:14 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 8:07 am
Posts: 29
I solved the problem. I noticed that i have given mistakenly the type of primary key . Instead of long, I gave integer.

Now its working fine.

Thanks Sebastian for your replies and suggestions.


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