• Register
1 vote

Problem :

I am beginner to spring + hibernate. Recently I have created the application with the use of spring + hibernate but I always face below error. I am trying to work on it for the very first time.

Below is the error for your reference:

INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at coreservlets.StudentDAOImpl.create(StudentDAOImpl.java:19)
at coreservlets.MainApp.main(MainApp.java:14)

Can somebody help me in overcoming above error?

8 5 2
3,230 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution :

I like answering questions based on  spring + hibernate so I am trying to help you in fixing your mentioned error.

I had also faced the same issue in the past it was in the class which was not the part of any service layer. My transaction manager was very easily obtained from a context by my getBean() method and also my class belonged to a view layer and the project was utilizing the OpenSessionInView technique.

In my case my sessionFactory.getCurrentSession() method was causing the same exception as yours. And solution for it is very simple as given below for your reference.

Session session;

try {
    mysession = sessionFactory.getCurrentSession();
} catch (HibernateException mye) {
    mysession = sessionFactory.openSession();

If by any chance the getCurrentSession() method fails for you then the openSession() should do the work for you..

8 4
5,680 points
this opensession causes performance problem and it will not work as expected when you have saveOrUpdate() method, it will not update records into DB as expected. It is not a good fix. I dont know how to fix this still i m searching , if any good way. please share.

Related questions

0 votes
2 answers 621 views
Problem : I am new to the Spring and JPA, wasted 10 days and no result with the searching on internet. I want to save the object to SQL SERVER, my connection is correct but when I write .flush() I get below exception “nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress”
asked Jan 21, 2020 jwilliam 3.9k points
0 votes
1 answer 329 views
Problem : I am new to Spring and JPA I want to save object to SQL SERVER, connection is correct but when I write .flush() I get the exception as below javax.persistence.TransactionRequiredException: no transaction is in progress
asked Oct 22, 2019 peterlaw 6.9k points
1 vote
1 answer 3.4K views
Problem : When I try to compile my spring project, I always face the below error : Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed I have the ... >1.4.1.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> . . .
asked Dec 12, 2019 alecxe 7.5k points
0 votes
1 answer 4.6K views
Problem : After executing spring boot with h2 database and JPA i am facing below error org.hibernate.tool.schema.spi.commandacceptanceexception: error executing ddl via jdbc statement
asked Nov 12, 2019 peterlaw 6.9k points
0 votes
1 answer 2.5K views
Problem : I am trying to use the curl command as follows: curl -u 591bf65f50057469f10b5fd9:0cf17f9b03d056ds0e11e48497e506a2 https://backend.tdk.com/api/devicetypes/59147fd79e93s12e61499ffe/messages But I am getting a JSON response as follows: {"data":[{"device":"18SE62"," ... T>, Object...) in the type RestTemplate is not applicable for the arguments (String, HttpEntity<String>,Class<MessageList>)
asked Dec 10, 2019 alecxe 7.5k points
0 votes
1 answer 3.6K views
Problem : I am trying to write Spring boot code The Program is running but I am getting following error: This application has no explicit mapping for /error, so you are seeing this as a fallback. How to fix this error?
asked Nov 22, 2019 peterlaw 6.9k points
0 votes
1 answer 1 view
1 view
Problem: the request sent by the client was syntactically incorrect.
asked Feb 18 Ethan ross 1.8k points
0 votes
1 answer 4 views
Problem: failed to destroy end point associated with protocolhandler.
asked Feb 17 Ethan ross 1.8k points
0 votes
1 answer 19 views
Problem unable to process parts as no multi-part configuration has been provided
asked Feb 9 charles mathews 3.8k points
0 votes
1 answer 5.4K views
Problem : I am developing the application in Spring, using a Tomcat, Mysql5, Java8... My problem is that I cannot deploy it, due to the "required bean 'entityManagerFactory' not found" problem. I have developed my project with the coworkers but ... a bean named 'entityManagerFactory' that could not be found. Action: Consider defining a bean named 'entityManagerFactory' in your configuration.
asked Jan 25, 2020 jwilliam 3.9k points