- This topic has 4 replies, 3 voices, and was last updated 18 years, 8 months ago by Sreenath Gurujala.
-
AuthorPosts
-
Sreenath GurujalaParticipantHi,
I am gettin an error when I try to add a new record to a simple table.This is the log
=========
Caused by: net.sf.hibernate.HibernateException: Session is closed
at net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3339)
at net.sf.hibernate.impl.BatcherImpl.prepareStatement(BatcherImpl.java:63)
at net.sf.hibernate.impl.BatcherImpl.prepareStatement(BatcherImpl.java:58)
at net.sf.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:62)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:776)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:749)
at com.example.hibernate.VipService.addVipdata(VipService.java:252)The error, I think is occuring because Hibernate is not setting the ID(Primary key) while trying to insert the data.
I have a sequence declared in my backend “incr_id”
If i do “select INCR_ID.nextval from dual ” it works fine..I guess there is some problem with mappings..mapping
=====
<class name=”Vipdata” table=”VIPDATA”>
<id name=”vipid” column=”VIPID” type=”java.lang.Integer”>
<generator class=”sequence”>
<param name=”sequence”>incr_id</param>
</generator>
</id>here is the hibernate code
=================
vipdata.setViptitle(f.getViptitle());
vipdata.setVipname(f.getVipname());
VipService.getInstance().addVipdata(vipdata);Please help to fix this problem….
Thanks in advance..sreenath
Haris PecoMembersreenath ,
I advice that you upgrade to hibernate 3 – it is much, much better.I make your case with sequence generator and it work fine
If you can’t upgrade, please send us mapping and complete application code for examplesBest
Sreenath GurujalaParticipantHi there,
Thanks for your early reply.I figured out the problem.The session was getting closed automatically.
I have to check if the session is open each time i try to do some transaction.I needed to do something like this
if (session == null || !session.isOpen()) {
….
session = sessionFactory.openSession();
threadLocal.set(session);
}Thank you once again for your quick response. 😀
Sreenath
Don NelsonMemberI recommend that you hide all of that complexity with a delegation pattern. For example, my hibernate persistence manager has this for the save method:
public PersistentObject save(PersistentObject persistentObject)
throws EosException {
BusinessObject businessObject = null;
if (persistentObject instanceof BusinessObject) {
businessObject = (BusinessObject) persistentObject;
}
Transaction tx = null;
try {
tx = session.beginTransaction();
session.saveOrUpdate(persistentObject);
tx.commit();
}In this case, PersistentObject is a marker inteterface, but the logic is clear – remove the burden
of transactions and such from your calling method, and abstract it to a simple-to-use
save method.Don
Sreenath GurujalaParticipantHi Don
Thank you very much.I appreciate your help.
Sreenath -
AuthorPosts