Advertisement
Guest User

Untitled

a guest
Jul 31st, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. org.hibernate.HibernateException: getCriteriaBuilder is not valid without active transaction
  2.  
  3. CriteriaBuilder builder = sessionFactory.getCurrentSession().getCriteriaBuilder();
  4.  
  5. @Repository("personDao")
  6. public class PersonDAOImpl implements PersonDAO {
  7.  
  8. @Autowired
  9. private SessionFactory sessionFactory;
  10.  
  11. @Override
  12. public void save(Person person) {
  13. sessionFactory.getCurrentSession().saveOrUpdate(person);
  14. }
  15.  
  16. @Override
  17. public void delete(Person person) {
  18. sessionFactory.getCurrentSession().delete(person);
  19. }
  20.  
  21. @Override
  22. public List<Person> listPersons() {
  23. CriteriaBuilder builder = sessionFactory.getCurrentSession().getCriteriaBuilder();
  24. CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
  25. Root<Person> employeeRoot=criteria.from(Person.class);
  26. criteria.select(employeeRoot);
  27. return sessionFactory.getCurrentSession().createQuery(criteria).getResultList();
  28. }
  29. }
  30.  
  31. @Service
  32. @Transactional
  33. public class PersonServiceImpl implements PersonService {
  34.  
  35. @Autowired
  36. private PersonDAO personDao;
  37.  
  38. @Override
  39. public void save(Person person) {
  40. personDao.save(person);
  41. }
  42.  
  43. @Override
  44. public void delete(Person person) {
  45. personDao.delete(person);
  46. }
  47.  
  48. @Override
  49. public List<Person> listPersons() {
  50. return personDao.listPersons();
  51. }
  52.  
  53. @Override
  54. public Person getById(Integer id) {
  55. return personDao.getById(id);
  56. }
  57.  
  58. }
  59.  
  60. @Controller
  61. @RequestMapping("/persons")
  62. public class PersonController {
  63. private static final Logger logger = LoggerFactory.getLogger(PersonController.class);
  64.  
  65. @Autowired
  66. private PersonService personService;
  67.  
  68. @RequestMapping(value = {"/", "/all", "/list"}, method = RequestMethod.GET)
  69. public String list(Model model) {
  70. logger.info("Se ingresa al método de listar personas");
  71. List<Person> persons = personService.listPersons();
  72. logger.info("listando personas, hay un total de " + persons.size());
  73. model.addAttribute("persons", persons.size());
  74. return "persons";
  75. }
  76. }
  77.  
  78. <?xml version="1.0" encoding="UTF-8"?>
  79. <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  80. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  81. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
  82.  
  83. <!-- The definition of the Root Spring Container shared by all Servlets
  84. and Filters -->
  85. <context-param>
  86. <param-name>contextConfigLocation</param-name>
  87. <param-value>/WEB-INF/spring/root-context.xml</param-value>
  88. </context-param>
  89.  
  90. <!-- Creates the Spring Container shared by all Servlets and Filters -->
  91. <listener>
  92. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  93. </listener>
  94.  
  95. <!-- Processes application requests -->
  96. <servlet>
  97. <servlet-name>appServlet</servlet-name>
  98. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  99. <init-param>
  100. <param-name>contextConfigLocation</param-name>
  101. <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
  102. </init-param>
  103. <load-on-startup>1</load-on-startup>
  104. </servlet>
  105.  
  106. <servlet-mapping>
  107. <servlet-name>appServlet</servlet-name>
  108. <url-pattern>/</url-pattern>
  109. </servlet-mapping>
  110. </web-app>
  111.  
  112. <?xml version="1.0" encoding="UTF-8"?>
  113. <beans:beans xmlns="http://www.springframework.org/schema/mvc"
  114. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  115. xmlns:context="http://www.springframework.org/schema/context"
  116. xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  117. xmlns:beans="http://www.springframework.org/schema/beans"
  118. xmlns:tx="http://www.springframework.org/schema/tx"
  119. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  120. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
  121. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
  122. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
  123. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
  124.  
  125. <!-- DispatcherServlet Context: defines this servlet's request-processing
  126. infrastructure -->
  127.  
  128. <!-- Enables the Spring MVC @Controller programming model -->
  129. <annotation-driven />
  130.  
  131. <!-- Handles HTTP GET requests for /resources/** by efficiently serving
  132. up static resources in the ${webappRoot}/resources directory -->
  133. <resources mapping="/resources/**" location="/resources/" />
  134.  
  135. <!-- Resolves views selected for rendering by @Controllers to .jsp resources
  136. in the /WEB-INF/views directory -->
  137. <beans:bean
  138. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  139. <beans:property name="prefix" value="/WEB-INF/views/" />
  140. <beans:property name="suffix" value=".jsp" />
  141. </beans:bean>
  142.  
  143. <context:component-scan base-package="com.edwin.spring" />
  144.  
  145. <!-- Datasource -->
  146. <context:property-placeholder location="classpath:hibernate.properties" />
  147.  
  148. <beans:bean
  149. class="org.springframework.jdbc.datasource.DriverManagerDataSource"
  150. id="dataSource">
  151. <beans:property name="driverClassName" value="${jdbc.driver}" />
  152. <beans:property name="url" value="${jdbc.url}" />
  153. <beans:property name="username" value="${jdbc.user}" />
  154. <beans:property name="password" value="${jdbc.password}" />
  155. </beans:bean>
  156.  
  157. <!--SessionFactory -->
  158. <beans:bean id="hibernateSessionFactory"
  159. class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  160. <beans:property name="dataSource" value="#{dataSource}" />
  161. <beans:property name="packagesToScan" value="com.edwin.spring.model" />
  162. <beans:property name="hibernateProperties">
  163. <beans:props>
  164. <beans:prop key="hibernate.dialect">${hibernate.dialect}</beans:prop>
  165. <beans:prop key="hibernate.connection.pool_size">1</beans:prop>
  166. <beans:prop key="hibernate.show_sql">${hibernate.show_sql}</beans:prop>
  167. <beans:prop key="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory
  168. </beans:prop>
  169. <!--<prop key="hibernate.hbm2ddl.auto">create-drop</prop> -->
  170. <beans:prop key="hibernate.format_sql">true</beans:prop>
  171. <beans:prop key="hibernate.generate_statistics">true</beans:prop>
  172. <beans:prop key="hibernate.use_sql_comments">true</beans:prop>
  173. <beans:prop key="hibernate.current_session_context_class">thread</beans:prop>
  174. <beans:prop key="hibernate.cglib.use_reflection_optimizer">true</beans:prop>
  175. <beans:prop key="hibernate.hibernate.cache.use_query_cache">true</beans:prop>
  176. <beans:prop key="hibernate.enable_lazy_load_no_trans">true</beans:prop>
  177. <beans:prop key="hibernate.transaction.flush_before_completion">true</beans:prop>
  178. </beans:props>
  179. </beans:property>
  180. </beans:bean>
  181.  
  182. <tx:annotation-driven transaction-manager="transactionManager"/>
  183.  
  184. <beans:bean class="org.springframework.orm.hibernate5.HibernateTransactionManager"
  185. id="transactionManager">
  186. <beans:property name="sessionFactory" ref="hibernateSessionFactory"/>
  187. </beans:bean>
  188.  
  189. </beans:beans>
  190.  
  191. #DB properties:
  192. jdbc.driver=com.mysql.jdbc.Driver
  193. jdbc.url=jdbc:mysql://localhost:3306/spring
  194. jdbc.user=root
  195. jdbc.password=1234
  196.  
  197. #Hibernate Configuration:
  198. hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
  199. hibernate.show_sql=true
  200. hibernate.format_sql=true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement