Creation of Spring + Hibernate + Maven Application


1. Create a Maven Project in eclipse.

2.In the pom.xml, add the dependencies for Spring artifacts(spring-context, commons-loggingspring-core), for Hibernate artifacts(hibernate-core) and your database specific connector, for example for mysql(mysql-connector-java). For all these artifacts, search on maven repository and copy the required version dependency and place them inside <dependencies> tag of pom.xml.

3. Create  a POJO Employee(a simple java class and name it Employee.java), define the properties in the class and provide getter and setter methods for the properties of Employee class.

Employee.java

public class Employee {

        private String name;
        private int id;
        public String getName() {
               return name;
        }
        public void setName(String name) {
               this.name = name;
        }
        public int getId() {
               return id;
        }
        public void setId(int id) {
               this.id = id;
        }
}

4. Create a database testDB in mysql (use the command- create database testDB)

5. Create an xml file- Beans.xml (Spring Configuration file) and add metadata for Employee class.


And the Beans.xml files' contents should be as-

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id = "employee" class = "com.dao.Employee">
      <property name = "id" value = "1"/>
      <property name = "name" value = "John"/>
   </bean>

</beans>


6. Hibernate requires two configuration files, one to configure database and connection related details and other a mapping file for mapping class to table.

The hibernate configuration file should be like-
(The file name should be hibernate.cfg.xml and should be in src/main/resources)
<?xml version="1.0"?>
<hibernate-configuration>
<session-factory>
<!--  Connection Props -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/testDB</property>

<property name="connection.user">root</property>
<property name="connection.password"></property>

<!-- Hibernate properties -->
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQL55Dialect</property>
<property name="hbm2ddl.auto">create</property>

<!-- Mapping files -->
<mapping resource="./employee.hbm.xml"></mapping>

</session-factory>
</hibernate-configuration>

And the other mapping files should contain mapping as-
(The file name can be employee.hbm.xml and should be in src/main/resources)


<?xml version="1.0" ?>
<hibernate-mapping>
       <class name="com.dao.Employee" table="Employee">
               <id column="ID" name="id" type="int"/>
               <property name="name" type="string"/>
      </class>
</hibernate-mapping>

7. Now create the main method containing class to load all these configuration and bean files, create employee table and insert the data provided in Beans.xml for Employee class into employee table as a row.

public class MainApp {
public static void main(String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    Configuration cfg=new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory sf=cfg.buildSessionFactory();
    Session session=sf.openSession();
    Employee employee=(Employee)context.getBean("employee");
    Transaction tx=session.beginTransaction();
    session.save(employee);
    tx.commit();
    session.close();
    sf.close();
   }

}

Later, run the application as Java Application and check if the table is created and also if it has a row in it.

Later, to work on the same table, change the hbm2ddl.auto property's value in hibernate.cfg.cml from create to update. And change the id value in Beans.xml for employee, as ID is made primary key and would throw error if not changed.

And attching the source code to refer-
https://github.com/HarikaPunna/examples

There would occur lot of exceptions if the structure is not followed, one such working structure of files is attached-








Comments

Popular posts from this blog

IoC and DI in Spring