• Register
1 vote
2.7k views

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 1
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
1 answer 45 views
45 views
Problem: I need help in solving this error: Could not obtain transaction-synchronized session for current thread.
asked Mar 13 Wafa Abu Yousef 6.1k points
0 votes
2 answers 772 views
772 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 438 views
438 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
0 votes
1 answer 88 views
88 views
Problem : After executing spring boot with h2 database and JPA i am facing below error, any help: org.hibernate.tool.schema.spi.commandacceptanceexception: error executing ddl via jdbc statement
asked Mar 6 Wafa Abu Yousef 6.1k points
1 vote
1 answer 5.4K views
5.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 5.2K views
5.2K 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 98 views
98 views
Problem: could anybody help me to fix this ? Could not extract response: no suitable httpmessageconverter found for response type.
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 3.1K views
3.1K 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
1 vote
1 answer 19 views
19 views
Problem : What does this mean > @Autowired - No qualifying bean of type found for dependency
asked Apr 23 Abik Dey 4k points
0 votes
1 answer 81 views
81 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 Mar 6 Wafa Abu Yousef 6.1k points