Guest User

Untitled

a guest
Oct 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.88 KB | None | 0 0
  1. Exception encountered during context initialization - cancelling refresh attempt:
  2. org.springframework.beans.factory.UnsatisfiedDependencyException:
  3. Error creating bean with name 'categoryDAO': Unsatisfied dependency expressed through field
  4. 'sessionFactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
  5. No qualifying bean of type 'org.hibernate.SessionFactory' available:
  6. expected at least 1 bean which qualifies as autowire candidate.
  7. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  8.  
  9. @Entity
  10. public class Category {
  11.  
  12. /*
  13. * private fields
  14. */
  15. @Id
  16. @GeneratedValue(strategy = GenerationType.IDENTITY)
  17. private int id;
  18. private String name;
  19. private String description;
  20. @Column(name = "img_url")
  21. private String imageurl;
  22. @Column(name = "is_active")
  23. private boolean active = true;
  24.  
  25. /*
  26. * getters and setters
  27. */
  28.  
  29. public int getId() {
  30. return id;
  31. }
  32.  
  33. public void setId(int id) {
  34. this.id = id;
  35. }
  36.  
  37. public String getName() {
  38. return name;
  39. }
  40.  
  41. public void setName(String name) {
  42. this.name = name;
  43. }
  44.  
  45. public String getDescription() {
  46. return description;
  47. }
  48.  
  49. public void setDescription(String description) {
  50. this.description = description;
  51. }
  52.  
  53. public String getImageurl() {
  54. return imageurl;
  55. }
  56.  
  57. public void setImageurl(String imageurl) {
  58. this.imageurl = imageurl;
  59. }
  60.  
  61. public boolean isActive() {
  62. return active;
  63. }
  64.  
  65. public void setActive(boolean active) {
  66. this.active = active;
  67. }
  68.  
  69. @Override
  70. public String toString() {
  71. return "Category [id=" + id + ", name=" + name + ", description=" + description + ", imageurl=" + imageurl
  72. + ", active=" + active + "]";
  73. }
  74. }
  75.  
  76. @Controller
  77. public class PageController {
  78.  
  79. @Autowired
  80. private CategoryDAO categoryDAO;
  81.  
  82. @RequestMapping(value = { "/", "/home", "/index" })
  83. public ModelAndView index() {
  84. ModelAndView mv = new ModelAndView("page");
  85. mv.addObject("title", "home");
  86.  
  87. //passing the list of categories
  88. mv.addObject("categories", categoryDAO.list());
  89. mv.addObject("userClickHome", true);
  90.  
  91. return mv;
  92. }
  93.  
  94. @RequestMapping(value = { "/about" })
  95. public ModelAndView about() {
  96. ModelAndView mv = new ModelAndView("page");
  97. mv.addObject("title", "About us");
  98. mv.addObject("userClickAbout", true);
  99. return mv;
  100. }
  101.  
  102. @RequestMapping(value = { "/contact" })
  103. public ModelAndView contact() {
  104. ModelAndView mv = new ModelAndView("page");
  105. mv.addObject("title", "Contact us");
  106. mv.addObject("userClickContact", true);
  107. return mv;
  108. }
  109.  
  110. @RequestMapping(value = "/show/all/products")
  111. public ModelAndView showAllProducts() {
  112. ModelAndView mv = new ModelAndView("page");
  113. mv.addObject("title", "All products");
  114.  
  115. //passing the list of categories
  116. mv.addObject("categories", categoryDAO.list());
  117. mv.addObject("userClickAllProducts", true);
  118. return mv;
  119. }
  120.  
  121. @RequestMapping(value = "/show/category/{id}/products")
  122. public ModelAndView showCategoryProducts(@PathVariable("id") int id) {
  123. ModelAndView mv = new ModelAndView("page");
  124.  
  125. // categoryDAO to fetch a single category
  126. Category category = null;
  127.  
  128. category = categoryDAO.get(id);
  129.  
  130. mv.addObject("title", category.getName());
  131.  
  132. //passing the list of categories
  133. mv.addObject("categories", categoryDAO.list());
  134.  
  135. //passing the list of category object
  136. mv.addObject("category", category);
  137.  
  138. mv.addObject("userClickCategoryProducts", true);
  139.  
  140. return mv;
  141. }
  142. }
  143.  
  144. @Repository("categoryDAO")
  145. public class CategoryDAOImpl implements CategoryDAO {
  146.  
  147. @Autowired
  148. private SessionFactory sessionFactory;
  149. private static List<Category> categories = new ArrayList<>();
  150. //private static CategoryDAO categoryDAO;
  151.  
  152. static {
  153.  
  154. Category category = new Category();
  155.  
  156. // first category
  157. category.setId(1);
  158. category.setName("Television");
  159. category.setDescription("This is some description for television");
  160. category.setImageurl("CAT_1.png");
  161.  
  162. categories.add(category);
  163.  
  164. // second category
  165. category = new Category();
  166. category.setId(2);
  167. category.setName("Mobile");
  168. category.setDescription("This is some description for mobile");
  169. category.setImageurl("CAT_2.png");
  170.  
  171. categories.add(category);
  172.  
  173. // third category
  174. category = new Category();
  175. category.setId(3);
  176. category.setName("laptop");
  177. category.setDescription("This is some description for laptop");
  178. category.setImageurl("CAT_3.png");
  179.  
  180. categories.add(category);
  181. }
  182.  
  183. @Override
  184. public List<Category> list() {
  185. // TODO Auto-generated method stub
  186. return categories;
  187. }
  188.  
  189. @Override
  190. public Category get(int id) {
  191.  
  192. //ecnhanced for loop
  193.  
  194. for(Category category : categories) {
  195. if(category.getId() == id) return category;
  196. }
  197. return null;
  198. }
  199.  
  200. @Override
  201. @Transactional
  202. public boolean add(Category category) {
  203. try {
  204. //add the category to the database table
  205. sessionFactory.getCurrentSession().persist(category);
  206. return true;
  207. } catch (Exception e) {
  208. e.printStackTrace();
  209. return false;
  210. }
  211. }
  212.  
  213. }
  214.  
  215. @Service
  216. public interface CategoryDAO {
  217. List<Category> list();
  218. Category get(int id);
  219. boolean add(Category category);
  220. }
  221.  
  222. @Configuration
  223. @ComponentScan(basePackages = {"net.kzn.shoppingbackend.dao"})
  224. @EnableTransactionManagement
  225. public class HibernateConfig {
  226.  
  227. // change the below based on the DBMS you choose
  228. private final static String DATABASE_URL = "jdbc:h2:tcp://localhost/~/onlineshopping";
  229. private final static String DATABASE_DRIVER = "org.h2.Driver";
  230. private final static String DATABASE_DIALECT = "org.hibernate.dialect.H2Dialect";
  231. private final static String DATABASE_USERNAME = "sa";
  232. private final static String DATABASE_PASSWORD = "";
  233.  
  234. // database bean will be available
  235. @Bean
  236. public DataSource getDataSource() {
  237. BasicDataSource dataSource = new BasicDataSource();
  238.  
  239. // providing the database connection information
  240.  
  241. dataSource.setDriverClassName(DATABASE_DRIVER);
  242. dataSource.setUrl(DATABASE_URL);
  243. dataSource.setUsername(DATABASE_USERNAME);
  244. dataSource.setPassword(DATABASE_PASSWORD);
  245.  
  246. return dataSource;
  247. }
  248.  
  249. // sessionFactory bean will be available
  250. public SessionFactory getSessionFactory(DataSource dataSource) {
  251.  
  252. LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);
  253.  
  254. builder.addProperties(getHibernateProperties());
  255. builder.scanPackages("net.kzn.shoppingbackend.dto");
  256.  
  257. return builder.buildSessionFactory();
  258. }
  259.  
  260. // All the hibernate properties will be returned in this method
  261. private Properties getHibernateProperties() {
  262.  
  263. Properties properties = new Properties();
  264.  
  265. properties.put("hibernate.dialect", DATABASE_DIALECT);
  266. properties.put("hibernate.show_sql", "true");
  267. properties.put("hibernate.format_sql", "true");
  268.  
  269. return properties;
  270. }
  271.  
  272. // transactionManager bean
  273.  
  274. public HibernateTransactionManager geTransactionManager(SessionFactory sessionFactory) {
  275.  
  276. HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
  277. return transactionManager;
  278. }
  279.  
  280. }
  281.  
  282. public class CategoryTestCase {
  283.  
  284. private static AnnotationConfigApplicationContext context;
  285. private static CategoryDAO categoryDAO;
  286.  
  287. private Category category;
  288.  
  289. @BeforeClass
  290. public static void init() {
  291. context = new AnnotationConfigApplicationContext();
  292. context.scan("net.kzn.shoppingbackend");
  293. context.refresh();
  294. categoryDAO = (CategoryDAO) context.getBean("categoryDAO");
  295. }
  296.  
  297. @Test
  298. public void testAddCategory() {
  299.  
  300. category = new Category();
  301.  
  302. category.setName("Television");
  303. category.setDescription("This is some description for television");
  304. category.setImageurl("CAT_1.png");
  305.  
  306. assertEquals("successfully added a category inside the table!", true, categoryDAO.add(category));
  307. }
  308. }
  309.  
  310. <project xmlns="http://maven.apache.org/POM/4.0.0"
  311. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  312. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  313. <modelVersion>4.0.0</modelVersion>
  314.  
  315. <groupId>net.kzn</groupId>
  316. <artifactId>shoppingbackend</artifactId>
  317. <version>0.0.1-SNAPSHOT</version>
  318. <packaging>jar</packaging>
  319.  
  320. <name>shoppingbackend</name>
  321. <url>http://maven.apache.org</url>
  322.  
  323. <properties>
  324. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  325. <spring.version>4.3.19.RELEASE</spring.version>
  326. <hibernate.version>5.2.7.Final</hibernate.version>
  327. </properties>
  328.  
  329. <dependencies>
  330. <!-- JUNIT version 4.12 -->
  331. <dependency>
  332. <groupId>junit</groupId>
  333. <artifactId>junit</artifactId>
  334. <version>4.12</version>
  335. <scope>test</scope>
  336. </dependency>
  337.  
  338. <!-- spring -->
  339. <dependency>
  340. <groupId>org.springframework</groupId>
  341. <artifactId>spring-context</artifactId>
  342. <version>${spring.version}</version>
  343. </dependency>
  344.  
  345. <dependency>
  346. <groupId>org.springframework</groupId>
  347. <artifactId>spring-orm</artifactId>
  348. <version>${spring.version}</version>
  349. </dependency>
  350.  
  351. <!-- H2 database -->
  352. <dependency>
  353. <groupId>com.h2database</groupId>
  354. <artifactId>h2</artifactId>
  355. <version>1.4.197</version>
  356. </dependency>
  357.  
  358. <!-- Hibernate Dependency -->
  359. <dependency>
  360. <groupId>org.hibernate</groupId>
  361. <artifactId>hibernate-core</artifactId>
  362. <version>${hibernate.version}</version>
  363. <exclusions>
  364. <exclusion>
  365. <groupId>dom4j</groupId>
  366. <artifactId>dom4j</artifactId>
  367. </exclusion>
  368. </exclusions>
  369. </dependency>
  370.  
  371. <!-- database connection pooling -->
  372. <dependency>
  373. <groupId>org.apache.commons</groupId>
  374. <artifactId>commons-dbcp2</artifactId>
  375. <version>2.1.1</version>
  376. </dependency>
  377.  
  378. </dependencies>
  379.  
  380. <build>
  381. <plugins>
  382. <plugin>
  383. <artifactId>maven-compiler-plugin</artifactId>
  384. <version>3.1</version>
  385. <configuration>
  386. <source>1.8</source>
  387. <target>1.8</target>
  388. </configuration>
  389. </plugin>
  390. </plugins>
  391. </build>
  392. </project>
Add Comment
Please, Sign In to add comment