Java Web Uygulaması Hibernate ve Bonecp Kullanımı

01-12-2013
Java'da Maven, Hibernate ve Bonecp kullanarak web uygulaması yapmak için bazı ayarların yapılması gereklidir. Bonecp, web uygulamaların vazgeçilmezi olan jdbc veri havuzu modülü için yaygın olarak kullanılan açık kaynak ve en hızlı veri havuzu kütüphanesidir. Hibernate ise Java Frameworkleri arasında en yaygın kullanılan Object Relational Mapping(ORM) kütüphanesidir

WEB-INF/hibernate-cfg.xml Dosyası
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
<!--org.hibernate.dialect.DB2390Dialect is for DB2 Connection. 
If you want to use mysql then write this: org.hibernate.dialect.MySQL5Dialect or org.hibernate.dialect.MySQLDialect  -->
        <property name="dialect">org.hibernate.dialect.DB2390Dialect</property>
        <mapping class="edu.javaProgramming.jsfSpringHibernateBonecp.hibernateClasses.gender.Gender"/>
<!-- edu.javaProgramming.jsfSpringHibernateBonecp.hibernateClasses.gender.Gender is just a sample class. 
This class is an Entity Class . This class is given at the bottom of this document.-->
    </session-factory>
</hibernate-configuration>

WEB-INF/spring-servlet.xml Konfigurasyonu
<?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:context="http://www.springframework.org/schema/context"

       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="edu"/> <!—edu is just a sample in our projects.  -->

 <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.connection.provider_class">com.jolbox.bonecp.provider.BoneCPConnectionProvider
                </prop>
                <prop key="hibernate.connection.driver_class">com.ibm.db2.jcc.DB2Driver</prop>
                <prop key="hibernate.connection.url">jdbc:db2://127.0.0.1:50000/BLOG</prop>
                <prop key="hibernate.connection.username">root</prop>
                <prop key="hibernate.connection.password">123456</prop>
                <prop key="bonecp.idleMaxAge">240</prop>
                <prop key="bonecp.idleConnectionTestPeriod">60</prop>
                <prop key="bonecp.partitionCount">3</prop>
                <prop key="bonecp.acquireIncrement">1</prop>
                <prop key="bonecp.maxConnectionsPerPartition">10</prop>
                <prop key="bonecp.minConnectionsPerPartition">2</prop>
                <prop key="bonecp.statementsCacheSize">50</prop>
                <prop key="bonecp.releaseHelperThreads">3</prop>
                <prop key="show_sql">true</prop>
            </props>
        </property>
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
        <property name="configLocation" value="/WEB-INF/hibernate.cfg.xml"/>

    </bean>

     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

      <!--View Scope provider. To use org.springframework.context.annotation.Scope annotation in a bean. Usage @Scope("view"). 
That’s @Scope("view") equals to @ViewScoped in JSF. Note @ViewScoped annotation is defined in javax.faces.bean -->
    <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
        <property name="scopes">
            <map>
                <entry key="view">
                    <bean class="edu.javaProgramming.jsfSpringHibernateBonecp.configuration.ViewScope"/>
                </entry>
            </map>
        </property>
    </bean>

ViewScope Sınıfı
package edu.javaProgramming.jsfSpringHibernateBonecp.configuration;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;

import javax.faces.context.FacesContext;
import java.util.Map;

/**
 * Implements the JSF View Scope for use by Spring. This class is registered as a Spring bean with the CustomScopeConfigurer.
*/
public class ViewScope implements Scope {

	public Object get(String name, ObjectFactory<?> objectFactory) {
		if (FacesContext.getCurrentInstance().getViewRoot() != null) {
			Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
			if (viewMap.containsKey(name)) {
				return viewMap.get(name);
			} else {
				Object object = objectFactory.getObject();
				viewMap.put(name, object);
				return object;
			}
		} else {
			return null;
		}
	}

	public Object remove(String name) {
		if (FacesContext.getCurrentInstance().getViewRoot() != null) {
			return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
		} else {
			return null;
		}
	}

	public void registerDestructionCallback(String name, Runnable callback) {
		// Do nothing
	}

	public Object resolveContextualObject(String key) {
		return null;
	}

	public String getConversationId() {
		return null;
	}

}
Örnek Bir Entity Sınıfı
import javax.persistence.*;

@Entity //it must be javax.persistence.Entity 
@Table(name = "GENDER", schema = "BLOG")
public class Gender {
    private Integer id;
    private String name;

    public Gender(String name) {
        this.name = name;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    @Column(name="AD")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Hibernate Maven Dependencies
<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
        <dependency>
            <groupId>hibernate</groupId>
            <artifactId>hibernate-tools</artifactId>
            <version>3.2.3.GA</version>
        </dependency>
Bonecp Maven Dependencies
     <dependency>
            <groupId>com.jolbox</groupId>
            <artifactId>bonecp</artifactId>
            <version>0.7.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.jolbox</groupId>
            <artifactId>bonecp-provider</artifactId>
            <version>0.7.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.jolbox</groupId>
            <artifactId>bonecp-spring</artifactId>
            <version>0.7.1.RELEASE</version>
        </dependency>

© 2019 Tüm Hakları Saklıdır. Codesenior.COM