Hello Spring Jpa and OSGi

0

源起

之前用 jboss ejb3 包 bundle 不是很順利,於是想用 spring jpa 支援當這個用途, 試看看 EntityManager 利用 spring 注入的方式。

Chapter 12. Object Relational Mapping (ORM) data access

Getting Started With JPA in Spring 2.0

Using JPA in Spring without referencing Spring

議題

先直接用 spring-osgi 包好的 bundle 來測,不過測試過程中不是很順利,目前 spring-osgi 所用到的 bundle 很多,但是 package 相依性還沒有調好,有些需要用 eclipse buddy 方式來轉包,既然要一個個轉包 (dao/jdbc/beans..),就直接包一起變成比較大包的 haha.remix.spring。

OSGi console 結果
osgi> ss

Framework is launched.

id    State       Bundle
0    ACTIVE      system.bundle_3.3.0.v20061101
5    ACTIVE      javax.servlet_2.4.0.200609281713
9    ACTIVE      org.apache.commons.logging_1.0.4
10    ACTIVE      org.eclipse.equinox.ds_1.0.0.v20060828
12    ACTIVE      org.eclipse.equinox.log_1.0.100.v20060717
13    ACTIVE      org.eclipse.osgi.services_3.1.100.v20060918
18    ACTIVE      org.springframework.osgi_2.0.0
68    ACTIVE      haha.hello.jpa.userdao_1.0.0
69    ACTIVE      haha.hello.jpa.userdao.client_1.0.0
70    ACTIVE      haha.remix.derby_1.0.0
71    ACTIVE      haha.remix.hibernate_1.0.0
73    ACTIVE      haha.remix.spring_1.0.0

haha.remix.spring

META-INF/MANIFEST.MF

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: haha.remix.spring
Bundle-SymbolicName: haha.remix.spring
Bundle-Version: 1.0.0
Bundle-Activator: haha.remix.spring.Activator
Bundle-Localization: plugin
Import-Package: javax.persistence,
 javax.persistence.spi,
 org.apache.commons.logging,
 org.hibernate,
 org.hibernate.ejb,
 org.osgi.framework;version="1.3.0" 
Bundle-ClassPath: lib/spring-beans-2.1-20061103.020011-52.jar,
 .,
 lib/spring-dao-2.1-20061007.091527-15.jar,
 lib/spring-jpa-2.1-20061103.020011-52.jar,
 lib/spring-jdbc-2.1-20061103.020011-52.jar,
 lib/aopalliance.osgi-1.0-20061103.020011-48.jar,
 lib/spring-aop-2.1-20061103.020011-52.jar,
 lib/spring-context-2.1-20061103.020011-52.jar,
 lib/spring-core-2.1-20061103.020011-52.jar,
 lib/spring-web-2.1-20061103.020011-52.jar
Export-Package: org.aopalliance.aop,
 org.springframework.aop;version="2.1.0",
 org.springframework.aop.framework;version="2.1.0",
 org.springframework.aop.target;version="2.1.0",
 org.springframework.beans;version="2.1.0",
 org.springframework.beans.factory;version="2.1.0",
 org.springframework.beans.factory.config;version="2.1.0",
 org.springframework.beans.factory.parsing;version="2.1.0",
 org.springframework.beans.factory.support;version="2.1.0",
 org.springframework.beans.factory.xml;version="2.1.0",
 org.springframework.context;version="2.1.0",
 org.springframework.context.support;version="2.1.0",
 org.springframework.core;version="2.1.0",
 org.springframework.core.io;version="2.1.0",
 org.springframework.core.io.support;version="2.1.0",
 org.springframework.stereotype;version="2.1.0",
 org.springframework.transaction.annotation;version="2.1.0",
 org.springframework.util;version="2.1.0",
 org.springframework.util.xml;version="2.1.0",
 org.springframework.web.context;version="2.1.0",
 org.springframework.web.context.support;version="2.1.0" 

haha.remix.hibernate

META-INF/MANIFEST.MF

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: haha.remix.hibernate
Bundle-SymbolicName: haha.remix.hibernate
Bundle-Version: 1.0.0
Bundle-Localization: plugin
Bundle-ClassPath: .,
 lib/hibernate3.jar,
 lib/hibernate-annotations.jar,
 lib/ejb3-persistence.jar,
 lib/dom4j-1.6.1.jar,
 lib/commons-collections-2.1.1.jar,
 lib/cglib-2.1.3.jar,
 lib/asm.jar,
 lib/jta.jar,
 lib/ehcache-1.2.jar,
 lib/hibernate-entitymanager.jar,
 lib/javassist.jar,
 lib/jboss-archive-browsing.jar,
 lib/antlr-2.7.6.jar
Import-Package: org.apache.commons.logging,
 org.osgi.framework;version="1.4.0",
 org.osgi.service.component;version="1.0.0",
 org.osgi.service.log;version="1.3.0" 
Export-Package: javax.persistence,
 javax.persistence.spi,
 org.hibernate,
 org.hibernate.ejb,
 org.hibernate.proxy
Eclipse-BuddyPolicy: registered

haha.hello.jpa.userdao

META-INF/MANIFEST.MF

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: haha.hello.jpa.userdao
Bundle-SymbolicName: haha.hello.jpa.userdao
Bundle-Version: 1.0.0
Bundle-Activator: haha.hello.jpa.userdao.Activator
Import-Package: javax.persistence,
 org.apache.derby.jdbc,
 org.hibernate.proxy,
 org.osgi.framework;version="1.4.0",
 org.springframework.stereotype;version="2.1.0",
 org.springframework.transaction.annotation;version="2.1.0" 
Eclipse-RegisterBuddy: haha.remix.hibernate
Export-Package: haha.hello.jpa.userdao

MEAT-INF/spring/bean.xml(部份)

<tx:annotation-driven />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="entityManagerFactory" 
  class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
...
</bean>

<bean id="transactionManager" 
   class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="userDao" class="haha.hello.jpa.userdao.UserDaoImpl"/>

<osgi:service id="osgiUserDao" ref="userDao" 
    interface="haha.hello.jpa.userdao.UserDao"/>

haha.hello.jpa.userdao.client

MEAT-INF/spring/bean.xml(部份)

<osgi:reference id="userDao" interface="haha.hello.jpa.userdao.UserDao"/>
<bean name="userDaoClient" 
    class="haha.hello.jpa.userdao.client.UserDaoClient" init-method="init">
      <property name="userDao" ref="userDao"/>
</bean>

觀察

  1. 如果沒有加上 @Transactional 的話,DAO 會遭遇 hibernate session 關閉問題。 http://forum.springframework.org/archive/index.php/t-29054.html

Comments

(leave url/email »)

   Preview comment