How To Show Data From Database Using SpringMVC + Hibernate + AngularJS

Hi.. in this article I will create simple web app using SpringMVC + Hibernate + AngularJS to show data from database. Some environment that I need are:

  1. SpringMVC libs.
  2. Hibernate libs.
  3. AngularJS.
  4. Another required libs like database connector, aopalliance, apache common.
  5. IDE. This time, I use netbeans.

I use mySQL as my database. The data that I want to show is from User table and has some data like this:

db

After create project that use SpringMVC and Hibernate framework, I setup application context, dispatcher servlet, and web.xml Continue reading “How To Show Data From Database Using SpringMVC + Hibernate + AngularJS”

Advertisements

How To Fix “Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]”

When I worked with my project wich using Hibernate + SpringMVC, I got this kind of error when I run my project:

SEVERE:   Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: 
start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'sampleController': 
Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: 
private com.ariestania.sample.service.LogUserActionService com.ariestania.sample.controller.BaseController.logUserActionService; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'logUserActionService': 
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: 
private org.hibernate.SessionFactory com.ariestania.sampleservice.LogUserActionService.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: 
Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]

I though that error occured because Continue reading “How To Fix “Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]””

How To Use Multiple Database Connections On SpringMVC + Hibernate

In my experience, I met a rare case which is I should use more than one database connection on my web application. In that case, what I did was create some hibernate configuration file and call it whenever I need it. I have one main database which is use for my web application it self, and some hibernate configuration for another purpose.

Here are some snap of my code. I use oracle for my example.

ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
      
    <context:component-scan base-package="com.ariestania.example" />
    <tx:annotation-driven /> 
 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
          p:driverClassName="oracle.jdbc.OracleDriver"
          p:url="jdbc:oracle:thin:@10.98.10.128:1521:examples"
          p:username="thisisuser"
          p:password="thisispass!"
          p:initialSize="5"
          p:maxActive="10"
          p:maxIdle="5"
          p:minIdle="2"
          p:maxWait="30000"
          p:removeAbandoned="true"
          p:removeAbandonedTimeout="30"
          p:validationQuery="SELECT 1 from dual"/>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
          p:dataSource-ref="dataSource"
          p:configLocation="classpath:hibernate.cfg.xml">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
          p:sessionFactory-ref="sessionFactory"/>

</beans>

Continue reading “How To Use Multiple Database Connections On SpringMVC + Hibernate”

How To Generate POJOs and Mapping File (Hibernate)

First of all, create new project which is use hibernate on the project. Then create database and table that you will use for the project. If you use ant, then you can add hibernate lib which is available from your IDE. But if you use maven, then add hibernate libs on your POM file. Mine is like this:

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.5.4-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.4-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-tools</artifactId>
            <version>3.2.4.GA</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.5.6</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>            
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>3.6.10.Final</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>

Right click on your source package then choose to create new hibernate configuration file.

new hibernate config Continue reading “How To Generate POJOs and Mapping File (Hibernate)”