Wednesday 29 January 2014

Hibernate JSF integration

Hibernate JSF integration

                         Hello Friends,today i am going to demonstrate you how hibernate orm framework can be integrated with JavaServerFaces.well for sake of simplicity i am not using annotation based configuration.i am pasting my code as given below.i am going to use traditional Student class for demonstrating the concept :

ManagedBean

package org.managedbean;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import org.dao.StudentDAO;


@ManagedBean(name="student")
@SessionScoped
public class StudentBean
{

        private int id;
        private String name;
        private String college;
        @ManagedProperty(value="#{studentDAO}")
        private StudentDAO dao;
       
       
        public StudentDAO getDao() {
            return dao;
        }
        public void setDao(StudentDAO dao) {
            this.dao = dao;
        }
        public StudentBean() {
            // TODO Auto-generated constructor stub
           
        }
        public StudentBean(int id,String name,String college)
        {
            this.id=id;
            this.name=name;
            this.college=college;
           
        }
       
        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 getCollege() {
            return college;
        }
        public void setCollege(String college) {
            this.college = college;
        }
       
        public void insert()
        {
            getDao().insert(this);

        }
       
}
 

DAO LAYER:
package org.managedbean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import org.dao.StudentDAO;

@ManagedBean(name="student")
@SessionScoped
public class StudentBean
{

        private int id;
        private String name;
        private String college;
        @ManagedProperty(value="#{studentDAO}")
        private StudentDAO dao;
      
      
        public StudentDAO getDao() {
            return dao;
        }
        public void setDao(StudentDAO dao) {
            this.dao = dao;
        }
        public StudentBean() {
            // TODO Auto-generated constructor stub
          
        }
        public StudentBean(int id,String name,String college)
        {
            this.id=id;
            this.name=name;
            this.college=college;
          
        }
      
        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 getCollege() {
            return college;
        }
        public void setCollege(String college) {
            this.college = college;
        }
      
        public void insert()
        {
            getDao().insert(this);

        }
     
}

Util class:( for creating session factory)
package org.util;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

@ManagedBean
@SessionScoped
public class HibernateUtil
{
   
    private static final SessionFactory sessionfactory=buildSessionFactory();

    public static SessionFactory buildSessionFactory()
    {   
       
        try {
            return new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    public static SessionFactory getSessionfactory() {
        return sessionfactory;
       
    }
  }
hibernate.cfg.xml-Configuration file
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  
    <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sample</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">admin!@#</property>
    <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
    <mapping resource="Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Student.cfg.xml -mapping file
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
     <class name="org.managedbean.StudentBean" table="student" catalog="sample">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="name" type="string">
            <column name="name" length="45" not-null="true" />
        </property>
        <property name="college" type="string">
            <column name="college" length="45" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

data.xhtml
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"     
      xmlns:h="http://java.sun.com/jsf/html">

    <h:head>
        <title>Hibernate + JSF CRUD</title>
    </h:head>
    <h:body>
       
        <h:form>
        <table>
        <tr>
        <td><h:outputLabel value="id"> </h:outputLabel>
       
        <h:inputText id="id" value="#{student.id}"></h:inputText></td>
        </tr>
       
        <tr>
        <td><h:outputLabel value="name"> </h:outputLabel>
       
        <h:inputText id="name" value="#{student.name}"></h:inputText></td>
        </tr>
       
        <tr>
        <td><h:outputLabel value="college"> </h:outputLabel>
       
        <h:inputText id="college" value="#{student.college}"></h:inputText></td>
        </tr>
       
        </table>
           <h:commandButton value="Insert"   action="#{student.insert}"></h:commandButton>
        </h:form>
    </h:body>
</html>

index.jsp
<jsp:forward page="data.xhtml"></jsp:forward>



here is the entry in the mysql:


i hope you will get my point.for any query related to this topic ping me on pathak.nisarg@yahoo.com
thanks for reading  this post,in the next post i am going to demonstrate  how we can reverse engineer on hibernate ORM.
Regards



2 comments:

Spring Boot SSL configuration -Tomcat Server

Hi Friends hope you all are doing well. Today I am going to demonstrate about how to configure SSL in Spring boot web Application. Need o...