Kraken JPA
Kraken JPA is based on hibernate 3 and provides declarative transaction feature.
Requirements
- kraken api 1.0.0
- slf4j api 1.5.6
- ipojo 1.2.0
- hibernate 3.3.0.SP1
- hibernate annotations 3.4.0.GA
- hibernate entity manager 3.4.0.GA
- hibernate commons annotations 3.3.0.GA
Install
kraken> bundle.install org.krakenapps kraken-jpa 1.1.0
POM Configuration
<Import-Package> javassist.util.proxy, org.hibernate.proxy, org.hibernate.exception, org.hibernate, org.hibernate.dialect, javax.persistence,* </Import-Package>
- You MUST import above packages for JPA model bundle.
- Otherwise, you will encounter HibernateProxy ClassNotFoundException.
iPOJO Handler Configuration
<component classname="com.example.Component"> <provides /> <requires field="entityManagerService" /> <jpa:Transactional name="entityManagerFactoryName" /> </component>
- jpa xml namespace is org.krakenapps.jpa.handler. You MUST use this namespace because iPOJO interpret it by special handler convention.
- entityManagerService's type is ThreadLocalEntityManagerService. iPOJO will inject ThreadLocalEntityManagerService object for you.
- ThreadLocalEntityManagerService.getEntityManager() will return an EntityManager object in current thread context.
- entityManagerFactoryName is alias for JPA entity manager factory.
Database Configuration
- Kraken JPA recognizes two configuration files in bundle.
- OSGI-INF/kraken-jpa/classes file have JPA entity classe names line by line.
- OSGI-INF/kraken-jpa/config file contains hibernate configurations. (properties file format)
- You can load JPA entity bundle at kraken console.
- "jpa.register [bundle id] [entity manager factory name]" script command will load above configuration files in specified bundle and create entity manager factory with configurations.
- You can also create entity manager factory programmatically.
- Find the service reference for org.krakenapps.jpa.JpaService interface.
- e.g. BundleContext.getServiceReference(JpaService.class.getName());
- Find the service reference for org.krakenapps.jpa.JpaService interface.
Declarative Transaction Support
- @Transactional method annotation provides declarative transaction feature.
- ThreadLocalEntityManagerService will begin transaction at method entry and commit transaction at method exit. If any error occured, transaction will be rollbacked. Finally, the EntityManager object closed.
- TransactionOption (from 1.1.0 release)
- @Transactional has TransactionOption.Required by default.
- TransactionOption.Required: Shares a transaction, if one exists, and creates a new transaction if necessary.
- TransactionOption.RequiresNew: Executes the method with a new transaction, regardless of the state of the current context.
- TransactionOption.Supported: Shares a transaction, if one exists.
Kraken JPA Script
- jpa.list: list all registered entity manager factories.
- jpa.register [bundle id] [entity manager factory name]: load configuration from specified bundle and register entity manager factory.
- jpa.unregister [entity manager factory name]: unregister and close entity manager factory.
Kraken JPA Class Diagram
History
- 1.1.1 release: Fixed handler validation bug at boot time.
- 1.1.0 release: Added TransactionOption to @Transactional. Nested @Transactional declaration is supported.
- 1.0.0 release: Implemented using hibernate.
