Guest User

Untitled

a guest
Apr 8th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.88 KB | None | 0 0
  1. package com.app.EcommerceBackend.config;
  2.  
  3. import java.util.Properties;
  4.  
  5. import javax.sql.DataSource;
  6.  
  7. import org.apache.commons.dbcp2.BasicDataSource;
  8. import org.hibernate.SessionFactory;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.ComponentScan;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.core.annotation.Order;
  13. import org.springframework.orm.hibernate5.HibernateTransactionManager;
  14. import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
  15. import org.springframework.transaction.annotation.EnableTransactionManagement;
  16.  
  17. @Configuration
  18. @ComponentScan(basePackages={"com.app.EcommerceBackend.dto"})
  19. @EnableTransactionManagement
  20. public class HibernateConfig {
  21.  
  22. // Change the below based on the DBMS you choose
  23. private final static String DATABASE_URL = "jdbc:h2:tcp://localhost/~/ecommerce";
  24. private final static String DATABASE_DRIVER = "org.h2.Driver";
  25. private final static String DATABASE_DIALECT = "org.hibernate.dialect.H2Dialect";
  26. private final static String DATABASE_USERNAME = "sa";
  27. private final static String DATABASE_PASSWORD = "";
  28.  
  29. // dataSource bean will be available
  30. @Bean
  31. public DataSource getDataSource() {
  32.  
  33. BasicDataSource dataSource = new BasicDataSource();
  34.  
  35. // Providing the database connection information
  36. dataSource.setDriverClassName(DATABASE_DRIVER);
  37. dataSource.setUrl(DATABASE_URL);
  38. dataSource.setUsername(DATABASE_USERNAME);
  39. dataSource.setPassword(DATABASE_PASSWORD);
  40.  
  41.  
  42. return dataSource;
  43.  
  44. }
  45.  
  46. // sessionFactory bean will be available
  47.  
  48. @Bean
  49. public SessionFactory getSessionFactory(DataSource dataSource) {
  50.  
  51. LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);
  52.  
  53. builder.addProperties(getHibernateProperties());
  54. builder.scanPackages("com.app.EcommerceBackend.dto");
  55.  
  56. return builder.buildSessionFactory();
  57.  
  58. }
  59.  
  60.  
  61.  
  62. // All the hibernate properties will be returned in this method
  63. private Properties getHibernateProperties() {
  64.  
  65. Properties properties = new Properties();
  66.  
  67.  
  68. properties.put("hibernate.dialect", DATABASE_DIALECT);
  69. properties.put("hibernate.show_sql", "true");
  70. properties.put("hibernate.format_sql", "true");
  71.  
  72. //properties.put("hibernate.hbm2ddl.auto", "create");
  73.  
  74.  
  75. return properties;
  76. }
  77.  
  78. // transactionManager bean
  79. @Bean
  80. public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
  81. HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
  82. return transactionManager;
  83. }
  84.  
  85.  
  86. }
  87.  
  88. package com.app.EcommerceBackend.daoImpl;
  89.  
  90. import java.util.ArrayList;
  91. import java.util.List;
  92.  
  93. import org.hibernate.SessionFactory;
  94. import org.springframework.beans.factory.annotation.Autowired;
  95. import org.springframework.stereotype.Repository;
  96. import org.springframework.transaction.annotation.Transactional;
  97.  
  98. import com.app.EcommerceBackend.dao.CategoryDAO;
  99. import com.app.EcommerceBackend.dto.Category;
  100.  
  101. @Repository("categoryDAO")
  102. @Transactional
  103. public class CategoryDAOImpl implements CategoryDAO
  104. {
  105. @Autowired
  106. private SessionFactory sessionFactory;
  107.  
  108. private static List<Category> categories = new ArrayList<>();
  109.  
  110. static {
  111. Category category = new Category();
  112.  
  113. // Add First Element
  114. category.setId(1);
  115. category.setName("Television");
  116. category.setDescription("this is some desc for television");
  117. category.setImageURL("img1.jpg");
  118.  
  119. categories.add(category);
  120.  
  121. // Add Second Element
  122. category = new Category();
  123. category.setId(2);
  124. category.setName("Mobile");
  125. category.setDescription("this is some desc for Mobile");
  126. category.setImageURL("img2.jpg");
  127.  
  128. categories.add(category);
  129.  
  130. // Add Third Element
  131. category = new Category();
  132. category.setId(3);
  133. category.setName("Laptop");
  134. category.setDescription("this is some desc for Laptop");
  135. category.setImageURL("img3.jpg");
  136.  
  137. categories.add(category);
  138.  
  139. }
  140.  
  141. @Override
  142. public List<Category> list() {
  143. return categories;
  144.  
  145. }
  146.  
  147. @Override
  148. public Category get(int id) {
  149.  
  150. for (Category category : categories) {
  151. if (category.getId() == id)
  152. return category;
  153. }
  154. return null;
  155. }
  156.  
  157. @Override
  158.  
  159. public boolean add(Category category) {
  160.  
  161. //add the category to the databse table
  162. try
  163. {
  164. sessionFactory.getCurrentSession().persist(category);
  165. return true;
  166. }
  167. catch (Exception e)
  168. {
  169. e.printStackTrace();
  170. return false;
  171. }
  172. }
  173.  
  174. }
  175.  
  176. **Category**
  177.  
  178. package com.app.EcommerceBackend.dto;
  179.  
  180. import java.io.Serializable;
  181.  
  182. import javax.persistence.Column;
  183. import javax.persistence.Entity;
  184. import javax.persistence.GeneratedValue;
  185. import javax.persistence.GenerationType;
  186. import javax.persistence.Id;
  187.  
  188. @Entity
  189. public class Category implements Serializable{
  190.  
  191. @Id
  192. @GeneratedValue(strategy = GenerationType.IDENTITY)
  193. private int id;
  194. private String name;
  195. private String description;
  196.  
  197. @Column(name = "image_url")
  198. private String imageURL;
  199.  
  200. @Column(name = "is_active")
  201. private boolean active = true;
  202.  
  203. @Override
  204. public String toString() {
  205. return "Category [id=" + id + ", name=" + name + ", description=" + description + ", imageURL=" + imageURL
  206. + ", active=" + active + "]";
  207. }
  208.  
  209. public int getId() {
  210. return id;
  211. }
  212.  
  213. public void setId(int id) {
  214. this.id = id;
  215. }
  216.  
  217. public String getName() {
  218. return name;
  219. }
  220.  
  221. public void setName(String name) {
  222. this.name = name;
  223. }
  224.  
  225. public String getDescription() {
  226. return description;
  227. }
  228.  
  229. public void setDescription(String description) {
  230. this.description = description;
  231. }
  232.  
  233. public String getImageURL() {
  234. return imageURL;
  235. }
  236.  
  237. public void setImageURL(String imageURL) {
  238. this.imageURL = imageURL;
  239. }
  240.  
  241. public boolean isActive() {
  242. return active;
  243. }
  244.  
  245. public void setActive(boolean active) {
  246. this.active = active;
  247. }
  248. }
  249.  
  250. package com.app.EcommerceBackend.test;
  251.  
  252. import static org.junit.Assert.assertEquals;
  253.  
  254. import java.sql.SQLException;
  255.  
  256. import org.junit.BeforeClass;
  257. import org.junit.Test;
  258. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  259.  
  260. import com.app.EcommerceBackend.dao.CategoryDAO;
  261. import com.app.EcommerceBackend.dto.Category;
  262.  
  263. public class CategoryTestCase {
  264.  
  265. private static AnnotationConfigApplicationContext context;
  266.  
  267. private static CategoryDAO categoryDAO;
  268.  
  269. private Category category;
  270.  
  271. @BeforeClass
  272. public static void init() throws Exception {
  273. org.h2.tools.Server.createTcpServer().start();
  274. context = new AnnotationConfigApplicationContext();
  275. context.scan("com.app.EcommerceBackend");
  276. context.refresh();
  277. categoryDAO = (CategoryDAO) context.getBean("categoryDAO");
  278. }
  279.  
  280. @Test
  281. public void testAddCategory() {
  282.  
  283. category = new Category();
  284.  
  285. category.setName("Laptop");
  286. category.setDescription("This is some description for laptop!");
  287. category.setImageURL("CAT_105.png");
  288.  
  289. assertEquals("Successfully added a category inside the table!", true, categoryDAO.add(category));
  290.  
  291. }
  292. }
  293.  
  294. org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
  295. at org.springframework.orm.hibernate5.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:542)
  296. at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
  297. at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:447)
  298. at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:277)
  299. at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
  300. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
  301. at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
  302. at com.sun.proxy.$Proxy29.add(Unknown Source)
  303. at com.app.EcommerceBackend.test.CategoryTestCase.testAddCategory(CategoryTestCase.java:40)
Add Comment
Please, Sign In to add comment