1、不使用karaf自己的jpa,也就是不要feature:install jpa
2、使用EclipseLink jpa
3、要使用 http://wiki.eclipse.org/Gemini/JPA提供的模块化支持,在osgi环境中使用EclipseLink作为jpa
4、在karaf的deploy目录中加入以下bundles:
(1)EclipseLink的bundles
javax.persistence_2.0.4.v201112161009.jar
org.eclipse.persistence.antlr_3.2.0.v201206041011.jar
org.eclipse.persistence.asm_3.3.1.v201206041142.jar
org.eclipse.persistence.core_2.4.1.v20121003-ad44345.jar
org.eclipse.persistence.jpa.jpql_2.0.1.v20121003-ad44345.jar
org.eclipse.persistence.jpa_2.4.1.v20121003-ad44345.jar
(2) Gemini/JPA的bundles
org.eclipse.gemini.jpa-1.1.0.RELEASE.jar
在这里下载: Gemini JPA Home
(3)从 http://www.osgi.org/Download/Release4V42下载的
osgi.enterprise.jar
(4)数据库的jdbc驱动器
mysql-connector-java-5.1.24-bin.jar
5、编写持久化单元bundle
在这个bundle中包含三部分:
(1)实体定义
package
dwpaas.console;
import
javax.persistence.Entity;
import
javax.persistence.GeneratedValue;
import
javax.persistence.Id;
@Entity
public
class DwpaasUser {
@Id
@GeneratedValue
private Integer id;
private String username;
private String name;
private String password;
public Integer getId() {
returnid;
}
publicvoid setId(Integer id) {
this.id = id;
}
public String getUsername() {
returnusername;
}
publicvoid setUsername(String username) {
this.username = username;
}
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
public String getPassword() {
returnpassword;
}
publicvoid setPassword(String password) {
this.password = password;
}
}
(2)持久化单元定义:persistence.xml,该文件放在META-INF目录下
xml
version="1.0"encoding="UTF-8"?>
<persistenceversion="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"]]>
<persistence-unitname="dwmc"transaction-type="RESOURCE_LOCAL"]]>
<provider]]>org.eclipse.persistence.jpa.PersistenceProviderprovider]]>
<class]]>dwpaas.console.DwpaasUserclass]]>
<properties]]>
<propertyname="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/dwmc?useUnicode=true&characterEncoding=UTF-8"/>
<propertyname="javax.persistence.jdbc.user"value="dwmc"/>
<propertyname="javax.persistence.jdbc.password"value="dwmc6309"/>
<propertyname="javax.persistence.jdbc.driver"value="com.mysql.jdbc.Driver"/>
<propertyname="eclipselink.logging.level"value="OFF"/>
<propertyname="eclipselink.orm.throw.exceptions"value="true"/>
<propertyname="eclipselink.ddl-generation"value="drop-and-create-tables"/>
<propertyname="eclipselink.ddl-generation.output-mode"value="database"/>
properties]]>
persistence-unit]]>
persistence]]>
(3)在bundle的描述文件中增加:
Meta-Persistence
: META-INF/persistence.xml
其完整的内容如下:
Manifest-Version
: 1.0
Bundle-ManifestVersion
: 2
Bundle-Name
: Pu
Bundle-SymbolicName
: dwpaas.console.pu
Bundle-Version
: 1.0.0.qualifier
Bundle-Vendor
: Dareway
Bundle-RequiredExecutionEnvironment
: JavaSE-1.6
Meta-Persistence
: META-INF/persistence.xml
Import-Package
: com.mysql.jdbc;
version
="5.1.24",
javax.persistence;
version
="2.0.4"
部署该持久化单元bundle之后,就会在osgi环境中自动注册emf服务。
6、
EntityManagerFactory服务
Property Name |
Property Value |
osgi.unit.name |
Name of the persistence unit |
osgi.unit.version |
Bundle version of the persistence bundle |
osgi.unit.provider |
Class name of assigned provider |
osgi.managed.bundles |
Bundle id of the persistence bundle |
利用下述代码从osgi环境中获得emf服务:
public
static EntityManagerFactory lookupEMF(String unitName) {
ServiceReference[] refs = null;
try {
refs = context.getServiceReferences(EntityManagerFactory.class
.getName(), String.format("(%s=%s)",
EntityManagerFactoryBuilder.JPA_UNIT_NAME, unitName));
} catch (InvalidSyntaxException isEx) {
thrownew RuntimeException("Filter error", isEx);
}
return (refs == null) ? null : (EntityManagerFactory)context
.getService(refs[0]);
}
该方法是在bundle的Activator中实现的,其中的context就是osgi的
BundleContext。
使用emf访问数据库:
EntityManagerFactory emf = lookupEMF(
"dwmc"
);
if (emf !=null){
System.out.println("get emf successful.");
EntityManager em = emf.createEntityManager();
}else{
System.out.println("get emf not successful.");
}
7、在karaf的console使用下述命令可以看到:
karaf@root()> bundle:list
START LEVEL 100 , List Threshold: 50
ID State Level Name
[ 91] [ Resolved] [ 80] XBean-Finder-Fragment (3.0.0.M3), Hosts: 92
[ 116] [ Active] [ 80] Java Persistence API 2.0 (2.0.4.v201112161009)
[ 117] [ Active] [ 80] EclipseLink JPA (2.4.1.v20121003-ad44345)
[ 118] [ Active] [ 80] EclipseLink Core (2.4.1.v20121003-ad44345)
[ 119] [ Active] [ 80] EclipseLink ANTLR (3.2.0.v201206041011)
[ 120] [ Active] [ 80] EclipseLink ASM (3.3.1.v201206041142)
[ 121] [ Active] [ 80] osgi.enterprise (4.2.0.201003190513)
[ 122] [ Active] [ 80] EclipseLink Hermes Parser (2.0.1.v20121003-ad44345)
[ 123] [ Active] [ 80] Sun Microsystems' JDBC Driver for MySQL (5.1.24)
[ 124] [ Active] [ 80] Pu (1.0.0.1)
[ 125] [ Active] [ 80] Emf (1.0.0.1)
[ 126] [ Active] [ 80] Gemini JPA (1.1.0.RELEASE)