简体   繁体   中英

Using transactional in spring boot with hibernate

I am getting the error while using hibernate in spring boot application No qualifying bean of type TransactionManager' available
I am using the following config class:

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@org.springframework.context.annotation.Configuration
@EnableTransactionManagement
public class Config {
    @Bean
    public SessionFactory sessionFactory() {

        Configuration configuration = new Configuration();
        configuration.configure();
        configuration.addAnnotatedClass(Ct.class);
        configuration.addAnnotatedClass(St.class);

        SessionFactory sessionFactory = configuration.buildSessionFactory();
        return sessionFactory;
    }

}  
@RestController
public class RestAPIController {
 @Autowired
    private SessionFactory sessionFactory;

 @PutMapping("/addS")
    @Transactional
    public void addSt(@RequestParam("cc") String cc,@RequestParam("st") String st) {
        CC cc1= new CC();
        CC.setCode(cc);
        State state = new State(cc,st);
        sessionFactory.getCurrentSession().save(state);
    }
}
}  

The main reason I added the @Transactional in the addSt method is due to error: The transaction was still an active when an exception occurred during Database. So I turned to use spring boot for managing transactions. I am not sure what to do here.

--------------------UPDATED CODE--------------------

@Repository
public interface StateRepository extends CrudRepository<State, String> {}


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class StateService {

    @Autowired
    private StateRepository stateRepository;

    public void save(State state) {
        stateRepository.save(state);
    }

    public List<State> findAll() {
        List<State> states = new ArrayList<>();
        stateRepository.findAll().forEach(states::add);
        return states;
    }
}

For starters use proper layers and write a service and use JPA instead of plain Hibernate. If you want a Session you can always use EntityManager.unwrap to obtain the underlying Session .

@Service
@Transactional
public StateService {

  @PersistenceContext
  private EntityManager em;

  public void save(State state) {
    em.persist(state);
  }

Use this service in your controller instead of the SessionFactory .

@RestController
public class RestAPIController {

    private final StateService stateService;

    RestAPIController(StateService stateService) {
      this.stateService=stateService;
    }

    @PutMapping("/addS")
    public void addSt(@RequestParam("cc") String cc, @RequestParam("st") String st) {
        CC cc1= new CC();
        CC.setCode(cc);
        State state = new State(cc,st);
        stateService.save(state);
    }
}

Now ditch your Config class and restart the application.

NOTE

When using Spring Data JPA it is even easier, define a repository extending CrudRepository and inject that into the service instead of an EntityManager . (I'm assuming that Long is the type of primary key you defined).

public interface StateRepository extends CrudRepository<State, Long> {}
@Service
@Transactional
public StateService {

  private final StateRepository states;

  public StateService(StateRepository states) {
    this.states=states;
  }

  public void save(State state) {
    states.save(state);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM