HHH-11290 - Migrate all documentation snippets that derive the source code from extras instead of actual Unit Tests

Fixed in the OSGI chapter
This commit is contained in:
Vlad Mihalcea 2017-07-05 12:39:03 +03:00
parent ceaeb81e33
commit c87253575d
6 changed files with 90 additions and 46 deletions

View File

@ -71,6 +71,8 @@ dependencies {
testRuntime( libraries.mariadb ) testRuntime( libraries.mariadb )
testRuntime( libraries.mssql ) testRuntime( libraries.mssql )
testCompile "org.osgi:org.osgi.core:4.3.1"
if (db.equalsIgnoreCase("oracle")) { if (db.equalsIgnoreCase("oracle")) {
dependencies { dependencies {
testRuntime( libraries.oracle ) { testRuntime( libraries.oracle ) {

View File

@ -1,6 +1,7 @@
[[osgi]] [[osgi]]
== OSGi == OSGi
:sourcedir: extras :sourcedir: ../../../../../test/java/org/hibernate/userguide/osgi
:extrasdir: extras
=== OSGi Specification and Environment === OSGi Specification and Environment
@ -73,7 +74,7 @@ You can deploy the `DataSource` manually (Karaf has a `deploy` dir), or through
==== ====
[source,xml] [source,xml]
---- ----
include::{sourcedir}/datasource-h2.xml[] include::{extrasdir}/datasource-h2.xml[]
---- ----
==== ====
@ -103,7 +104,7 @@ The container takes the name of your persistence unit, then automatically inject
==== ====
[source,xml] [source,xml]
---- ----
include::{sourcedir}/blueprint.xml[] include::{extrasdir}/blueprint.xml[]
---- ----
==== ====
@ -137,11 +138,12 @@ The service handles the OSGi `ClassLoader`, discovered extension points, scannin
Manually creating an `EntityManagerFactory` is guaranteed to NOT work during runtime! Manually creating an `EntityManagerFactory` is guaranteed to NOT work during runtime!
==== ====
[[osgi-discover-EntityManagerFactory]]
.Discover/Use `EntityManagerFactory` .Discover/Use `EntityManagerFactory`
==== ====
[source,java] [source,java]
---- ----
include::{sourcedir}/UnmanagedJPAHibernateUtil.java[] include::{sourcedir}/jpa/HibernateUtil.java[tag=osgi-discover-EntityManagerFactory, indent=0]
---- ----
==== ====
@ -170,11 +172,12 @@ It is VITAL that your `SessionFactory` be obtained through the service, rather t
Manually creating a `SessionFactory` is guaranteed to NOT work during runtime! Manually creating a `SessionFactory` is guaranteed to NOT work during runtime!
==== ====
[[osgi-discover-SessionFactory]]
.Discover/Use `SessionFactory` .Discover/Use `SessionFactory`
==== ====
[source,java] [source,java]
---- ----
include::{sourcedir}/NativeHibernateUtil.java[] include::{sourcedir}/_native/HibernateUtil.java[tag=osgi-discover-SessionFactory, indent=0]
---- ----
==== ====
@ -207,7 +210,7 @@ Add `OSGI-INF/blueprint/blueprint.xml` to your classpath. Envers' blueprint is a
==== ====
[source,xml] [source,xml]
---- ----
include::{sourcedir}/extension_point_blueprint.xml[] include::{extrasdir}/extension_point_blueprint.xml[]
---- ----
==== ====

View File

@ -1,19 +0,0 @@
public class HibernateUtil {
private SessionFactory sf;
public Session getSession() {
return getSessionFactory().openSession();
}
private SessionFactory getSessionFactory() {
if ( sf == null ) {
Bundle thisBundle = FrameworkUtil.getBundle( HibernateUtil.class );
BundleContext context = thisBundle.getBundleContext();
ServiceReference sr = context.getServiceReference( SessionFactory.class.getName() );
sf = ( SessionFactory ) context.getService( sr );
}
return sf;
}
}

View File

@ -1,21 +0,0 @@
public class HibernateUtil {
private EntityManagerFactory emf;
public EntityManager getEntityManager() {
return getEntityManagerFactory().createEntityManager();
}
private EntityManagerFactory getEntityManagerFactory() {
if ( emf == null ) {
Bundle thisBundle = FrameworkUtil.getBundle( HibernateUtil.class );
BundleContext context = thisBundle.getBundleContext();
ServiceReference serviceReference = context.getServiceReference( PersistenceProvider.class.getName() );
PersistenceProvider persistenceProvider = ( PersistenceProvider ) context.getService( serviceReference );
emf = persistenceProvider.createEntityManagerFactory( "YourPersistenceUnitName", null );
}
return emf;
}
}

View File

@ -0,0 +1,35 @@
package org.hibernate.userguide.osgi._native;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
//tag::osgi-discover-SessionFactory[]
public class HibernateUtil {
private SessionFactory sf;
public Session getSession() {
return getSessionFactory().openSession();
}
private SessionFactory getSessionFactory() {
if ( sf == null ) {
Bundle thisBundle = FrameworkUtil.getBundle(
HibernateUtil.class
);
BundleContext context = thisBundle.getBundleContext();
ServiceReference sr = context.getServiceReference(
SessionFactory.class.getName()
);
sf = ( SessionFactory ) context.getService( sr );
}
return sf;
}
}
//end::osgi-discover-SessionFactory[]

View File

@ -0,0 +1,44 @@
package org.hibernate.userguide.osgi.jpa;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceProvider;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
//tag::osgi-discover-EntityManagerFactory[]
public class HibernateUtil {
private EntityManagerFactory emf;
public EntityManager getEntityManager() {
return getEntityManagerFactory().createEntityManager();
}
private EntityManagerFactory getEntityManagerFactory() {
if ( emf == null ) {
Bundle thisBundle = FrameworkUtil.getBundle(
HibernateUtil.class
);
BundleContext context = thisBundle.getBundleContext();
ServiceReference serviceReference = context.getServiceReference(
PersistenceProvider.class.getName()
);
PersistenceProvider persistenceProvider = ( PersistenceProvider ) context
.getService(
serviceReference
);
emf = persistenceProvider.createEntityManagerFactory(
"YourPersistenceUnitName",
null
);
}
return emf;
}
}
//end::osgi-discover-EntityManagerFactory[]