Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.app.EcommerceBackend.config;
- import java.util.Properties;
- import javax.sql.DataSource;
- import org.apache.commons.dbcp2.BasicDataSource;
- import org.hibernate.SessionFactory;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.core.annotation.Order;
- import org.springframework.orm.hibernate5.HibernateTransactionManager;
- import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
- @Configuration
- @ComponentScan(basePackages={"com.app.EcommerceBackend.dto"})
- @EnableTransactionManagement
- public class HibernateConfig {
- // Change the below based on the DBMS you choose
- private final static String DATABASE_URL = "jdbc:h2:tcp://localhost/~/ecommerce";
- private final static String DATABASE_DRIVER = "org.h2.Driver";
- private final static String DATABASE_DIALECT = "org.hibernate.dialect.H2Dialect";
- private final static String DATABASE_USERNAME = "sa";
- private final static String DATABASE_PASSWORD = "";
- // dataSource bean will be available
- @Bean
- public DataSource getDataSource() {
- BasicDataSource dataSource = new BasicDataSource();
- // Providing the database connection information
- dataSource.setDriverClassName(DATABASE_DRIVER);
- dataSource.setUrl(DATABASE_URL);
- dataSource.setUsername(DATABASE_USERNAME);
- dataSource.setPassword(DATABASE_PASSWORD);
- return dataSource;
- }
- // sessionFactory bean will be available
- @Bean
- public SessionFactory getSessionFactory(DataSource dataSource) {
- LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);
- builder.addProperties(getHibernateProperties());
- builder.scanPackages("com.app.EcommerceBackend.dto");
- return builder.buildSessionFactory();
- }
- // All the hibernate properties will be returned in this method
- private Properties getHibernateProperties() {
- Properties properties = new Properties();
- properties.put("hibernate.dialect", DATABASE_DIALECT);
- properties.put("hibernate.show_sql", "true");
- properties.put("hibernate.format_sql", "true");
- //properties.put("hibernate.hbm2ddl.auto", "create");
- return properties;
- }
- // transactionManager bean
- @Bean
- public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
- HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
- return transactionManager;
- }
- }
- package com.app.EcommerceBackend.daoImpl;
- import java.util.ArrayList;
- import java.util.List;
- import org.hibernate.SessionFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Repository;
- import org.springframework.transaction.annotation.Transactional;
- import com.app.EcommerceBackend.dao.CategoryDAO;
- import com.app.EcommerceBackend.dto.Category;
- @Repository("categoryDAO")
- @Transactional
- public class CategoryDAOImpl implements CategoryDAO
- {
- @Autowired
- private SessionFactory sessionFactory;
- private static List<Category> categories = new ArrayList<>();
- static {
- Category category = new Category();
- // Add First Element
- category.setId(1);
- category.setName("Television");
- category.setDescription("this is some desc for television");
- category.setImageURL("img1.jpg");
- categories.add(category);
- // Add Second Element
- category = new Category();
- category.setId(2);
- category.setName("Mobile");
- category.setDescription("this is some desc for Mobile");
- category.setImageURL("img2.jpg");
- categories.add(category);
- // Add Third Element
- category = new Category();
- category.setId(3);
- category.setName("Laptop");
- category.setDescription("this is some desc for Laptop");
- category.setImageURL("img3.jpg");
- categories.add(category);
- }
- @Override
- public List<Category> list() {
- return categories;
- }
- @Override
- public Category get(int id) {
- for (Category category : categories) {
- if (category.getId() == id)
- return category;
- }
- return null;
- }
- @Override
- public boolean add(Category category) {
- //add the category to the databse table
- try
- {
- sessionFactory.getCurrentSession().persist(category);
- return true;
- }
- catch (Exception e)
- {
- e.printStackTrace();
- return false;
- }
- }
- }
- **Category**
- package com.app.EcommerceBackend.dto;
- import java.io.Serializable;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- @Entity
- public class Category implements Serializable{
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private int id;
- private String name;
- private String description;
- @Column(name = "image_url")
- private String imageURL;
- @Column(name = "is_active")
- private boolean active = true;
- @Override
- public String toString() {
- return "Category [id=" + id + ", name=" + name + ", description=" + description + ", imageURL=" + imageURL
- + ", active=" + active + "]";
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- public String getImageURL() {
- return imageURL;
- }
- public void setImageURL(String imageURL) {
- this.imageURL = imageURL;
- }
- public boolean isActive() {
- return active;
- }
- public void setActive(boolean active) {
- this.active = active;
- }
- }
- package com.app.EcommerceBackend.test;
- import static org.junit.Assert.assertEquals;
- import java.sql.SQLException;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- import com.app.EcommerceBackend.dao.CategoryDAO;
- import com.app.EcommerceBackend.dto.Category;
- public class CategoryTestCase {
- private static AnnotationConfigApplicationContext context;
- private static CategoryDAO categoryDAO;
- private Category category;
- @BeforeClass
- public static void init() throws Exception {
- org.h2.tools.Server.createTcpServer().start();
- context = new AnnotationConfigApplicationContext();
- context.scan("com.app.EcommerceBackend");
- context.refresh();
- categoryDAO = (CategoryDAO) context.getBean("categoryDAO");
- }
- @Test
- public void testAddCategory() {
- category = new Category();
- category.setName("Laptop");
- category.setDescription("This is some description for laptop!");
- category.setImageURL("CAT_105.png");
- assertEquals("Successfully added a category inside the table!", true, categoryDAO.add(category));
- }
- }
- org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
- at org.springframework.orm.hibernate5.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:542)
- at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
- at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:447)
- at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:277)
- at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
- at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
- at com.sun.proxy.$Proxy29.add(Unknown Source)
- at com.app.EcommerceBackend.test.CategoryTestCase.testAddCategory(CategoryTestCase.java:40)
Add Comment
Please, Sign In to add comment