Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- org.hibernate.HibernateException: getCriteriaBuilder is not valid without active transaction
- CriteriaBuilder builder = sessionFactory.getCurrentSession().getCriteriaBuilder();
- @Repository("personDao")
- public class PersonDAOImpl implements PersonDAO {
- @Autowired
- private SessionFactory sessionFactory;
- @Override
- public void save(Person person) {
- sessionFactory.getCurrentSession().saveOrUpdate(person);
- }
- @Override
- public void delete(Person person) {
- sessionFactory.getCurrentSession().delete(person);
- }
- @Override
- public List<Person> listPersons() {
- CriteriaBuilder builder = sessionFactory.getCurrentSession().getCriteriaBuilder();
- CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
- Root<Person> employeeRoot=criteria.from(Person.class);
- criteria.select(employeeRoot);
- return sessionFactory.getCurrentSession().createQuery(criteria).getResultList();
- }
- }
- @Service
- @Transactional
- public class PersonServiceImpl implements PersonService {
- @Autowired
- private PersonDAO personDao;
- @Override
- public void save(Person person) {
- personDao.save(person);
- }
- @Override
- public void delete(Person person) {
- personDao.delete(person);
- }
- @Override
- public List<Person> listPersons() {
- return personDao.listPersons();
- }
- @Override
- public Person getById(Integer id) {
- return personDao.getById(id);
- }
- }
- @Controller
- @RequestMapping("/persons")
- public class PersonController {
- private static final Logger logger = LoggerFactory.getLogger(PersonController.class);
- @Autowired
- private PersonService personService;
- @RequestMapping(value = {"/", "/all", "/list"}, method = RequestMethod.GET)
- public String list(Model model) {
- logger.info("Se ingresa al método de listar personas");
- List<Person> persons = personService.listPersons();
- logger.info("listando personas, hay un total de " + persons.size());
- model.addAttribute("persons", persons.size());
- return "persons";
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
- <!-- The definition of the Root Spring Container shared by all Servlets
- and Filters -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/spring/root-context.xml</param-value>
- </context-param>
- <!-- Creates the Spring Container shared by all Servlets and Filters -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- Processes application requests -->
- <servlet>
- <servlet-name>appServlet</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>appServlet</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
- <?xml version="1.0" encoding="UTF-8"?>
- <beans:beans xmlns="http://www.springframework.org/schema/mvc"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jdbc="http://www.springframework.org/schema/jdbc"
- xmlns:beans="http://www.springframework.org/schema/beans"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
- http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
- <!-- DispatcherServlet Context: defines this servlet's request-processing
- infrastructure -->
- <!-- Enables the Spring MVC @Controller programming model -->
- <annotation-driven />
- <!-- Handles HTTP GET requests for /resources/** by efficiently serving
- up static resources in the ${webappRoot}/resources directory -->
- <resources mapping="/resources/**" location="/resources/" />
- <!-- Resolves views selected for rendering by @Controllers to .jsp resources
- in the /WEB-INF/views directory -->
- <beans:bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <beans:property name="prefix" value="/WEB-INF/views/" />
- <beans:property name="suffix" value=".jsp" />
- </beans:bean>
- <context:component-scan base-package="com.edwin.spring" />
- <!-- Datasource -->
- <context:property-placeholder location="classpath:hibernate.properties" />
- <beans:bean
- class="org.springframework.jdbc.datasource.DriverManagerDataSource"
- id="dataSource">
- <beans:property name="driverClassName" value="${jdbc.driver}" />
- <beans:property name="url" value="${jdbc.url}" />
- <beans:property name="username" value="${jdbc.user}" />
- <beans:property name="password" value="${jdbc.password}" />
- </beans:bean>
- <!--SessionFactory -->
- <beans:bean id="hibernateSessionFactory"
- class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
- <beans:property name="dataSource" value="#{dataSource}" />
- <beans:property name="packagesToScan" value="com.edwin.spring.model" />
- <beans:property name="hibernateProperties">
- <beans:props>
- <beans:prop key="hibernate.dialect">${hibernate.dialect}</beans:prop>
- <beans:prop key="hibernate.connection.pool_size">1</beans:prop>
- <beans:prop key="hibernate.show_sql">${hibernate.show_sql}</beans:prop>
- <beans:prop key="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory
- </beans:prop>
- <!--<prop key="hibernate.hbm2ddl.auto">create-drop</prop> -->
- <beans:prop key="hibernate.format_sql">true</beans:prop>
- <beans:prop key="hibernate.generate_statistics">true</beans:prop>
- <beans:prop key="hibernate.use_sql_comments">true</beans:prop>
- <beans:prop key="hibernate.current_session_context_class">thread</beans:prop>
- <beans:prop key="hibernate.cglib.use_reflection_optimizer">true</beans:prop>
- <beans:prop key="hibernate.hibernate.cache.use_query_cache">true</beans:prop>
- <beans:prop key="hibernate.enable_lazy_load_no_trans">true</beans:prop>
- <beans:prop key="hibernate.transaction.flush_before_completion">true</beans:prop>
- </beans:props>
- </beans:property>
- </beans:bean>
- <tx:annotation-driven transaction-manager="transactionManager"/>
- <beans:bean class="org.springframework.orm.hibernate5.HibernateTransactionManager"
- id="transactionManager">
- <beans:property name="sessionFactory" ref="hibernateSessionFactory"/>
- </beans:bean>
- </beans:beans>
- #DB properties:
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/spring
- jdbc.user=root
- jdbc.password=1234
- #Hibernate Configuration:
- hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
- hibernate.show_sql=true
- hibernate.format_sql=true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement