jQuery in Action, 3rd edition
The moose likes Object Relational Mapping and the fly likes getting message Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "getting message "get is not valid without active transaction" while trying to delete using Hibernate" Watch "getting message "get is not valid without active transaction" while trying to delete using Hibernate" New topic
Author

getting message "get is not valid without active transaction" while trying to delete using Hibernate

Monica Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 820
I am trying to delete User based on userId but getting message "org.hibernate.HibernateException: get is not valid without active transaction"

error message:


org.hibernate.HibernateException: get is not valid without active transaction
org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:297)
$Proxy6.get(Unknown Source)
com.dao.LoginHibernateDaoImpl.deleteUsersbyId(LoginHibernateDaoImpl.java:115)
com.web.DeleteUserAction.execute(DeleteUserAction.java:48)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


The code which leads to this message is :



thanks

Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19757
    
  10

Why wait so long to start the transaction? Did you have a reason you wanted to get the User object outside the transaction?

And now you have an error message about exactly that. I suggest it shouldn't be too hard to fix the problem.
Volodymyr Lysenko
Ranch Hand

Joined: Mar 29, 2012
Posts: 511
    
    1

Hello!

Monica, all methods of org.hibernate.Session should be called within active transaction.
You need to get current session then begin transaction only then use org.hibernate.Session then commit transaction then close session.
Probably all methods of org.hibernate.Session are not executed when you call them but WHEN transaction is committed (line 13 in your code).


True person is moral, false is right!
Monica Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 820
Thanks. It worked.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting message "get is not valid without active transaction" while trying to delete using Hibernate
 
It's not a secret anymore!