Search This Blog

Tuesday, 22 July 2014

Configuration of Hibernate session factory and JPA entity manager factory – in standalone and spring applications:




Configuration of Hibernate session factory and JPA entity manager factory – in standalone and spring applications:
 




Note: Place “hibernate.cfg.xml”,“persistence.xml” and etc configuration files under class path.


Creating session factory using Configuration class in standalone application:

private static final SessionFactory sessionFactory;
    static {
        try {
            sessionFactory = new Configuration().configure()
                    .buildSessionFactory();
        } catch (Throwable exception) {
            System.err.println("Initial SessionFactory creation failed." + exception);
            throw new ExceptionInInitializerError(exception);
        }
    }

Creating session factory using LocalSessionFactoryBean in spring application:

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/test_db" />
        <property name="username" value="root" />
        <property name="password" value="root1" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
    </bean>

Creating JPA Entity Manager Factory in standalone application:

        EntityManagerFactory entityManagerFactory=Persistence.createEntityManagerFactory("PersistenceUnitName");
        EntityManager entityManager=entityManagerFactory.createEntityManager();

Creating JPA Entity Manager Factory using LocalContainerEntityManagerFactoryBean in spring application:

        <bean
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        id="entityManagerFactory">
        <property name="dataSource"> <ref local="dataSource" /></property>
    </bean>

        <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/test_db" />
        <property name="username" value="root" />
        <property name="password" value="root1" />
    </bean>

Java Classes are not affected while running application on server in eclipse & class not found exception – Steps to avoid these issues





Java Classes are not affected while running application on server in eclipse & class not found exception – Steps to avoid these issues



Step 1: Check libraries added to project properly, if jar file are not added then add jars by using "Add jars".


Step 2: clean the project in eclipse and build the project.
 


Step 3: Undeploy the application in server and remove application folders from the server environment manually.
Step 4: Run the application on server again.

Monday, 21 July 2014

Simple Hibernate save - get - load –delete – entity class objects



Simple Hibernate save - get - load –delete – entity class objects



save 
====
public int saveIteam(Item item) {
        Integer itemCode = 0;
        try {
            log.info("Before saveItem - Dao start");
            session = getSessionFactory().openSession();
            transaction = session.beginTransaction();
           log.info("Before addItem - Dao");
            itemCode = (Integer) session.save(item);
            //item.setItemName("itemName");
            transaction.commit();
        } catch (HibernateException hibernateException) {
            transaction.rollback();
            log.info(hibernateException.getMessage());
        } finally {
            //session.close();
        }
        return itemCode;
    }


get
===
public Item getItem(int itemCode) {
        try {       
            session = getSessionFactory().openSession();
            transaction = session.beginTransaction();
            item = (Item) session.get(Item.class,itemCode);           
            transaction.commit();
        } catch (HibernateException hibernateException) {
            transaction.rollback();
            log.info(hibernateException.getMessage());
        } finally {
            session.close();
        }
        return item;
    }


load
====
public Item loadItem(int itemCode) {
        Item item = null;
        try {           
            session = getSessionFactory().openSession();
            transaction = session.beginTransaction();
            item = (Item) session.load(Item.class,itemCode);
           
            transaction.commit();
        } catch (HibernateException hibernateException) {
            transaction.rollback();
             log.info(hibernateException.getMessage());
        } finally {
            session.close();
        }
        return item;
    }


delete
======
public void deleteItem(int itemCode) {
        try {
           
            session = getSessionFactory().openSession();
            transaction = session.beginTransaction();
            Item item = new Item();
            //item.setItemCode(itemCode);
            item = (Item) session.load(Item.class,itemCode);
            session.delete(item);
            transaction.commit();
        } catch (HibernateException hibernateException) {
            transaction.rollback();
            log.info(hibernateException.getMessage());
        } finally {
            session.close();
        }
    }