mardi 10 juin 2014

Spring, Hibernate and WebLogic 12C integration


In this page I will present the configuration to integrate Spring, Hibernate and Weblogic 12C and give the reasons why this combination is good.



Spring:
 Spring is a framework for building J2EE applications, its power resides in the simplicity and the flexibility in terms of development. Spring provides the best solutions for developing J2EE applications as it overcomes the problems encountered  in other frameworks like EJB.




Hibernate:
Hibernate a very popular ORM (Object-Relational Mapping) framework, it


WebLogic Server 12C
Weblogic 12C is a full J2EE application server, used for deploying enterprise applications and supports high availability, clustering, workload, balancing and monitoring. Its architecture permits a very advanced environment configuration and control. It is the version  that supports J2EE-6, all previous versions (10.3.x(6,5,,,) ) are J2EE-5 or lower. This version, 12C,  supports JPA2 as a persistence layer among others supported one's  like TOPLINK, and KODO that are default installed in the application server.




Configuration:


  1. Server configuration:
     First
    , create a data source in the Weblogic server to your database with a name of your choice that must satisfy the notation used for jndi sources: preferably jndi/datadsource_name, I will use appsDataSource to name the datasource in the weblogic server.


    The key file in the configuration is the web.xml, here is  what you need to include in the file to get you datasource recognised:
       
         <persistence-unit-ref>
                <description>
                    Persistence context for the inventory management application.
                </description>
                <persistence-context-ref-name>
                    persistence/appsDataSource
                </persistence-context-ref-name>
                <persistence-unit-name>
                    appsPU                 <!-- Defined in persistenc.xml -->
                </persistence-unit-name>
    <persistence-unit-ref>
  2. Persistence Layer.
    add the following to the standard configuration, but dont forget to name your persistence unit appsPU 
         <property name="hibernate.transaction.manager_lookup_class"
      value="org.hibernate.transaction.WeblogicTransactionManagerLookup" />
      <property name="hibernate.transaction.jta.platform"
      value="org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform" />
     this enables JTA and informs the server that the persistence provider is Hibernate.
  3. Spring Configuration.
    for the beans.xml, do as follows:
     <jee:jndi-lookup id="appsPU" jndi-name="persistence/appsDataSource"/>










samedi 26 avril 2014

Spring, hibernate and Jboss integration:
configuration tested on JBoss 7.0.2 
  in this article I will present the spring, hibernate and JBoss AS 7 integration. DataSource is a JTA data source and will be retrieved from the JEE container (JBoss) and it is named java:jboss/datasources/appsDataSource



Dependencies:
...
 <properties>
<org.springframework.version>3.0.7.RELEASE</org.springframework.version>
<version.org.hibernate3.commons.annotations>3.2.0.Final</version.org.hibernate3.commons.annotations>
<version.org.hibernate>1.0.1.Final</version.org.hibernate>
<version.org.hibernate.core>3.6.8.Final</version.org.hibernate.core>
<version.org.hibernate.em>3.6.8.Final</version.org.hibernate.em>
<version.org.hibernate.infinispan>3.6.8.Final</version.org.hibernate.infinispan>
<version.org.hibernate.validator>3.1.0.GA</version.org.hibernate.validator>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>2.7.10</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.7.10</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>jboss</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<app.server>jboss</app.server>
</properties>
<dependencies>
<!-- Import the JPA API, we use provided scope as the API is included
in JBoss AS 7 -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>${version.org.hibernate}</version>
<scope>provided</scope>
</dependency>
<!-- Define hiberante artifacts versions -->
<!-- Define dependency version of hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${version.org.hibernate.core}</version>
</dependency>
<!-- Define dependency version of hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${version.org.hibernate.em}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${version.org.hibernate.validator}</version>
</dependency>
<!-- Define dependency version of hibernate-commons-annotations -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>${version.org.hibernate3.commons.annotations}</version>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
<version>5.0.1.FINAL</version>
</dependency>
<!-- Define dependency version of hibernate-infinispan -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-infinispan</artifactId>
<version>${version.org.hibernate.infinispan}</version>
<exclusions>
<exclusion>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

...   





persistence.xml :
<persistence-unit name="appsPU" transaction-type="JTA">
 
    <jta-data-source>java:jboss/datasources/appsDataSource</jta-data-source>
    <shared-cache-mode>NONE</shared-cache-mode>
<properties>
<property name="hibernate.query.jpaql_strict_compliance" value="false" />
<property name="hibernate.jdbc.use_streams_for_binary" value="false" />
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<!-- SQL stdout logging -->
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.use_sql_comments" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.connection.autocommit" value="false" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.query.jpaql_strict_compliance" value="false" />
<property name="hibernate.cache.use_query_cache" value="false" />
<property name="hibernate.cache.use_second_level_cache" value="false" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" />
<property name="hibernate.cglib.use_reflection_optimizer" value="false" />
<property name="hibernate.generate_statistics" value="true" />
<property name="org.hibernate.cacheable" value="false"/>
<property name="hibernate.generate_statistics" value="true" />
<property name="javax.persistence.retrieveMode" value="BYPASS" />
<property name="javax.persistence.storeMode" value="REFRESH" />
<property name="javax.persistence.sharedCache.mode" value="NONE" />
<!-- <property name="jboss.entity.manager.jndi.name" value="java:/ebankingPU"/> -->
<property name="jboss.as.jpa.providerModule" value="hibernate3-bundled" />
<property name="jboss.entity.manager.factory.jndi.name" value="java:/appsPUFactory" />
</properties>
</persistence-unit>





applicationContext.xml :
...
<jee:jndi-lookup id="appsPU"  jndi-name="java:/appsPUFactory"/>
<tx:annotation-driven />
<tx:jta-transaction-manager/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
....

The transactions and data source are managed by the application server as it is a full J2EE container so it provides better performance in managing those critical resources. This is a best practice, delegate the work to the container as possible as you can (or are the choice fits your desire).


For the DAO, use the following to get the Entity Manager from the spring context (created using the previous <jee:jndi-loopkup  .....   )

@PersistenceContext(unitName="appsPU", type = PersistenceContextType.EXTENDED) private EntityManager em;
this instructs Spring to retrieve the Entity Manager from the Persistence Unit in the application Server:
  The persistence.xml creates the entity manager from the data source in the server and provides it as a resource under the name   java:/appsPUFactory in Spring context.

Hope this helps, Please feel free to post your comments, if you facing troubles please don't hesitate to post it in the comment, I will be very thankful for any comment posted, it will make me improve the content and share usable and reliable information.
Thank you!