• Register
1 vote
1.1k 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

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 378 views
378 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 jwilliam 3.9k points
0 votes
1 answer 219 views
219 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 1.4K views
1.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 3.7K views
3.7K 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 1.8K views
1.8K 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