HHH-9916 - Update quickstarts and tutorials for 5.0 + convert to asciidoc(tor)
This commit is contained in:
parent
ddac360d7a
commit
56448fafa2
|
@ -1,3 +1,5 @@
|
|||
import org.asciidoctor.gradle.AsciidoctorTask
|
||||
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
|
@ -8,24 +10,23 @@ buildscript {
|
|||
repositories {
|
||||
mavenCentral()
|
||||
mavenLocal()
|
||||
|
||||
maven {
|
||||
name 'jboss-nexus'
|
||||
url "http://repository.jboss.org/nexus/content/groups/public/"
|
||||
}
|
||||
maven {
|
||||
name 'Bintray Asciidoctor repo'
|
||||
url 'http://dl.bintray.com/content/aalmiray/asciidoctor'
|
||||
}
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath "org.jboss.jdocbook:gradle-jdocbook:1.2.2"
|
||||
classpath 'org.asciidoctor:asciidoctor-gradle-plugin:0.7.0'
|
||||
classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.2'
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: "java"
|
||||
apply plugin: "jdocbook"
|
||||
apply plugin: 'org.asciidoctor.convert'
|
||||
|
||||
apply from: "${rootProject.projectDir}/utilities.gradle"
|
||||
|
||||
defaultTasks 'buildDocs'
|
||||
|
@ -34,6 +35,7 @@ configurations {
|
|||
asciidoclet {
|
||||
description = 'Dependencies for Asciidoclet (the javadoc doclet tool for using Asciidoc)'
|
||||
}
|
||||
// asciidoctor
|
||||
}
|
||||
|
||||
if ( JavaVersion.current().isJava8Compatible() ) {
|
||||
|
@ -45,6 +47,7 @@ if ( JavaVersion.current().isJava8Compatible() ) {
|
|||
dependencies {
|
||||
ext.pressgangVersion = '3.0.0'
|
||||
|
||||
// asciidoctor 'org.asciidoctor:asciidoctorj:1.5.2'
|
||||
asciidoclet 'org.asciidoctor:asciidoclet:0.+'
|
||||
|
||||
jdocbookXsl "org.jboss.pressgang:pressgang-xslt-ns:${pressgangVersion}"
|
||||
|
@ -162,10 +165,6 @@ jdocbook {
|
|||
}
|
||||
|
||||
// book-specific config
|
||||
quickstart {
|
||||
masterSourceDocumentName = 'Hibernate_Getting_Started_Guide.xml'
|
||||
}
|
||||
|
||||
devguide {
|
||||
masterSourceDocumentName = 'Hibernate_Development_Guide.xml'
|
||||
}
|
||||
|
@ -188,7 +187,7 @@ stageStyles_devguide.doLast {
|
|||
}
|
||||
}
|
||||
|
||||
[ 'devguide', 'manual', 'quickstart' ].each { bookName ->
|
||||
[ 'devguide', 'manual'].each { bookName ->
|
||||
task "stageLocalStyles_$bookName"(type: Copy) {
|
||||
into project.file( "target/docbook/stage/$bookName" )
|
||||
from project.file( 'src/main/style' )
|
||||
|
@ -197,10 +196,46 @@ stageStyles_devguide.doLast {
|
|||
tasks[ "stageStyles_$bookName" ].dependsOn "stageLocalStyles_$bookName"
|
||||
}
|
||||
|
||||
|
||||
asciidoctor {
|
||||
// we do not want it creating its "default task"
|
||||
enabled = false
|
||||
}
|
||||
|
||||
// Topical Guides ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
task renderTopicalGuides(type: AsciidoctorTask, group: 'Documentation') {
|
||||
description = 'Renders the Topical Guides in HTML format using Asciidoctor.'
|
||||
sourceDir = file( 'src/main/asciidoc/topical' )
|
||||
outputDir = new File("$buildDir/asciidoc/topical/html")
|
||||
backends "html5"
|
||||
separateOutputDirs false
|
||||
options logDocuments: true
|
||||
attributes icons: 'font', experimental: true, 'source-highlighter': 'prettify'
|
||||
}
|
||||
|
||||
buildDocs.dependsOn renderTopicalGuides
|
||||
|
||||
|
||||
|
||||
// Getting Started Guides (quick starts) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
task renderGettingStartedGuides(type: AsciidoctorTask, group: 'Documentation') {
|
||||
description = 'Renders the Getting Started Guides (quick starts) in HTML format using Asciidoctor.'
|
||||
sourceDir = file( 'src/main/asciidoc/quickstart/guides' )
|
||||
outputDir = new File("$buildDir/asciidoc/quickstart/html")
|
||||
backends "html5"
|
||||
separateOutputDirs false
|
||||
options logDocuments: true
|
||||
attributes icons: 'font', experimental: true, 'source-highlighter': 'prettify'
|
||||
}
|
||||
|
||||
buildDocs.dependsOn renderGettingStartedGuides
|
||||
|
||||
task buildTutorialZip(type: Zip) {
|
||||
destinationDir = file( "target/work/tutorials" )
|
||||
archiveName = 'hibernate-tutorials.zip'
|
||||
from 'src/main/docbook/quickstart/tutorials'
|
||||
from 'src/main/asciidoc/quickstart/tutorials'
|
||||
destinationDir = tasks.renderGettingStartedGuides.outputDir
|
||||
archiveName = 'hibernate-tutorials.zip'
|
||||
expand(
|
||||
version: project.version,
|
||||
slf4j: "1.7.5",
|
||||
|
@ -209,45 +244,5 @@ task buildTutorialZip(type: Zip) {
|
|||
)
|
||||
}
|
||||
|
||||
buildDocs.dependsOn buildTutorialZip
|
||||
renderGettingStartedGuides.dependsOn buildTutorialZip
|
||||
|
||||
buildDocs.doLast {
|
||||
for ( File languageDir : dirList( "target/docbook/publish/quickstart" ) ) {
|
||||
for ( File formatDir : dirList( languageDir ) ) {
|
||||
final File copyDir = new File( formatDir, "files" );
|
||||
copyDir.mkdirs();
|
||||
ant.copy( file: buildTutorialZip.archivePath.getAbsolutePath(), todir: copyDir.getAbsolutePath() )
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
File[] dirList(String dirName) {
|
||||
return dirList( (File) file(dirName) );
|
||||
}
|
||||
|
||||
File[] dirList(File dir) {
|
||||
return dir.listFiles({file -> file.isDirectory() } as FileFilter).sort();
|
||||
}
|
||||
|
||||
|
||||
// asciidoctor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
task asciidoctor(type: org.asciidoctor.gradle.AsciidoctorTask, group: 'Documentation') {
|
||||
description = 'Generates the Asciidoc(tor) topical guides in HTML format.'
|
||||
backend = 'html5'
|
||||
sourceDir = file( 'src/main/asciidoc/topical' )
|
||||
outputDir = new File("$buildDir/asciidoc/topical/html")
|
||||
}
|
||||
|
||||
tasks.withType(org.asciidoctor.gradle.AsciidoctorTask) { docTask ->
|
||||
options = [
|
||||
logDocuments: true,
|
||||
attributes: [
|
||||
icons: 'font',
|
||||
'source-highlighter': 'prettify',
|
||||
experimental: true
|
||||
]
|
||||
]
|
||||
}
|
||||
|
||||
buildDocs.dependsOn asciidoctor
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
= Hibernate Getting Started Guide
|
||||
:toc:
|
||||
:docinfo:
|
||||
|
||||
include::preface.adoc[]
|
||||
|
||||
:numbered:
|
||||
include::obtaining.adoc[]
|
||||
|
||||
include::tutorial_native.adoc[]
|
||||
|
||||
include::tutorial_annotations.adoc[]
|
||||
|
||||
include::tutorial_jpa.adoc[]
|
||||
|
||||
include::tutorial_envers.adoc[]
|
||||
|
||||
include::tutorial_osgi.adoc[]
|
|
@ -0,0 +1,55 @@
|
|||
[[obtaining]]
|
||||
== Obtaining Hibernate
|
||||
|
||||
=== The Hibernate Modules/Artifacts
|
||||
|
||||
Hibernate's functionality is split into a number of modules/artifacts meant to isolate dependencies (modularity).
|
||||
|
||||
hibernate-core:: The main (core) Hibernate module. Defines its ORM features and APIs as well as the various integration SPIs.
|
||||
hibernate-entitymanager:: Defines Hibernate's support for link:$$http://jcp.org/en/jsr/detail?id=317$$[JPA]
|
||||
hibernate-java8:: Support for using Java8 specific data-types such as any of the JSR 310 Date/Time types in domain model.
|
||||
hibernate-envers:: Hibernate's historical entity versioning feature
|
||||
hibernate-spatial:: Hibernate's Spatial/GIS data-type support
|
||||
hibernate-osgi:: Hibernate support for running in OSGi containers.
|
||||
hibernate-c3p0:: Integrates the link:$$http://www.mchange.com/projects/c3p0/$$[C3P0] connection pooling library into Hibernate
|
||||
hibernate-hikaricp:: Integrates the link:$$http://brettwooldridge.github.io/HikariCP/$$[HikariCP] connection pooling library into Hibernate
|
||||
hibernate-proxool:: Integrates the link:$$http://proxool.sourceforge.net/$$[Proxool] connection pooling library into Hibernate
|
||||
hibernate-ehcache:: Integrates the link:$$http://ehcache.org/$$[Ehcache] caching library into Hibernate as a second-level cache provider.
|
||||
hibernate-infinispan:: Integrates the link:$$http://infinispan.org/$$[Infinispan] caching library into Hibernate as a second-level cache provider.
|
||||
|
||||
|
||||
=== Release Bundle Downloads
|
||||
|
||||
The Hibernate team provides release bundles hosted on the SourceForge File Release System, in both
|
||||
`TGZ` and `ZIP` formats. Each release bundle contains `JAR` files, documentation, source code, and other goodness.
|
||||
|
||||
You can download releases of Hibernate, in your chosen format, from the list at
|
||||
https://sourceforge.net/projects/hibernate/files/hibernate-orm/. The release bundle is structured as follows:
|
||||
|
||||
* The `lib/required/` directory contains the `hibernate-core` jar and all of its dependencies. All of these jars are
|
||||
required to be available on your classpath no matter which features of Hibernate are being used.
|
||||
* The `/lib/jpa/` directory contains the `hibernate-entitymanager` jar as well as all of its dependencies (beyond
|
||||
those in `lib/required/`)
|
||||
* The `lib/java8/` directory contains the `hibernate-java8` jar and all of its dependencies (beyond those in `lib/required/`)
|
||||
* The `lib/envers` directory contains the `hibernate-envers` jar and all of its dependencies (beyond those in
|
||||
`lib/required/` and `lib/jpa/`).
|
||||
* The `lib/spatial/` directory contains the `hibernate-spatial` jar and all of its dependencies (beyond those in `lib/required/`)
|
||||
* The `lib/osgi/` directory contains the `hibernate-osgi` jar and all of its dependencies (beyond those in `lib/required/` and `lib/jpa/`)
|
||||
* The `lib/optional/` directory contains the jars needed for the various connection pooling and second-level cache integrations
|
||||
provided by Hibernate, along with their dependencies.
|
||||
|
||||
|
||||
=== Maven Repository Artifacts
|
||||
|
||||
The authoritative repository for Hibernate artifacts is the JBoss Maven repository. The Hibernate artifacts are
|
||||
synced to Maven Central as part of an automated job (some small delay may occur).
|
||||
|
||||
The team responsible for the JBoss Maven repository maintains a number of Wiki pages that contain important information:
|
||||
|
||||
* http://community.jboss.org/docs/DOC-14900 - General information about the repository.
|
||||
* http://community.jboss.org/docs/DOC-15170 - Information about setting up the JBoss repositories in order to do
|
||||
development work on JBoss projects themselves.
|
||||
* http://community.jboss.org/docs/DOC-15169 - Information about setting up access to the repository to use JBoss
|
||||
projects as part of your own software.
|
||||
|
||||
The Hibernate ORM artifacts are published under the `org.hibernate` groupId.
|
|
@ -0,0 +1,35 @@
|
|||
[[preface]]
|
||||
|
||||
[preface]
|
||||
== Preface
|
||||
|
||||
Working with both Object-Oriented software and Relational Databases can be cumbersome and time consuming.
|
||||
Development costs are significantly higher due to a paradigm mismatch between how data is represented in objects
|
||||
versus relational databases. Hibernate is an Object/Relational Mapping (ORM) solution for Java environments. The
|
||||
term Object/Relational Mapping refers to the technique of mapping data between an object model representation to
|
||||
a relational data model representation. See http://en.wikipedia.org/wiki/Object-relational_mapping for a good
|
||||
high-level discussion. Also, Martin Fowler's link:$$http://martinfowler.com/bliki/OrmHate.html$$[OrmHate] article
|
||||
takes a look at many of the mismatch problems.
|
||||
|
||||
Although having a strong background in SQL is not required to use Hibernate, having a basic understanding of the
|
||||
concepts can help you understand Hibernate more quickly and fully. An understanding of data modeling principles
|
||||
is especially important. Both http://www.agiledata.org/essays/dataModeling101.html and
|
||||
http://en.wikipedia.org/wiki/Data_modeling are good starting points for understanding these data modeling principles.
|
||||
|
||||
Hibernate takes care of the mapping from Java classes to database tables, and from Java data types to SQL data
|
||||
types. In addition, it provides data query and retrieval facilities. It can significantly reduce development
|
||||
time otherwise spent with manual data handling in SQL and JDBC. Hibernate’s design goal is to relieve the
|
||||
developer from 95% of common data persistence-related programming tasks by eliminating the need for manual,
|
||||
hand-crafted data processing using SQL and JDBC. However, unlike many other persistence solutions, Hibernate
|
||||
does not hide the power of SQL from you and guarantees that your investment in relational technology and
|
||||
knowledge is as valid as always.
|
||||
|
||||
Hibernate may not be the best solution for data-centric applications that only use stored-procedures to
|
||||
implement the business logic in the database, it is most useful with object-oriented domain models and business
|
||||
logic in the Java-based middle-tier. However, Hibernate can certainly help you to remove or encapsulate
|
||||
vendor-specific SQL code and streamlines the common task of translating result sets from a tabular
|
||||
representation to a graph of objects.
|
||||
|
||||
See http://hibernate.org/orm/contribute/ for information on getting involved.
|
||||
|
||||
IMPORTANT: The projects and code for the tutorials referenced in this guide are available as link:hibernate-tutorials.zip[]
|
|
@ -0,0 +1,103 @@
|
|||
[[tutorial_annotations]]
|
||||
== Tutorial Using Native Hibernate APIs and Annotation Mappings
|
||||
|
||||
NOTE: This tutorial is located within the download bundle under `annotations/`.
|
||||
|
||||
.Objectives
|
||||
- [*] Bootstrap a Hibernate `SessionFactory`
|
||||
- [*] Use annotations to provide mapping information
|
||||
- [*] Use the Hibernate native APIs
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-annotations-config]]
|
||||
=== The Hibernate configuration file
|
||||
|
||||
The contents are identical to <<hibernate-gsg-tutorial-basic-config>> with one important difference...
|
||||
The `<mapping/>` element at the very end naming the annotated entity class using the `class` attribute.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-annotations-entity]]
|
||||
=== The annotated entity Java class
|
||||
|
||||
The entity class in this tutorial is `org.hibernate.tutorial.annotations.Event` which follows JavaBean conventions.
|
||||
In fact the class itself is identical to the one in <<hibernate-gsg-tutorial-basic-entity>>, except that annotations
|
||||
are used to provide the metadata, rather than a separate mapping file.
|
||||
|
||||
[[hibernate-gsg-tutorial-annotations-entity-entity]]
|
||||
.Identifying the class as an entity
|
||||
====
|
||||
[source, JAVA]
|
||||
----
|
||||
@Entity
|
||||
@Table( name = "EVENTS" )
|
||||
public class Event {
|
||||
...
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
The `@javax.persistence.Entity` annotation is used to mark a class as an entity. It functions the same as the
|
||||
`<class/>` mapping element discussed in <<hibernate-gsg-tutorial-basic-mapping>>. Additionally the
|
||||
`@javax.persistence.Table` annotation explicitly specifies the table name. Without this specification, the default
|
||||
table name would be _EVENT_.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-annotations-entity-id]]
|
||||
.Identifying the identifier property
|
||||
====
|
||||
[source, JAVA]
|
||||
----
|
||||
@Id
|
||||
@GeneratedValue(generator="increment")
|
||||
@GenericGenerator(name="increment", strategy = "increment")
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
`@javax.persistence.Id` marks the property which defines the entity's identifier.
|
||||
|
||||
`@javax.persistence.GeneratedValue` and `@org.hibernate.annotations.GenericGenerator` work in tandem
|
||||
to indicate that Hibernate should use Hibernate's `increment` generation strategy for this entity's identifier values.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-annotations-entity-properties]]
|
||||
.Identifying basic properties
|
||||
====
|
||||
[source, JAVA]
|
||||
----
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name = "EVENT_DATE")
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
As in <<hibernate-gsg-tutorial-basic-mapping, the `date` property needs special handling to account for its special
|
||||
naming and its SQL type.
|
||||
|
||||
Attributes of an entity are considered persistent by default when mapping with annotations, which is why we don't see
|
||||
any mapping information associated with `title`.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-annotations-test]]
|
||||
=== Example code
|
||||
|
||||
`org.hibernate.tutorial.annotations.AnnotationsIllustrationTest` is essentially the same as
|
||||
`org.hibernate.tutorial.hbm.NativeApiIllustrationTest` discussed in <<hibernate-gsg-tutorial-basic-test>>.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-annotations-further]]
|
||||
=== Take it further!
|
||||
|
||||
.Practice Exercises
|
||||
- [ ] Add an association to the `Event` entity to model a message thread. Use the _Developer Guide_
|
||||
as a guide.
|
||||
- [ ] Add a callback to receive notifications when an `Event` is created, updated or deleted. Try the same with
|
||||
an event listener. Use the _Developer Guide_ as a guide.
|
|
@ -0,0 +1,64 @@
|
|||
[[tutorial_envers]]
|
||||
== Tutorial Using Envers
|
||||
|
||||
NOTE: This tutorial is located within the download bundle under `envers/`.
|
||||
|
||||
.Objectives
|
||||
- [*] Annotate an entity as historical
|
||||
- [*] Configure Envers
|
||||
- [*] Use the Envers APIs to view and analyze historical data
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-envers-config]]
|
||||
=== persistence.xml
|
||||
|
||||
This file was discussed in the JPA tutorial in <<hibernate-gsg-tutorial-jpa-config>>, and is essentially the same here.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-envers-entity]]
|
||||
=== The annotated entity Java class
|
||||
|
||||
Again, the entity is largely the same as in <<hibernate-gsg-tutorial-jpa-entity>>. The major difference is the
|
||||
addition of the `@org.hibernate.envers.Audited` annotation, which tells Envers to automatically track changes to this
|
||||
entity.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-envers-test]]
|
||||
=== Example code
|
||||
|
||||
The code saves some entities, makes a change to one of the entities and then uses the Envers API to pull back the
|
||||
initial revision as well as the updated revision. A revision refers to a historical snapshot of an entity.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-envers-test-api]]
|
||||
.Using the org.hibernate.envers.AuditReader
|
||||
====
|
||||
[source, JAVA]
|
||||
----
|
||||
public void testBasicUsage() {
|
||||
...
|
||||
AuditReader reader = AuditReaderFactory.get( entityManager );
|
||||
Event firstRevision = reader.find( Event.class, 2L, 1 );
|
||||
...
|
||||
Event secondRevision = reader.find( Event.class, 2L, 2 );
|
||||
...
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
We see that an `org.hibernate.envers.AuditReader` is obtained from the `org.hibernate.envers.AuditReaderFactory`
|
||||
which wraps the `javax.persistence.EntityManager`.
|
||||
|
||||
Next, the `find` method retrieves specific revisions of the entity. The first call says to find revision number
|
||||
1 of Event with id 2. The second call says to find revision number 2 of Event with id 2.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-annotations-further]]
|
||||
=== Take it further!
|
||||
|
||||
.Practice Exercises
|
||||
- [*] Provide a custom revision entity to additionally capture who made the changes.
|
||||
- [*] Write a query to retrieve only historical data which meets some criteria. Use the _User Guide_ to see how
|
||||
Envers queries are constructed.
|
||||
- [*] Experiment with auditing entities which have various forms of relationships (many-to-one, many-to-many, etc). Try
|
||||
retrieving historical versions (revisions) of such entities and navigating the object tree.
|
|
@ -0,0 +1,115 @@
|
|||
[[tutorial_jpa]]
|
||||
== Tutorial Using the Java Persistence API (JPA)
|
||||
|
||||
NOTE: This tutorial is located within the download bundle under `entitymanager/`.
|
||||
|
||||
.Objectives
|
||||
- [*] Bootstrap a JPA `EntityManagerFactory`
|
||||
- [*] Use annotations to provide mapping information
|
||||
- [*] Use JPA API calls
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-jpa-config]]
|
||||
=== persistence.xml
|
||||
|
||||
The previous tutorials used the Hibernate-specific `hibernate.cfg.xml` configuration file. JPA, however, defines
|
||||
a different bootstrap process that uses its own configuration file named `persistence.xml`. This bootstrapping process
|
||||
is defined by the JPA specification. In Java(TM) SE environments the persistence provider (Hibernate in this case)
|
||||
is required to locate all JPA configuration files by classpath lookup of the `META-INF/persistence.xml` resource name.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-jpa-config-pu]]
|
||||
.persistence.xml
|
||||
====
|
||||
[source, XML]
|
||||
----
|
||||
<persistence 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"
|
||||
version="2.0">
|
||||
<persistence-unit name="org.hibernate.tutorial.jpa">
|
||||
...
|
||||
</persistence-unit>
|
||||
</persistence>
|
||||
----
|
||||
====
|
||||
|
||||
`persistence.xml` files should provide a unique name for each "persistence unit". Applications use this name to
|
||||
reference the configuration when obtaining an `javax.persistence.EntityManagerFactory reference.
|
||||
|
||||
The settings defined in the `<properties/>` element are discussed in <<hibernate-gsg-tutorial-basic-config>>.
|
||||
Here the `javax.persistence`-prefixed varieties are used when possible. Notice that the remaining
|
||||
Hibernate-specific configuration setting names are now prefixed with `hibernate.`.
|
||||
|
||||
Additionally, the `<class/>` element functions the same as we saw in <<hibernate-gsg-tutorial-annotations-config>>.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-jpa-entity]]
|
||||
=== The annotated entity Java class
|
||||
|
||||
The entity is exactly the same as in <<hibernate-gsg-tutorial-annotations-entity>>.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-jpa-test]]
|
||||
=== Example code
|
||||
|
||||
The previous tutorials used the Hibernate native APIs. This tutorial uses the JPA APIs.
|
||||
|
||||
[[hibernate-gsg-tutorial-jpa-test-setUp]]
|
||||
.Obtaining the javax.persistence.EntityManagerFactory
|
||||
====
|
||||
[source, JAVA]
|
||||
----
|
||||
protected void setUp() throws Exception {
|
||||
entityManagerFactory = Persistence.createEntityManagerFactory( "org.hibernate.tutorial.jpa" );
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
Notice again that the persistence unit name is `org.hibernate.tutorial.jpa`, which matches <<hibernate-gsg-tutorial-jpa-config-pu>>.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-jpa-test-saving]]
|
||||
.Saving (persisting) entities
|
||||
====
|
||||
[source, JAVA]
|
||||
----
|
||||
EntityManager entityManager = entityManagerFactory.createEntityManager();
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.persist( new Event( "Our very first event!", new Date() ) );
|
||||
entityManager.persist( new Event( "A follow up event", new Date() ) );
|
||||
entityManager.getTransaction().commit();
|
||||
entityManager.close();
|
||||
----
|
||||
====
|
||||
|
||||
The code is similar to <<hibernate-gsg-tutorial-basic-test-saving>>. The `javax.persistence.EntityManager` interface
|
||||
is used instead of the `org.hibernate.Session` interface. JPA calls this operation "persist" instead of "save".
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-jpa-test-list]]
|
||||
.Obtaining a list of entities
|
||||
====
|
||||
[source, JAVA]
|
||||
----
|
||||
entityManager = entityManagerFactory.createEntityManager();
|
||||
entityManager.getTransaction().begin();
|
||||
List<Event> result = entityManager.createQuery( "from Event", Event.class ).getResultList();
|
||||
for ( Event event : result ) {
|
||||
System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() );
|
||||
}
|
||||
entityManager.getTransaction().commit();
|
||||
entityManager.close();
|
||||
----
|
||||
====
|
||||
|
||||
Again, the code is pretty similar to what we saw in <<hibernate-gsg-tutorial-basic-test-list>>.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-annotations-further]]
|
||||
=== Take it further!
|
||||
|
||||
.Practice Exercises
|
||||
- [ ] Develop an EJB Session bean to investigate implications of using a container-managed
|
||||
persistence context. Try both stateless and stateful use-cases.
|
||||
- [ ] Use listeners with CDI-based injection to develop a JMS-based event message hub
|
|
@ -0,0 +1,231 @@
|
|||
[[tutorial-native]]
|
||||
== Tutorial Using Native Hibernate APIs and hbm.xml Mapping
|
||||
|
||||
NOTE: This tutorial is located within the download bundle under `basic/`.
|
||||
|
||||
.Objectives
|
||||
- [*] Bootstrap a Hibernate `SessionFactory`
|
||||
- [*] Use Hibernate mapping (`hbm.xml`) files to provide mapping information
|
||||
- [*] Use the Hibernate native APIs
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-basic-config]]
|
||||
=== The Hibernate configuration file
|
||||
|
||||
For this tutorial, the `hibernate.cfg.xml` file defines the Hibernate configuration information.
|
||||
|
||||
The `connection.driver_class`, `connection.url`, `connection.username` and `connection.password` `<property/>` elements
|
||||
define JDBC connection information. These tutorials utilize the H2 in-memory database, so the values of these properties
|
||||
are all specific to running H2 in its in-memory mode. `connection.pool_size` is used to configure the number of
|
||||
connections in Hibernate's built-in connection pool.
|
||||
|
||||
IMPORTANT: The built-in Hibernate connection pool is in no way intended for production use. It lacks several
|
||||
features found on production-ready connection pools.
|
||||
|
||||
The `dialect` property specifies the particular SQL variant with which Hibernate will converse.
|
||||
|
||||
TIP: In most cases, Hibernate is able to properly determine which dialect to use. This is particularly useful
|
||||
if your application targets multiple databases.
|
||||
|
||||
The `hbm2ddl.auto` property enables automatic generation of database schemas directly into the database.
|
||||
|
||||
Finally, add the mapping file(s) for persistent classes to the configuration. The `resource` attribute of the
|
||||
`<mapping/>` element causes Hibernate to attempt to locate that mapping as a classpath resource using a
|
||||
`java.lang.ClassLoader` lookup.
|
||||
|
||||
There are many ways and options to bootstrap a Hibernate `SessionFactory`. For additional details, see
|
||||
the _Native Bootstrapping_ topical guide.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-basic-entity]]
|
||||
=== The entity Java class
|
||||
|
||||
The entity class for this tutorial is `org.hibernate.tutorial.hbm.Event`
|
||||
|
||||
.Notes About the Entity
|
||||
* This class uses standard JavaBean naming conventions for property getter and setter methods, as well as
|
||||
private visibility for the fields. Although this is the recommended design, it is not required.
|
||||
* The no-argument constructor, which is also a JavaBean convention, is a requirement for all persistent classes.
|
||||
Hibernate needs to create objects for you, using Java Reflection. The constructor can be private. However, package
|
||||
or public visibility is required for runtime proxy generation and efficient data retrieval without bytecode
|
||||
instrumentation.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-basic-mapping]]
|
||||
=== The mapping file
|
||||
|
||||
The mapping file for this tutorial is the classpath resource `org/hibernate/tutorial/hbm/Event.hbm.xml` (as discussed above).
|
||||
|
||||
Hibernate uses the mapping metadata to determine how to load and store objects of the persistent class. The Hibernate
|
||||
mapping file is one choice for providing Hibernate with this metadata.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-basic-mapping-class]]
|
||||
.The class mapping element
|
||||
====
|
||||
[source, XML]
|
||||
----
|
||||
<class name="Event" table="EVENTS">
|
||||
...
|
||||
</class>
|
||||
----
|
||||
====
|
||||
|
||||
.Functions of the <varname>class</varname> mapping element
|
||||
* The `name` attribute (combined here with the `package` attribute from the containing `<hibernate-mapping/>` element)
|
||||
names the FQN of the class to be defined as an entity.
|
||||
* The `table` attribute names the database table which contains the data for this entity.
|
||||
|
||||
Instances of the `Event` class are now mapped to rows in the `EVENTS` database table.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-basic-mapping-id]]
|
||||
.The id mapping element
|
||||
====
|
||||
[source, XML]
|
||||
----
|
||||
<id name="id" column="EVENT_ID">
|
||||
...
|
||||
</id>
|
||||
----
|
||||
====
|
||||
|
||||
Hibernate uses the property named by the `<id/>` element to uniquely identify rows in the table.
|
||||
|
||||
IMPORTANT: It is not required for the id element to map to the table's actual primary key column(s), but it is
|
||||
the normal convention. Tables mapped in Hibernate do not even need to define primary keys. However, it is strongly
|
||||
recommend that all schemas define proper referential integrity. Therefore id and primary key are used interchangeably
|
||||
throughout Hibernate documentation.
|
||||
|
||||
The `<id/>` element here names the EVENT_ID column as the primary key of the EVENTS table. It also identifies the
|
||||
`id` property of the `Event` class as the property containing the identifier value.
|
||||
|
||||
The `generator element informs Hibernate about which strategy is used to generated primary key values for this entity.
|
||||
This example uses a simple incrementing count.
|
||||
|
||||
[[hibernate-gsg-tutorial-basic-mapping-property]]
|
||||
.The property mapping element
|
||||
====
|
||||
[source, XML]
|
||||
----
|
||||
<property name="date" type="timestamp" column="EVENT_DATE"/>
|
||||
<property name="title"/>
|
||||
----
|
||||
====
|
||||
|
||||
The two `<property/>` elements declare the remaining two persistent properties of the `Event` class: `date` and `title`.
|
||||
The `date` property mapping includes the `column` attribute, but the `title` does not.
|
||||
In the absence of a `column` attribute, Hibernate uses the property name as the column name.
|
||||
This is appropriate for `title`, but since `date` is a reserved keyword in most databases, you need to specify a
|
||||
non-reserved word for the column name.
|
||||
|
||||
The `title mapping also lacks a type attribute. The types declared and used in the mapping files are neither Java data
|
||||
types nor SQL database types. Instead, they are *Hibernate mapping types*, which are converters which translate between
|
||||
Java and SQL data types. Hibernate attempts to determine the correct conversion and mapping type autonomously if the
|
||||
type attribute is not specified in the mapping, by using Java reflection to determine the Java type of the declared
|
||||
property and using a default mapping type for that Java type.
|
||||
|
||||
In some cases this automatic detection might not chose the default you expect or need, as seen with the
|
||||
`date` property. Hibernate cannot know if the property, which is of type `java.util.Date`, should map to a SQL
|
||||
_DATE_, _TIME_, or _TIMESTAMP_ datatype. Full date and time information is preserved by mapping the property to
|
||||
the _timestamp_ converter, which identifies the converter class `org.hibernate.type.TimestampType`.
|
||||
|
||||
TIP: Hibernate determines the mapping type using reflection when the mapping files are processed. This process adds
|
||||
overhead in terms of time and resources. If startup performance is important, consider explicitly defining the type
|
||||
to use.
|
||||
|
||||
[[hibernate-gsg-tutorial-basic-test]]
|
||||
=== Example code
|
||||
|
||||
The `org.hibernate.tutorial.hbm.NativeApiIllustrationTest` class illustrates using the Hibernate native API.
|
||||
|
||||
NOTE: The examples in these tutorials are presented as JUnit tests, for ease of use. One benefit of this
|
||||
approach is that `setUp` and `tearDown` roughly illustrate how a `org.hibernate.SessionFactory` is created at the
|
||||
start-up of an application and closed at the end of the application lifecycle.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-basic-test-setUp]]
|
||||
.Obtaining the org.hibernate.SessionFactory
|
||||
====
|
||||
[source, JAVA]
|
||||
----
|
||||
protected void setUp() throws Exception {
|
||||
// A SessionFactory is set up once for an application!
|
||||
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
|
||||
.configure() // configures settings from hibernate.cfg.xml
|
||||
.build();
|
||||
try {
|
||||
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
|
||||
}
|
||||
catch (Exception e) {
|
||||
// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
|
||||
// so destroy it manually.
|
||||
StandardServiceRegistryBuilder.destroy( registry );
|
||||
}
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
The `setUp` method first builds a `org.hibernate.boot.registry.StandardServiceRegistry` instance which incorporates
|
||||
configuration information into a working set of Services for use by the SessionFactory. In this tutorial
|
||||
we defined all configuration information in `hibernate.cfg.xml` so there is not much interesting to see here.
|
||||
|
||||
Using the `StandardServiceRegistry` we create the `org.hibernate.boot.MetadataSources` which is the start point for
|
||||
telling Hibernate about your domain model. Again, since we defined that in `hibernate.cfg.xml` so there is not much
|
||||
interesting to see here.
|
||||
|
||||
`org.hibernate.boot.Metadata` represents the complete, partially validated view of the application domain model which the
|
||||
`SessionFactory` will be based on.
|
||||
|
||||
The final step in the bootstrap process is to build the `SessionFactory`. The `SessionFactory` is a
|
||||
thread-safe object that is instantiated once to serve the entire application.
|
||||
|
||||
The `SessionFactory` acts as a factory for `org.hibernate.Session` instances, which should be thought of
|
||||
as a corollary to a "unit of work".
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-basic-test-saving]]
|
||||
.Saving entities
|
||||
====
|
||||
[source, JAVA]
|
||||
----
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
session.save( new Event( "Our very first event!", new Date() ) );
|
||||
session.save( new Event( "A follow up event", new Date() ) );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
----
|
||||
====
|
||||
|
||||
`testBasicUsage()` first creates some new `Event` objects and hands them over to Hibernate for management, using the
|
||||
`save()` method. Hibernate now takes responsibility to perform an _INSERT_ on the database for each `Event`.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-basic-test-list]]
|
||||
.Obtaining a list of entities
|
||||
====
|
||||
[source, JAVA]
|
||||
----
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
List result = session.createQuery( "from Event" ).list();
|
||||
for ( Event event : (List<Event>) result ) {
|
||||
System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() );
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
----
|
||||
====
|
||||
|
||||
Here we see an example of the Hibernate Query Language (HQL) to load all existing `Event` objects from the database
|
||||
by generating the appropriate _SELECT_ SQL, sending it to the database and populating `Event` objects with the result
|
||||
set data.
|
||||
|
||||
|
||||
[[hibernate-gsg-tutorial-annotations-further]]
|
||||
=== Take it further!
|
||||
|
||||
.Practice Exercises
|
||||
- [ ] Reconfigure the examples to connect to your own persistent relational database.
|
||||
- [ ] Add an association to the `Event` entity to model a message thread.
|
|
@ -0,0 +1,17 @@
|
|||
[[tutorial_osgi]]
|
||||
== OSGi Tutorial
|
||||
|
||||
[IMPORTANT]
|
||||
====
|
||||
This guide and the tutorials were very much out-of-date as of 5.0, and were therefore removed until they can be updated.
|
||||
For details see link:$$https://hibernate.atlassian.net/browse/HHH-9919$$[HHH-9919].
|
||||
|
||||
In the meantime, we suggest looking at the Pax Exam and Karaf based
|
||||
link:$$https://github.com/hibernate/hibernate-orm/blob/master/hibernate-osgi/src/test/java/org/hibernate/osgi/test/OsgiIntegrationTest.java$$[hibernate-osgi tests]
|
||||
for examples of using Hibernate in Karaf specifically. Usage in other OSGi containers should be similar.
|
||||
|
||||
The Hibernate-produced Karaf features file (feature repository) is published to the JBoss Nexus repository, along
|
||||
with our other artifacts, using the `org.hibernate` groupId and `hibernate-osgi` with the `karaf` classifier. The
|
||||
features it defines are versioned using the same version as the release it comes from. The Karaf feature file is
|
||||
tested against Karaf version 3.0.3.
|
||||
====
|
|
@ -26,11 +26,13 @@ package org.hibernate.tutorial.annotations;
|
|||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Illustrates the use of Hibernate native APIs. The code here is unchanged from the {@code basic} example, the
|
||||
|
@ -43,10 +45,18 @@ public class AnnotationsIllustrationTest extends TestCase {
|
|||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
// A SessionFactory is set up once for an application
|
||||
sessionFactory = new Configuration()
|
||||
.configure() // configures settings from hibernate.cfg.xml
|
||||
.buildSessionFactory();
|
||||
// A SessionFactory is set up once for an application!
|
||||
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
|
||||
.configure() // configures settings from hibernate.cfg.xml
|
||||
.build();
|
||||
try {
|
||||
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
|
||||
}
|
||||
catch (Exception e) {
|
||||
// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
|
||||
// so destroy it manually.
|
||||
StandardServiceRegistryBuilder.destroy( registry );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
|
@ -26,11 +26,13 @@ package org.hibernate.tutorial.hbm;
|
|||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Illustrates use of Hibernate native APIs.
|
||||
|
@ -42,10 +44,18 @@ public class NativeApiIllustrationTest extends TestCase {
|
|||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
// A SessionFactory is set up once for an application
|
||||
sessionFactory = new Configuration()
|
||||
.configure() // configures settings from hibernate.cfg.xml
|
||||
.buildSessionFactory();
|
||||
// A SessionFactory is set up once for an application!
|
||||
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
|
||||
.configure() // configures settings from hibernate.cfg.xml
|
||||
.build();
|
||||
try {
|
||||
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
|
||||
}
|
||||
catch (Exception e) {
|
||||
// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
|
||||
// so destroy it manually.
|
||||
StandardServiceRegistryBuilder.destroy( registry );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -55,6 +65,7 @@ public class NativeApiIllustrationTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testBasicUsage() {
|
||||
// create a couple of events...
|
||||
Session session = sessionFactory.openSession();
|
|
@ -8,7 +8,7 @@ specific to the custom implementation. The second way is to integrate with the
|
|||
This guide will cover the second approach.
|
||||
|
||||
|
||||
=== Implementor contracts
|
||||
== Implementor contracts
|
||||
|
||||
A Hibernate naming convention is that SPI contracts extending API contracts are named with Implementor appended
|
||||
to the API contract name. For `SessionFactory` and `Session` that is `SessionFactoryImplementor` and
|
||||
|
@ -18,7 +18,7 @@ implement `org.hibernate.engine.spi.SessionFactoryImplementor` and `org.hibernat
|
|||
See all pertinent JavaDocs for discussions of implementation details.
|
||||
|
||||
|
||||
=== Integration hooks
|
||||
== Integration hooks
|
||||
|
||||
`org.hibernate.boot.SessionFactoryBuilder` is part of the Hibernate native bootstrap API where we want to configure
|
||||
the `SessionFactory` to be built. Third parties can hook into this process by supplying a
|
||||
|
|
|
@ -1,67 +0,0 @@
|
|||
<?xml version='1.0' encoding='UTF-8' ?>
|
||||
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<authorgroup xmlns="http://docbook.org/ns/docbook">
|
||||
<author>
|
||||
<personname>
|
||||
<firstname>Gavin</firstname>
|
||||
<surname>King</surname>
|
||||
</personname>
|
||||
</author>
|
||||
<author>
|
||||
<personname>
|
||||
<firstname>Christian</firstname>
|
||||
<surname>Bauer</surname>
|
||||
</personname>
|
||||
</author>
|
||||
<author>
|
||||
<personname>
|
||||
<firstname>Steve</firstname>
|
||||
<surname>Ebersole</surname>
|
||||
</personname>
|
||||
</author>
|
||||
<author>
|
||||
<personname>
|
||||
<firstname>Emmanuel</firstname>
|
||||
<surname>Bernard</surname>
|
||||
</personname>
|
||||
</author>
|
||||
<author>
|
||||
<personname>
|
||||
<firstname>Adam</firstname>
|
||||
<surname>Warski</surname>
|
||||
</personname>
|
||||
</author>
|
||||
<author>
|
||||
<personname>
|
||||
<firstname>Brett</firstname>
|
||||
<surname>Meyer</surname>
|
||||
</personname>
|
||||
</author>
|
||||
|
||||
<othercredit>
|
||||
<personname>
|
||||
<firstname>James</firstname>
|
||||
<surname>Cobb</surname>
|
||||
</personname>
|
||||
<affiliation>
|
||||
<shortaffil>Graphic Design</shortaffil>
|
||||
</affiliation>
|
||||
</othercredit>
|
||||
|
||||
<othercredit>
|
||||
<personname>
|
||||
<firstname>Cheyenne</firstname>
|
||||
<surname>Weaver</surname>
|
||||
</personname>
|
||||
<affiliation>
|
||||
<shortaffil>Graphic Design</shortaffil>
|
||||
</affiliation>
|
||||
</othercredit>
|
||||
|
||||
</authorgroup>
|
|
@ -1,11 +0,0 @@
|
|||
<!ENTITY version "WORKING">
|
||||
<!ENTITY today "TODAY">
|
||||
<!ENTITY copyrightYear "2004">
|
||||
<!ENTITY copyrightHolder "Red Hat, Inc.">
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<!ENTITY semi ";">
|
|
@ -1,54 +0,0 @@
|
|||
<?xml version='1.0' encoding='UTF-8' ?>
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<!DOCTYPE book SYSTEM "http://docbook.org/xml/5.0/dtd/docbook.dtd" [
|
||||
<!ENTITY % BOOK_ENTITIES SYSTEM "Hibernate_Getting_Started_Guide.ent">
|
||||
%BOOK_ENTITIES;
|
||||
]>
|
||||
|
||||
<book>
|
||||
<info>
|
||||
<title>Hibernate Getting Started Guide</title>
|
||||
<releaseinfo>&version;</releaseinfo>
|
||||
<edition>1.0</edition>
|
||||
<productname>JBoss Hibernate Core</productname>
|
||||
<productnumber>&version;</productnumber>
|
||||
<pubdate>&today;</pubdate>
|
||||
<issuenum>1</issuenum>
|
||||
<mediaobject>
|
||||
<imageobject role="fo">
|
||||
<imagedata fileref="images/hibernate_logo_a.png" align="center" />
|
||||
</imageobject>
|
||||
<imageobject role="html">
|
||||
<imagedata fileref="images/hibernate_logo_a.png" depth="3cm" />
|
||||
</imageobject>
|
||||
</mediaobject>
|
||||
<copyright>
|
||||
<year>©rightYear;</year>
|
||||
<holder>©rightHolder;</holder>
|
||||
</copyright>
|
||||
<authorgroup xmlns:xl="http://www.w3.org/1999/xlink">
|
||||
<author>
|
||||
<orgname><link xl:href="http://hibernate.org">The Hibernate Team</link></orgname>
|
||||
</author>
|
||||
<othercredit>
|
||||
<orgname><link xl:href="http://design.jboss.org/">The JBoss Visual Design Team</link></orgname>
|
||||
</othercredit>
|
||||
</authorgroup>
|
||||
</info>
|
||||
|
||||
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="content/preface.xml" />
|
||||
|
||||
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="content/obtaining.xml" />
|
||||
|
||||
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="content/tutorial_native.xml" />
|
||||
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="content/tutorial_annotations.xml" />
|
||||
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="content/tutorial_jpa.xml" />
|
||||
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="content/tutorial_envers.xml" />
|
||||
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="content/tutorial_osgi.xml" />
|
||||
|
||||
</book>
|
|
@ -1,187 +0,0 @@
|
|||
<?xml version='1.0' encoding='UTF-8' ?>
|
||||
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<chapter xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<title>Obtaining Hibernate</title>
|
||||
|
||||
<section xml:id="hibernate-gsg-setup-releaseBundle">
|
||||
<title>Release Bundle Downloads</title>
|
||||
<para>
|
||||
The Hibernate team provides release bundles hosted on the SourceForge File Release System, in
|
||||
<literal>ZIP</literal>
|
||||
and
|
||||
<literal>TGZ</literal>
|
||||
formats. Each release bundle contains<literal>JARs</literal>,
|
||||
documentation, source code, and other goodness.
|
||||
</para>
|
||||
<para>
|
||||
You can download releases of Hibernate, in your chosen format, from the list at
|
||||
<link xlink:href="http://sourceforge.net/projects/hibernate/files/hibernate4/"/>.
|
||||
</para>
|
||||
<itemizedlist>
|
||||
<listitem>
|
||||
<para>
|
||||
The <filename>lib/required/</filename> directory contains all the JARs Hibernate requires. All the
|
||||
jars in this directory must also be included in your project's classpath.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
The <filename>/lib/jpa/</filename> directory contains the
|
||||
<systemitem>hibernate-entitymanager</systemitem> jar and its dependencies beyond those
|
||||
in <filename>lib/required/</filename>. This defines Hibernate support for
|
||||
<link xlink:href="http://jcp.org/en/jsr/detail?id=317">JPA</link>.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
The <filename>lib/envers</filename> directory contains the <systemitem>hibernate-envers</systemitem>
|
||||
jar and its dependencies beyond those in <filename>lib/required/</filename>
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
The <filename>lib/optional</filename> directory contains the jars needed for optional features of
|
||||
Hibernate.
|
||||
</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
</section>
|
||||
|
||||
<section xml:id="hibernate-gsg-setup-mavenRepoArtifacts">
|
||||
<title>Maven Repository Artifacts</title>
|
||||
<note>
|
||||
<para>
|
||||
The authoritative repository for Hibernate artifacts is the JBoss Maven repository. The team responsible
|
||||
for the JBoss Maven repository maintains a number of Wiki pages that contain important information.
|
||||
</para>
|
||||
<itemizedlist>
|
||||
<title>Maven Repository Wiki Pages</title>
|
||||
<listitem>
|
||||
<para>
|
||||
<link xlink:href="http://community.jboss.org/docs/DOC-14900"/> - General information about the repository.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
<link xlink:href="http://community.jboss.org/docs/DOC-15170"/> - Information about setting up the JBoss
|
||||
repositories in order to do development work on JBoss projects themselves.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
<link xlink:href="http://community.jboss.org/docs/DOC-15169"/> - Information about setting up access to
|
||||
the repository to use JBoss projects as part of your own software.
|
||||
</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
</note>
|
||||
|
||||
<para>
|
||||
Hibernate produces a number of artifacts (all under the <systemitem>org.hibernate</systemitem> groupId):
|
||||
</para>
|
||||
<variablelist>
|
||||
<title>Hibernate Artifacts under groupId <systemitem>org.hibernate</systemitem>
|
||||
</title>
|
||||
<varlistentry>
|
||||
<term>hibernate-core</term>
|
||||
<listitem>
|
||||
<para>
|
||||
The main artifact, needed to build applications using the native Hibernate APIs including
|
||||
defining metadata in both annotations as well as Hibernate's own <filename>hbm.xml</filename>
|
||||
format.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
<varlistentry>
|
||||
<term>hibernate-entitymanager</term>
|
||||
<listitem>
|
||||
<para>
|
||||
Represents Hibernate's implementation of <phrase>JPA</phrase>, as specified at
|
||||
<link xlink:href="http://jcp.org/en/jsr/detail?id=317"/>.
|
||||
</para>
|
||||
<para>
|
||||
This artifact depends on <systemitem>hibernate-core</systemitem>
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
<varlistentry>
|
||||
<term>hibernate-envers</term>
|
||||
<listitem>
|
||||
<para>
|
||||
An optional module that provides historical auditing of changes to your entities.
|
||||
</para>
|
||||
<para>
|
||||
This artifact depends on both <systemitem>hibernate-core</systemitem>
|
||||
and <systemitem>hibernate-entitymanager</systemitem>.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
<varlistentry>
|
||||
<term>hibernate-c3p0</term>
|
||||
<listitem>
|
||||
<para>
|
||||
Provides integration between Hibernate and the <application>C3P0</application> connection
|
||||
pool library. See <link xlink:href="http://sourceforge.net/projects/c3p0/"/> for information
|
||||
about <application>C3P0</application>.
|
||||
</para>
|
||||
<para>
|
||||
This artifact depends on <systemitem>hibernate-core</systemitem>, but is generally included
|
||||
in a project as a runtime dependency. It pulls in the <application>C3P0</application>
|
||||
dependencies automatically.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
<varlistentry>
|
||||
<term>hibernate-proxool</term>
|
||||
<listitem>
|
||||
<para>
|
||||
Provides integration between Hibernate and the <application>Proxool</application> connection
|
||||
pool library. See <link xlink:href="http://proxool.sourceforge.net/"/> for more information about
|
||||
this library.
|
||||
</para>
|
||||
<para>
|
||||
This artifact depends on <systemitem>hibernate-core</systemitem>, but is generally included
|
||||
in a project as a runtime dependency. It pulls in the <application>Proxool</application>
|
||||
dependencies automatically..
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
<varlistentry>
|
||||
<term>hibernate-ehcache</term>
|
||||
<listitem>
|
||||
<para>
|
||||
Privides integration between Hibernate and <application>EhCache</application>, as a
|
||||
second-level cache. See <link xlink:href="http://ehcache.sourceforge.net/"/> for more
|
||||
information about<application>EhCache</application>.
|
||||
</para>
|
||||
<para>
|
||||
This artifact depends on <systemitem>hibernate-core</systemitem>, but is generally included
|
||||
in a project as a runtime dependency. It pulls in the <application>Ehcache</application>
|
||||
dependencies automatically.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
<varlistentry>
|
||||
<term>hibernate-infinispan</term>
|
||||
<listitem>
|
||||
<para>
|
||||
Provides integration between Hibernate and <application>Infinispan</application>, as a
|
||||
second-level cache. See <link xlink:href="http://jboss.org/infinispan"/> for more information
|
||||
about <application>Infinispan</application>.
|
||||
</para>
|
||||
<para>
|
||||
This artifact depends on <systemitem>hibernate-core</systemitem>, but is generally included
|
||||
in a project as a runtime dependency. It pulls in the <application>Infinispan</application>
|
||||
dependencies automatically.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
</variablelist>
|
||||
</section>
|
||||
</chapter>
|
|
@ -1,103 +0,0 @@
|
|||
<?xml version='1.0' encoding="UTF-8"?>
|
||||
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<preface xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" >
|
||||
<title>Preface</title>
|
||||
|
||||
<para>
|
||||
Working with both Object-Oriented software and Relational Databases can be cumbersome and time consuming.
|
||||
Development costs are significantly higher due to a paradigm mismatch between how data is represented in objects
|
||||
versus relational databases. Hibernate is an Object/Relational Mapping solution for Java environments. The
|
||||
term Object/Relational Mapping refers to the technique of mapping data between an object model representation to
|
||||
a relational data model representation. See
|
||||
<link xlink:href="http://en.wikipedia.org/wiki/Object-relational_mapping"/> for a good high-level discussion.
|
||||
</para>
|
||||
|
||||
<note>
|
||||
<para>
|
||||
You do not need a strong background in SQL to use Hibernate, but having a basic understanding of the
|
||||
concepts can help you understand Hibernate more fully and quickly. An understanding of data modeling
|
||||
principles is especially important. You might want to consider these resources as a good starting point:
|
||||
</para>
|
||||
<itemizedlist>
|
||||
<title>Data Modeling Resources</title>
|
||||
<listitem>
|
||||
<para>
|
||||
<link xlink:href="http://www.agiledata.org/essays/dataModeling101.html"/>
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
<link xlink:href="http://en.wikipedia.org/wiki/Data_modeling"/>
|
||||
</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
</note>
|
||||
|
||||
<para>
|
||||
Hibernate takes care of the mapping from Java classes to database tables, and from Java data types to SQL data
|
||||
types. In addition, it provides data query and retrieval facilities. It can significantly reduce development
|
||||
time otherwise spent with manual data handling in SQL and JDBC. Hibernate’s design goal is to relieve the
|
||||
developer from 95% of common data persistence-related programming tasks by eliminating the need for manual,
|
||||
hand-crafted data processing using SQL and JDBC. However, unlike many other persistence solutions, Hibernate
|
||||
does not hide the power of SQL from you and guarantees that your investment in relational technology and
|
||||
knowledge is as valid as always.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Hibernate may not be the best solution for data-centric applications that only use stored-procedures to
|
||||
implement the business logic in the database, it is most useful with object-oriented domain models and business
|
||||
logic in the Java-based middle-tier. However, Hibernate can certainly help you to remove or encapsulate
|
||||
vendor-specific SQL code and streamlines the common task of translating result sets from a tabular
|
||||
representation to a graph of objects.
|
||||
</para>
|
||||
|
||||
<section>
|
||||
<title>Get Involved</title>
|
||||
<itemizedlist>
|
||||
<listitem>
|
||||
<para>
|
||||
Use Hibernate and report any bugs or issues you find. See
|
||||
<link xlink:href="http://hibernate.org/issuetracker.html"/> for details.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
Try your hand at fixing some bugs or implementing enhancements. Again, see
|
||||
<link xlink:href="http://hibernate.org/issuetracker.html"/>.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
Engage with the community using mailing lists, forums, IRC, or other ways listed at
|
||||
<link xlink:href="http://hibernate.org/community.html"/>.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
Help improve or translate this documentation. Contact us on
|
||||
the developer mailing list if you have interest.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
Spread the word. Let the rest of your organization know about the benefits of
|
||||
Hibernate.<!-- I didn't like 'evangelize'. Too many religious overtones. I'd like something stronger than this though. I'll have a think. -->
|
||||
</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>Tutorial code</title>
|
||||
<para>
|
||||
The referenced projects and code for the tutorials in this guide are available at
|
||||
<link xlink:href="files/hibernate-tutorials.zip"/>.
|
||||
</para>
|
||||
</section>
|
||||
</preface>
|
|
@ -1,133 +0,0 @@
|
|||
<?xml version='1.0' encoding='UTF-8' ?>
|
||||
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<chapter xmlns="http://docbook.org/ns/docbook">
|
||||
<title>Tutorial Using Native Hibernate APIs and Annotation Mappings</title>
|
||||
|
||||
<para>
|
||||
This tutorial is located within the download bundle under <filename>annotations</filename>.
|
||||
</para>
|
||||
<itemizedlist>
|
||||
<title>Objectives</title>
|
||||
<listitem>
|
||||
<para>
|
||||
Use annotations to provide mapping information
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
Use the <phrase>native</phrase> Hibernate APIs
|
||||
</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-annotations-config">
|
||||
<title>The Hibernate configuration file</title>
|
||||
|
||||
<para>
|
||||
The contents are identical to <xref linkend="hibernate-gsg-tutorial-basic-config"/>, with one important
|
||||
difference. The <varname>mapping</varname> element at the very end naming the annotated entity class using
|
||||
the <option>class</option> attribute.
|
||||
</para>
|
||||
</section>
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-annotations-entity">
|
||||
<title>The annotated entity Java class</title>
|
||||
<para>
|
||||
The entity class in this tutorial is <classname>org.hibernate.tutorial.annotations.Event</classname> which
|
||||
follows JavaBean conventions. In fact the class itself is identical to the one in <xref
|
||||
linkend="hibernate-gsg-tutorial-basic-entity"/>, except that annotations are used to provide the metadata,
|
||||
rather than a separate <filename>hbm.xml</filename> file.
|
||||
</para>
|
||||
|
||||
<example xml:id="hibernate-gsg-tutorial-annotations-entity-entity">
|
||||
<title>Identifying the class as an entity</title>
|
||||
<programlisting role="JAVA">@Entity
|
||||
@Table( name = "EVENTS" )
|
||||
public class Event {
|
||||
...
|
||||
}</programlisting>
|
||||
</example>
|
||||
|
||||
<para>
|
||||
<!-- Is an entity an interface?? -->
|
||||
The <interfacename>@javax.persistence.Entity</interfacename> annotation is used to mark a class as an entity.
|
||||
It functions the same as the <varname>class</varname> mapping element discussed in <xref
|
||||
linkend="hibernate-gsg-tutorial-basic-mapping"/>. Additionally the
|
||||
<interfacename>@javax.persistence.Table</interfacename> annotation explicitly specifies the table
|
||||
name. Without this specification, the default table name would be <database class="table">EVENT</database>).
|
||||
</para>
|
||||
|
||||
<example xml:id="hibernate-gsg-tutorial-annotations-entity-id">
|
||||
<title>Identifying the identifier property</title>
|
||||
<programlisting role="JAVA">@Id
|
||||
@GeneratedValue(generator="increment")
|
||||
@GenericGenerator(name="increment", strategy = "increment")
|
||||
public Long getId() {
|
||||
return id;
|
||||
}</programlisting>
|
||||
</example>
|
||||
|
||||
<para>
|
||||
<interfacename>@javax.persistence.Id</interfacename> marks the property which defines the
|
||||
entity's identifier. <interfacename>@javax.persistence.GeneratedValue</interfacename> and
|
||||
<interfacename>@org.hibernate.annotations.GenericGenerator</interfacename> work in tandem
|
||||
to indicate that Hibernate should use Hibernate's <literal>increment</literal> generation
|
||||
strategy for this entity's identifier values.
|
||||
</para>
|
||||
|
||||
<example xml:id="hibernate-gsg-tutorial-annotations-entity-properties">
|
||||
<title>Identifying basic properties</title>
|
||||
<programlisting role="JAVA">public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name = "EVENT_DATE")
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}</programlisting>
|
||||
</example>
|
||||
|
||||
<para>
|
||||
As in <xref linkend="hibernate-gsg-tutorial-basic-mapping"/>, the <varname>date</varname> property needs
|
||||
special handling to account for its special naming and its SQL type.
|
||||
</para>
|
||||
</section>
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-annotations-test">
|
||||
<title>Example code</title>
|
||||
<para>
|
||||
<classname>org.hibernate.tutorial.annotations.AnnotationsIllustrationTest</classname> is essentially the
|
||||
same as <classname>org.hibernate.tutorial.hbm.NativeApiIllustrationTest</classname> discussed in
|
||||
<xref linkend="hibernate-gsg-tutorial-basic-test"/>.
|
||||
</para>
|
||||
</section>
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-annotations-further">
|
||||
<title>Take it further!</title>
|
||||
<itemizedlist>
|
||||
<title>Practice Exercises</title>
|
||||
<listitem>
|
||||
<para>
|
||||
Add an association to the <classname>Event</classname> entity to model a message thread. Use the
|
||||
<citetitle pubwork="book">Developer Guide</citetitle> as a guide.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
Add a callback to receive notifications when an <classname>Event</classname> is created, updated or
|
||||
deleted. Try the same with an event listener. Use the <citetitle pubwork="book">Developer
|
||||
Guide</citetitle> as a guide.
|
||||
</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
</section>
|
||||
|
||||
</chapter>
|
|
@ -1,107 +0,0 @@
|
|||
<?xml version='1.0' encoding='UTF-8' ?>
|
||||
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<chapter xmlns="http://docbook.org/ns/docbook">
|
||||
<title>Tutorial Using Envers</title>
|
||||
|
||||
<para>
|
||||
This tutorial is located within the download bundle under <filename>envers</filename>.
|
||||
</para>
|
||||
<itemizedlist>
|
||||
<title>Objectives</title>
|
||||
<listitem>
|
||||
<para>
|
||||
Configure Envers.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
Use the Envers APIs to view and analyze historical data.
|
||||
</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-envers-config">
|
||||
<title><filename>persistence.xml</filename></title>
|
||||
<para>
|
||||
This file was discussed in the <systemitem>JPA</systemitem> tutorial in <xref
|
||||
linkend="hibernate-gsg-tutorial-jpa-config" />, and is essentially the same here.
|
||||
</para>
|
||||
</section>
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-envers-entity">
|
||||
<title>The annotated entity Java class</title>
|
||||
<para>
|
||||
Again, the entity is largely the same as in <xref linkend="hibernate-gsg-tutorial-jpa-entity" />. The major
|
||||
difference is the addition of the <interfacename>@org.hibernate.envers.Audited</interfacename> annotation, which
|
||||
tells Envers to automatically track changes to this entity.
|
||||
</para>
|
||||
</section>
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-envers-test">
|
||||
<title>Example code</title>
|
||||
<para>
|
||||
Again, this tutorial makes use of the <systemitem>JPA</systemitem> APIs. However, the code also makes a change to one
|
||||
of the entities, then uses the Envers API to pull back the initial <firstterm>revision</firstterm> as well as the updated
|
||||
revision. A revision refers to a version of an entity.
|
||||
</para>
|
||||
<example xml:id="hibernate-gsg-tutorial-envers-test-api">
|
||||
<title>Using the <interfacename>org.hibernate.envers.AuditReader</interfacename></title>
|
||||
<programlisting role="JAVA">public void testBasicUsage() {
|
||||
...
|
||||
AuditReader reader = AuditReaderFactory.get( entityManager );
|
||||
Event firstRevision = reader.find( Event.class, 2L, 1 );
|
||||
...
|
||||
Event secondRevision = reader.find( Event.class, 2L, 2 );
|
||||
...
|
||||
}</programlisting>
|
||||
</example>
|
||||
<procedure>
|
||||
<title>Description of Example</title>
|
||||
<step>
|
||||
<para>
|
||||
An <interfacename>org.hibernate.envers.AuditReader</interfacename> is obtained from the
|
||||
<classname>org.hibernate.envers.AuditReaderFactory</classname> which wraps the
|
||||
<interfacename>javax.persistence.EntityManager</interfacename>.
|
||||
</para>
|
||||
</step>
|
||||
<step>
|
||||
<para>
|
||||
Next, the <methodname>find</methodname> method retrieves specific revisions of the entity. The first call
|
||||
reads <literal>find revision number 1 of Event with id 2</literal>. The second call reads <literal>find
|
||||
revision number 2 of Event with id 2</literal>.
|
||||
</para>
|
||||
</step>
|
||||
</procedure>
|
||||
</section>
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-annotations-further">
|
||||
<title>Take it further!</title>
|
||||
<itemizedlist>
|
||||
<title>Practice Exercises</title>
|
||||
<listitem>
|
||||
<para>
|
||||
Provide a custom revision entity to additionally capture who made the changes.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
Write a query to retrieve only historical data which meets some criteria. Use the
|
||||
<citetitle pubwork="book">Envers User Guide</citetitle> to see how Envers queries are constructed.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
Experiment with auditing entities which have many-to-one, many-to-many relations as well as collections.
|
||||
Try retrieving historical versions (revisions) of such entities and navigating the object tree.
|
||||
</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
</section>
|
||||
|
||||
</chapter>
|
|
@ -1,145 +0,0 @@
|
|||
<?xml version='1.0' encoding='UTF-8' ?>
|
||||
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<chapter xmlns="http://docbook.org/ns/docbook">
|
||||
<title>Tutorial Using the <firstterm><phrase>Java Persistence API (JPA)</phrase></firstterm></title>
|
||||
|
||||
<para>
|
||||
This tutorial is located within the download bundle under <filename>entitymanager</filename>.
|
||||
</para>
|
||||
<itemizedlist>
|
||||
<title>Objectives</title>
|
||||
<listitem>
|
||||
<para>
|
||||
Use annotations to provide mapping information.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
Use <systemitem>JPA</systemitem>.
|
||||
</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-jpa-config">
|
||||
<title><filename>persistence.xml</filename></title>
|
||||
|
||||
<para>
|
||||
The previous tutorials used the Hibernate-specific
|
||||
<filename><replaceable>hibernate.cfg.xml</replaceable></filename> configuration file.
|
||||
<systemitem>JPA</systemitem>, however, defines a different bootstrap process that uses its own configuration
|
||||
file named <filename>persistence.xml</filename>. This bootstrapping process is defined by the
|
||||
<systemitem>JPA</systemitem> specification. In <trademark>Java</trademark> SE environments the persistence
|
||||
provider (Hibernate in this case) is required to locate all <systemitem>JPA</systemitem> configuration files
|
||||
by classpath lookup of the <filename>META-INF/persistence.xml</filename> resource name.
|
||||
</para>
|
||||
|
||||
<example xml:id="hibernate-gsg-tutorial-jpa-config-pu">
|
||||
<title><filename>persistence.xml</filename></title>
|
||||
<programlisting role="XML"><![CDATA[<persistence 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"
|
||||
version="2.0">
|
||||
<persistence-unit name="org.hibernate.tutorial.jpa">
|
||||
...
|
||||
</persistence-unit>
|
||||
</persistence>]]></programlisting>
|
||||
</example>
|
||||
|
||||
<para>
|
||||
<filename>persistence.xml</filename> files should provide a unique name for each <phrase>persistence
|
||||
unit</phrase>. Applications use this name to reference the configuration when obtaining an
|
||||
<interfacename>javax.persistence.EntityManagerFactory</interfacename> reference.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
The settings defined in the <varname>properties</varname> element are discussed in <xref
|
||||
linkend="hibernate-gsg-tutorial-basic-config"/>. Here the <literal>javax.persistence</literal>-prefixed
|
||||
varieties are used when possible. Notice that the remaining Hibernate-specific configuration setting names
|
||||
are now prefixed with <literal>hibernate.</literal>.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Additionally, the <varname>class</varname> element functions the same as in <xref
|
||||
linkend="hibernate-gsg-tutorial-annotations-config"/>.
|
||||
</para>
|
||||
</section>
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-jpa-entity">
|
||||
<title>The annotated entity Java class</title>
|
||||
<para>
|
||||
The entity is exactly the same as in <xref linkend="hibernate-gsg-tutorial-annotations-entity"/>
|
||||
</para>
|
||||
</section>
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-jpa-test">
|
||||
<title>Example code</title>
|
||||
<para>
|
||||
The previous tutorials used the Hibernate APIs. This tutorial uses the <systemitem>JPA</systemitem> APIs.
|
||||
</para>
|
||||
|
||||
<example xml:id="hibernate-gsg-tutorial-jpa-test-setUp">
|
||||
<title>Obtaining the <interfacename>javax.persistence.EntityManagerFactory</interfacename></title>
|
||||
<programlisting role="JAVA">protected void setUp() throws Exception {
|
||||
entityManagerFactory = Persistence.createEntityManagerFactory( "org.hibernate.tutorial.jpa" );
|
||||
}</programlisting>
|
||||
</example>
|
||||
|
||||
<para>
|
||||
Notice again that the persistence unit name is <literal>org.hibernate.tutorial.jpa</literal>, which matches
|
||||
<xref linkend="hibernate-gsg-tutorial-jpa-config-pu"/>
|
||||
</para>
|
||||
|
||||
<example xml:id="hibernate-gsg-tutorial-jpa-test-saving">
|
||||
<title>Saving (persisting) entities</title>
|
||||
<programlisting role="JAVA">EntityManager entityManager = entityManagerFactory.createEntityManager();
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.persist( new Event( "Our very first event!", new Date() ) );
|
||||
entityManager.persist( new Event( "A follow up event", new Date() ) );
|
||||
entityManager.getTransaction().commit();
|
||||
entityManager.close();</programlisting>
|
||||
</example>
|
||||
|
||||
<para>
|
||||
The code is similar to <xref linkend="hibernate-gsg-tutorial-basic-test-saving"/>. An
|
||||
<interfacename>javax.persistence.EntityManager</interfacename> interface is used instead of a
|
||||
<interfacename>org.hibernate.Session</interfacename> interface. <systemitem>JPA</systemitem> calls this
|
||||
operation <methodname>persist</methodname> instead of <methodname>save</methodname>.
|
||||
</para>
|
||||
|
||||
<example xml:id="hibernate-gsg-tutorial-jpa-test-list">
|
||||
<title>Obtaining a list of entities</title>
|
||||
<programlisting role="JAVA"><![CDATA[entityManager = entityManagerFactory.createEntityManager();
|
||||
entityManager.getTransaction().begin();
|
||||
List<Event> result = entityManager.createQuery( "from Event", Event.class ).getResultList();
|
||||
for ( Event event : result ) {
|
||||
System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() );
|
||||
}
|
||||
entityManager.getTransaction().commit();
|
||||
entityManager.close();]]></programlisting>
|
||||
</example>
|
||||
|
||||
<para>
|
||||
Again, the code is pretty similar to <xref linkend="hibernate-gsg-tutorial-basic-test-list"/>.
|
||||
</para>
|
||||
</section>
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-annotations-further">
|
||||
<title>Take it further!</title>
|
||||
<itemizedlist>
|
||||
<title>Practice Exercises</title>
|
||||
<listitem>
|
||||
<para>
|
||||
Develop an EJB Session bean to investigate implications of using a container-managed
|
||||
persistence context (<interfacename>javax.persistence.EntityManager</interfacename>). Try both
|
||||
stateless and stateful use-cases.
|
||||
</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
</section>
|
||||
</chapter>
|
|
@ -1,342 +0,0 @@
|
|||
<?xml version='1.0' encoding='UTF-8' ?>
|
||||
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<chapter xmlns="http://docbook.org/ns/docbook">
|
||||
<title>Tutorial Using Native Hibernate APIs and <phrase>hbm.xml</phrase> Mappings</title>
|
||||
|
||||
<para>
|
||||
This tutorial is located within the download bundle under <filename>basic/</filename>.
|
||||
</para>
|
||||
<itemizedlist>
|
||||
<title>Objectives</title>
|
||||
<listitem>
|
||||
<para>
|
||||
using Hibernate mapping files (<phrase>hbm.xml</phrase>) to provide mapping information
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
using the <phrase>native</phrase> Hibernate APIs
|
||||
</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-basic-config">
|
||||
<title>The Hibernate configuration file</title>
|
||||
|
||||
<para>
|
||||
The resource file <filename>hibernate.cfg.xml</filename> defines Hibernate configuration
|
||||
information.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
The <varname>connection.driver_class</varname>, <varname>connection.url</varname>,
|
||||
<varname>connection.username</varname> and <varname>connection.password</varname>
|
||||
<varname>property</varname> elements define JDBC connection information. These tutorials utilize the H2
|
||||
in-memory database, So the values of these properties are all specific to running H2 in its in-memory mode.
|
||||
<varname>connection.pool_size</varname> is used to configure the number of connections in Hibernate's
|
||||
built-in connection pool.
|
||||
</para>
|
||||
|
||||
<important>
|
||||
<para>
|
||||
The built-in Hibernate connection pool is in no way intended for production use. It lacks several
|
||||
features found on production-ready connection pools. See the section discussion in <citetitle
|
||||
pubwork="book">Hibernate Developer Guide</citetitle> for further information.
|
||||
</para>
|
||||
</important>
|
||||
|
||||
<para>
|
||||
The <varname>dialect</varname> property specifies the particular SQL variant with which Hibernate will
|
||||
converse.
|
||||
</para>
|
||||
|
||||
<tip>
|
||||
<para>
|
||||
In most cases, Hibernate is able to properly determine which dialect to use. This is particularly useful
|
||||
if your application targets multiple databases. This is discussed in detail in the <citetitle
|
||||
pubwork="book">Hibernate Developer Guide</citetitle>
|
||||
</para>
|
||||
</tip>
|
||||
|
||||
<para>
|
||||
The <varname>hbm2ddl.auto</varname> property enables automatic generation of database schemas directly into
|
||||
the database.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Finally, add the mapping file(s) for persistent classes to the configuration. The <option>resource</option>
|
||||
attribute of the <varname>mapping</varname> element causes Hibernate to attempt to locate that mapping as a
|
||||
classpath resource, using a <classname>java.lang.ClassLoader</classname> lookup.
|
||||
</para>
|
||||
|
||||
</section>
|
||||
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-basic-entity">
|
||||
<title>The entity Java class</title>
|
||||
<para>
|
||||
The entity class for this tutorial is <classname>org.hibernate.tutorial.hbm.Event</classname>.
|
||||
</para>
|
||||
<itemizedlist>
|
||||
<title>Notes About the Entity</title>
|
||||
<listitem>
|
||||
<para>
|
||||
This class uses standard JavaBean naming conventions for property getter and setter methods, as well as
|
||||
private visibility for the fields. Although this is the recommended design, it is not required.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
The <methodname>no-argument</methodname> constructor, which is also a JavaBean convention, is a
|
||||
requirement for all persistent classes. Hibernate needs to create objects for you, using Java
|
||||
Reflection. The constructor can be private. However, package or public visibility is required for runtime
|
||||
proxy generation and efficient data retrieval without bytecode instrumentation.
|
||||
</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
</section>
|
||||
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-basic-mapping">
|
||||
<title>The mapping file</title>
|
||||
<para>
|
||||
The <filename>hbm.xml</filename> mapping file for this tutorial is the classpath resource
|
||||
<filename>org/hibernate/tutorial/hbm/Event.hbm.xml</filename> as we saw in
|
||||
<xref linkend="hibernate-gsg-tutorial-basic-config"/>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Hibernate uses the mapping metadata to determine how to load and store objects of the persistent class. The
|
||||
Hibernate mapping file is one choice for providing Hibernate with this metadata.
|
||||
</para>
|
||||
|
||||
<example xml:id="hibernate-gsg-tutorial-basic-mapping-class">
|
||||
<title>The <literal>class</literal> mapping element</title>
|
||||
<programlisting role="XML"><![CDATA[<class name="Event" table="EVENTS">
|
||||
...
|
||||
</class>]]></programlisting>
|
||||
</example>
|
||||
|
||||
<orderedlist>
|
||||
<title>Functions of the <varname>class</varname> mapping element</title>
|
||||
<listitem>
|
||||
<para>
|
||||
The <option>name</option> attribute (combined here with the <option>package</option> attribute from
|
||||
the containing <varname>hibernate-mapping</varname> element) names the FQN of the class to be
|
||||
defined as an entity.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
The <option>table</option> attribute names the database table which contains the data for this
|
||||
entity.
|
||||
</para>
|
||||
</listitem>
|
||||
</orderedlist>
|
||||
|
||||
<para>
|
||||
Instances of the <classname>Event</classname> class are now mapped to rows in the
|
||||
<database class="table">EVENTS</database> table.
|
||||
</para>
|
||||
|
||||
<example xml:id="hibernate-gsg-tutorial-basic-mapping-id">
|
||||
<title>The <literal>id</literal> mapping element</title>
|
||||
<programlisting role="XML"><![CDATA[<id name="id" column="EVENT_ID">
|
||||
...
|
||||
</id>]]></programlisting>
|
||||
</example>
|
||||
|
||||
<para>
|
||||
Hibernate uses the property named by the <varname>id</varname> element to uniquely identify rows in the
|
||||
table.
|
||||
</para>
|
||||
|
||||
<important>
|
||||
<!-- Again, perhaps more sense moving this to the Dev Guide -->
|
||||
<para>
|
||||
It is not required for the <varname>id</varname> element to map to the table's actual primary key
|
||||
column(s), but it is the normal convention. Tables mapped in Hibernate do not even need to define
|
||||
primary keys. However, it is strongly recommend that all schemas define proper referential
|
||||
integrity. Therefore <varname>id</varname> and <phrase>primary key</phrase> are used interchangeably
|
||||
throughout Hibernate documentation.
|
||||
</para>
|
||||
</important>
|
||||
<para>
|
||||
The <varname>id</varname> element here identifies the <database class="field">EVENT_ID</database> column as
|
||||
the primary key of the <database class="table">EVENTS</database> table. It also identifies the
|
||||
<varname>id</varname> property of the <classname>Event</classname> class as the property containing the
|
||||
identifier value.
|
||||
</para>
|
||||
<para>
|
||||
The <varname>generator</varname> element nested inside the <varname>id</varname> element informs Hibernate
|
||||
about which strategy is used to generated primary key values for this entity. This example uses a simple
|
||||
incrementing count.
|
||||
</para>
|
||||
|
||||
<example xml:id="hibernate-gsg-tutorial-basic-mapping-property">
|
||||
<title>The <literal>property</literal> mapping element</title>
|
||||
<programlisting role="XML"><![CDATA[<property name="date" type="timestamp" column="EVENT_DATE"/>
|
||||
<property name="title"/>]]></programlisting>
|
||||
</example>
|
||||
|
||||
<para>
|
||||
The two <varname>property</varname> elements declare the remaining two properties of the
|
||||
<classname>Event</classname> class: <varname>date</varname> and <varname>title</varname>. The
|
||||
<varname>date</varname> property mapping includes the <option>column</option> attribute, but the
|
||||
<varname>title</varname> does not. In the absence of a <option>column</option> attribute, Hibernate
|
||||
uses the property name as the column name. This is appropriate for <varname>title</varname>, but since
|
||||
<varname>date</varname> is a reserved keyword in most databases, you need to specify a non-reserved
|
||||
word for the column name.
|
||||
</para>
|
||||
<para>
|
||||
The <varname>title</varname> mapping also lacks a <option>type</option> attribute. The types
|
||||
declared and used in the mapping files are neither Java data types nor SQL database types. Instead,
|
||||
they are <firstterm><phrase>Hibernate mapping types</phrase></firstterm>. Hibernate mapping types are
|
||||
converters which translate between Java and SQL data types. Hibernate attempts to determine the correct
|
||||
conversion and mapping type autonomously if the <option>type</option> attribute is not present in the
|
||||
mapping, by using Java reflection to determine the Java type of the declared property and using a
|
||||
default mapping type for that Java type.
|
||||
</para>
|
||||
<para>
|
||||
In some cases this automatic detection might not chose the default you expect or need, as seen with the
|
||||
<varname>date</varname> property. Hibernate cannot know if the property, which is of type
|
||||
<classname>java.util.Date</classname>, should map to a SQL <database class="datatype">DATE</database>,
|
||||
<database class="datatype">TIME</database>, or <database class="datatype">TIMESTAMP</database> datatype.
|
||||
Full date and time information is preserved by mapping the property to a <type>timestamp</type> converter,
|
||||
which identifies an instance of the class <classname>org.hibernate.type.TimestampType</classname>.
|
||||
</para>
|
||||
|
||||
<tip>
|
||||
<!-- This tip probably makes more sense in the Dev Guide -->
|
||||
<para>
|
||||
Hibernate determines the mapping type using reflection when the mapping files are processed. This
|
||||
process adds overhead in terms of time and resources. If startup performance is important, consider
|
||||
explicitly defining the type to use.
|
||||
</para>
|
||||
</tip>
|
||||
|
||||
</section>
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-basic-test">
|
||||
<title>Example code</title>
|
||||
<para>
|
||||
The <classname>org.hibernate.tutorial.hbm.NativeApiIllustrationTest</classname> class illustrates using
|
||||
the Hibernate <phrase>native API</phrase>.
|
||||
</para>
|
||||
<note>
|
||||
<para>
|
||||
The examples in these tutorials are presented as JUnit tests, for ease of use. One benefit of this
|
||||
approach is that <methodname>setUp</methodname> and <methodname>tearDown</methodname> roughly illustrate
|
||||
how a <interfacename>org.hibernate.SessionFactory</interfacename> is created at the start-up of an
|
||||
application and closed<!--destroyed?--> at the end of the application lifecycle.
|
||||
</para>
|
||||
</note>
|
||||
|
||||
<example xml:id="hibernate-gsg-tutorial-basic-test-setUp">
|
||||
<title>Obtaining the <interfacename>org.hibernate.SessionFactory</interfacename></title>
|
||||
<programlisting role="JAVA">protected void setUp() throws Exception {
|
||||
// A SessionFactory is set up once for an application
|
||||
sessionFactory = new Configuration()
|
||||
.configure() // configures settings from hibernate.cfg.xml
|
||||
.buildSessionFactory();
|
||||
}</programlisting>
|
||||
</example>
|
||||
|
||||
<procedure>
|
||||
<title>Tutorial Workflow</title>
|
||||
<step>
|
||||
<title>The configuration is loaded.</title>
|
||||
<para>
|
||||
The <classname>org.hibernate.cfg.Configuration</classname> class is the first thing to notice. In this
|
||||
tutorial, all configuration details are located in the <filename>hibernate.cfg.xml</filename> file
|
||||
discussed in <xref linkend="hibernate-gsg-tutorial-basic-config"/>.
|
||||
</para>
|
||||
</step>
|
||||
<step>
|
||||
<title>The <interfacename>org.hibernate.SessionFactory</interfacename> is created.</title>
|
||||
<para>
|
||||
The <classname>org.hibernate.cfg.Configuration</classname> then creates the
|
||||
<interfacename>org.hibernate.SessionFactory</interfacename> which is a thread-safe object that is
|
||||
instantiated once to serve the entire application.
|
||||
</para>
|
||||
</step>
|
||||
<step>
|
||||
<title><interfacename>SessionFactory</interfacename> creates <classname>Session</classname> instances.</title>
|
||||
<para>
|
||||
The <interfacename>org.hibernate.SessionFactory</interfacename> acts as a factory for
|
||||
<interfacename>org.hibernate.Session</interfacename> instances as can be seen in the
|
||||
<methodname>testBasicUsage</methodname> method.
|
||||
<!-- todo : reference to a discussion in dev guide -->
|
||||
</para>
|
||||
</step>
|
||||
<step>
|
||||
<title><classname>Session</classname>s perform work.</title>
|
||||
<para>
|
||||
A <interfacename>org.hibernate.Session</interfacename> should be thought of as a corollary to a "unit of
|
||||
work".
|
||||
</para>
|
||||
</step>
|
||||
</procedure>
|
||||
|
||||
<example xml:id="hibernate-gsg-tutorial-basic-test-saving">
|
||||
<title>Saving entities</title>
|
||||
<programlisting role="JAVA">Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
session.save( new Event( "Our very first event!", new Date() ) );
|
||||
session.save( new Event( "A follow up event", new Date() ) );
|
||||
session.getTransaction().commit();
|
||||
session.close();</programlisting>
|
||||
</example>
|
||||
|
||||
<para>
|
||||
<methodname>testBasicUsage</methodname> first creates some new <classname>Event</classname> objects and
|
||||
hands them over to Hibernate for management, using the <methodname>save</methodname> method. Hibernate now
|
||||
takes responsibility to perform an <command>INSERT</command> on the database.
|
||||
</para>
|
||||
|
||||
<example xml:id="hibernate-gsg-tutorial-basic-test-list">
|
||||
<title>Obtaining a list of entities</title>
|
||||
<programlisting role="JAVA"><![CDATA[session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
List result = session.createQuery( "from Event" ).list();
|
||||
for ( Event event : (List<Event>) result ) {
|
||||
System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() );
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();]]></programlisting>
|
||||
</example>
|
||||
|
||||
<para>
|
||||
<methodname>testBasicUsage</methodname> illustrates use of the <firstterm>Hibernate Query Language
|
||||
(HQL)</firstterm> to load all existing <classname>Event</classname> objects from the database and generate the
|
||||
appropriate <literal>SELECT</literal> SQL, send it to the database and populate <classname>Event</classname>
|
||||
objects with the result set data.
|
||||
</para>
|
||||
</section>
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-annotations-further">
|
||||
<title>Take it further!</title>
|
||||
<itemizedlist>
|
||||
<title>Practice Exercises</title>
|
||||
<listitem>
|
||||
<para>
|
||||
Reconfigure the examples to connect to your own persistent relational database.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
With help of the <citetitle pubwork="book">Developer Guide</citetitle>, add an association to
|
||||
the <classname>Event</classname> entity to model a message thread.
|
||||
</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
</section>
|
||||
|
||||
</chapter>
|
|
@ -1,110 +0,0 @@
|
|||
<?xml version='1.0' encoding='UTF-8' ?>
|
||||
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<chapter xmlns="http://docbook.org/ns/docbook"
|
||||
xmlns:xl="http://www.w3.org/1999/xlink"
|
||||
xmlns:xi="http://www.w3.org/2001/XInclude">
|
||||
<title>OSGi Tutorial</title>
|
||||
|
||||
<para>
|
||||
Hibernate targets the OSGi 4.3 spec or later and supports three types
|
||||
of configurations.
|
||||
|
||||
<orderedlist>
|
||||
<listitem>
|
||||
<link xl:href="https://github.com/hibernate/hibernate-orm/tree/master/documentation/src/main/docbook/quickstart/tutorials/osgi/managed-jpa">Container-Managed JPA</link>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<link xl:href="https://github.com/hibernate/hibernate-orm/tree/master/documentation/src/main/docbook/quickstart/tutorials/osgi/unmanaged-jpa">Unmanaged JPA</link>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<link xl:href="https://github.com/hibernate/hibernate-orm/tree/master/documentation/src/main/docbook/quickstart/tutorials/osgi/unmanaged-native">Unmanaged Native</link>
|
||||
</listitem>
|
||||
</orderedlist>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
For more details about OSGi, the three configurations, hibernate-osgi, extensions points, and caveats, please
|
||||
see the OSGi chapter of the Developer's Guide!
|
||||
</para>
|
||||
|
||||
<section>
|
||||
<title>
|
||||
Project Overview
|
||||
</title>
|
||||
<para>
|
||||
Each configuration has a QuickStart project located within the download bundle (under <filename>osgi</filename>).
|
||||
The bundles can be used as-is within Apache Karaf. Feel free to use them as literal
|
||||
"quick start" bundle templates.
|
||||
</para>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>
|
||||
Project Structure
|
||||
</title>
|
||||
<itemizedlist>
|
||||
<listitem>
|
||||
<filename>osgi/datasource-h2.xml</filename>: Enterprise OSGi JPA usage can include a DataSource installed in the container.
|
||||
The client bundle's <literal>persistence.xml</literal> references the DataSource through JNDI. For an
|
||||
example, see how managed-jpa's <literal>persistence.xml</literal> calls out the
|
||||
<literal>jta-data-source</literal>.
|
||||
</listitem>
|
||||
<listitem>
|
||||
<filename>osgi/[project]/features.xml</filename>: This is arguably the most important "quick start" material. It defines
|
||||
a single Karaf feature ("hibernate-test") that demonstrates the necessary 3rd party libraries and
|
||||
bundle activation ordering.
|
||||
</listitem>
|
||||
<listitem>
|
||||
<filename>osgi/[project]/pom.xml</filename>: The POM includes typical compile-time dependencies (JPA, OSGi Core,
|
||||
OSGi Enterprise), as well as OSGi manifest data.
|
||||
</listitem>
|
||||
<listitem>
|
||||
<filename>osgi/[project]/src/main/resources/OSGI-INF/blueprint/blueprint.xml</filename>:
|
||||
The Blueprint includes container-managed EntityManager
|
||||
injection (for managed-jpa), as well as demonstrations showing how to register
|
||||
your custom implementations of Hibernate extension points.
|
||||
</listitem>
|
||||
<listitem>
|
||||
<filename>osgi/[project]/src/main/resources/META-INF/persistence.xml</filename> or
|
||||
<filename>osgi/[project]/src/main/resources/hibernate.cfg.xml</filename>: Note that the configurations
|
||||
are no different than typical uses of Hibernate!
|
||||
</listitem>
|
||||
<listitem>
|
||||
<filename>osgi/[project]/src/main/java/org/hibernate/osgitest/HibernateUtil.java</filename>: Demonstrates how to create an
|
||||
EntityManagerFactory (JPA) or SessionFactory (Native) using hibernate-osgi's services. Note that in
|
||||
managed-jpa, this is replaced by <literal>DataPointServiceImpl#entityManager</literal>, injected by
|
||||
<filename>blueprint.xml</filename> (described above).
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
</section>
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-envers-config">
|
||||
<title>
|
||||
TODOs
|
||||
</title>
|
||||
<itemizedlist>
|
||||
<listitem>
|
||||
If using managed-jpa, <filename>features.xml</filename> will need the path to
|
||||
<filename>datasource-h2.xml</filename> updated.
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
</section>
|
||||
|
||||
<section xml:id="hibernate-gsg-tutorial-envers-config">
|
||||
<title>
|
||||
Karaf Commands
|
||||
</title>
|
||||
<para>
|
||||
All three bundles include Karaf Commands that can be used directly on the Karaf command line to test
|
||||
basic persistence operations ("dp:add [name]", "dp:getall", "dp:deleteall", etc.). I leave them in the
|
||||
QuickStarts as a useful sanity check.
|
||||
</para>
|
||||
</section>
|
||||
|
||||
</chapter>
|
Binary file not shown.
Before Width: | Height: | Size: 31 KiB |
|
@ -1,28 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
|
||||
<!--
|
||||
First install the H2 driver using:
|
||||
> install -s mvn:com.h2database/h2/1.3.163
|
||||
|
||||
Then copy this file to the deploy folder
|
||||
-->
|
||||
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
|
||||
|
||||
<bean id="dataSource" class="org.h2.jdbcx.JdbcDataSource">
|
||||
<property name="URL" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE"/>
|
||||
<property name="user" value="sa"/>
|
||||
<property name="password" value=""/>
|
||||
</bean>
|
||||
|
||||
<service interface="javax.sql.DataSource" ref="dataSource">
|
||||
<service-properties>
|
||||
<entry key="osgi.jndi.service.name" value="jdbc/h2ds"/>
|
||||
</service-properties>
|
||||
</service>
|
||||
</blueprint>
|
|
@ -1 +0,0 @@
|
|||
/target
|
|
@ -1,81 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<features>
|
||||
<feature name="hibernate-test">
|
||||
|
||||
<!-- JTA -->
|
||||
<config name="org.apache.aries.transaction">
|
||||
aries.transaction.recoverable = true
|
||||
aries.transaction.timeout = 600
|
||||
aries.transaction.howl.logFileDir = /tmp/karaf/txlog
|
||||
aries.transaction.howl.maxLogFiles = 2
|
||||
aries.transaction.howl.maxBlocksPerFile = 512
|
||||
aries.transaction.howl.bufferSizeKBytes = 4
|
||||
</config>
|
||||
<bundle dependency="true" start-level="30">mvn:org.apache.geronimo.specs/geronimo-jta_1.1_spec/1.1.1</bundle>
|
||||
<bundle start-level="30">mvn:org.apache.aries.transaction/org.apache.aries.transaction.blueprint/1.0.0</bundle>
|
||||
<bundle start-level="30">mvn:org.apache.aries.transaction/org.apache.aries.transaction.manager/1.0.1</bundle>
|
||||
|
||||
<!-- JPA -->
|
||||
<bundle start-level="30">mvn:org.hibernate.javax.persistence/hibernate-jpa-2.1-api/1.0.0.Final</bundle>
|
||||
<!-- No container currently supports JPA 2.1. Clone and build Aries from the following fork (upgrades to
|
||||
JPA 2.1). Aries should be upgrading as soon as the spec is out.
|
||||
https://github.com/brmeyer/aries/tree/jpa21 -->
|
||||
<bundle start-level="30">mvn:org.apache.aries/org.apache.aries.util/1.1.1-SNAPSHOT</bundle>
|
||||
<bundle start-level="30">mvn:org.apache.aries.jpa/org.apache.aries.jpa.api/1.0.1-SNAPSHOT</bundle>
|
||||
<bundle start-level="30">mvn:org.apache.aries.jpa/org.apache.aries.jpa.blueprint.aries/1.0.2-SNAPSHOT</bundle>
|
||||
<bundle start-level="30">mvn:org.apache.aries.jpa/org.apache.aries.jpa.container/1.0.1-SNAPSHOT</bundle>
|
||||
<bundle start-level="30">mvn:org.apache.aries.jpa/org.apache.aries.jpa.container.context/1.0.2-SNAPSHOT</bundle>
|
||||
|
||||
<!-- JNDI -->
|
||||
<bundle start-level="30">mvn:org.apache.aries/org.apache.aries.util/1.0.0</bundle>
|
||||
<bundle start-level="30">mvn:org.apache.aries.jndi/org.apache.aries.jndi.api/1.0.0</bundle>
|
||||
<bundle start-level="30">mvn:org.apache.aries.jndi/org.apache.aries.jndi.core/1.0.0</bundle>
|
||||
<bundle start-level="30">mvn:org.apache.aries.jndi/org.apache.aries.jndi.rmi/1.0.0</bundle>
|
||||
<bundle start-level="30">mvn:org.apache.aries.jndi/org.apache.aries.jndi.url/1.0.0</bundle>
|
||||
<bundle start-level="30">mvn:org.apache.aries.jndi/org.apache.aries.jndi.legacy.support/1.0.0</bundle>
|
||||
|
||||
<!-- Taken from Karaf-Tutorial -->
|
||||
<bundle>mvn:commons-collections/commons-collections/3.2.1</bundle>
|
||||
<bundle>mvn:commons-pool/commons-pool/1.5.4</bundle>
|
||||
<bundle>mvn:commons-dbcp/commons-dbcp/1.4</bundle>
|
||||
<bundle>mvn:commons-lang/commons-lang/2.6</bundle>
|
||||
<bundle>wrap:mvn:net.sourceforge.serp/serp/1.13.1</bundle>
|
||||
|
||||
<bundle>mvn:com.h2database/h2/1.3.170</bundle>
|
||||
<bundle>blueprint:file:/[PATH]/datasource-h2.xml</bundle>
|
||||
|
||||
<!-- These do not natively support OSGi, so using 3rd party bundles. -->
|
||||
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.antlr/2.7.7_5</bundle>
|
||||
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.dom4j/1.6.1_5</bundle>
|
||||
|
||||
<!-- These do not natively support OSGi, so wrap with BND. -->
|
||||
<bundle>wrap:mvn:org.jboss/jandex/1.1.0.Final</bundle>
|
||||
|
||||
<bundle>mvn:com.fasterxml/classmate/0.8.0</bundle>
|
||||
<bundle>mvn:org.apache.logging.log4j/log4j-api/2.0</bundle>
|
||||
<bundle>mvn:log4j/log4j/1.2.17</bundle>
|
||||
<bundle>mvn:org.jboss.logging/jboss-logging/3.2.1.Final</bundle>
|
||||
<bundle>mvn:org.javassist/javassist/3.18.1-GA</bundle>
|
||||
|
||||
<bundle>mvn:org.hibernate.common/hibernate-commons-annotations/4.0.5.Final</bundle>
|
||||
|
||||
<bundle>mvn:org.hibernate/hibernate-core/5.0.0-SNAPSHOT</bundle>
|
||||
<bundle>mvn:org.hibernate/hibernate-entitymanager/5.0.0-SNAPSHOT</bundle>
|
||||
|
||||
<!-- TODO: It seems that the persistence unit bundle needs to be started
|
||||
before hibernate-osgi. When the BundleActivator is started,
|
||||
the persistence unit is provided even though managed-jpa
|
||||
hasn't completely started yet. If that happens, you'll get an "illegal
|
||||
bundle state" exception. Is there a way for the activator to
|
||||
watch for bundles with PUs before registering the persistence provider? -->
|
||||
<bundle>mvn:org.hibernate.osgi/managed-jpa/1.0.0</bundle>
|
||||
|
||||
<bundle>mvn:org.hibernate/hibernate-osgi/5.0.0-SNAPSHOT</bundle>
|
||||
</feature>
|
||||
</features>
|
|
@ -1,70 +0,0 @@
|
|||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.hibernate.osgi</groupId>
|
||||
<artifactId>managed-jpa</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.hibernate.javax.persistence</groupId>
|
||||
<artifactId>hibernate-jpa-2.1-api</artifactId>
|
||||
<version>1.0.0.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.osgi</groupId>
|
||||
<artifactId>org.osgi.core</artifactId>
|
||||
<version>4.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.osgi</groupId>
|
||||
<artifactId>org.osgi.enterprise</artifactId>
|
||||
<version>4.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.karaf.shell</groupId>
|
||||
<artifactId>org.apache.karaf.shell.console</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-bundle-plugin</artifactId>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<instructions>
|
||||
<Bundle-SymbolicName>org.hibernate.osgi.managed-jpa</Bundle-SymbolicName>
|
||||
<Bundle-Name>managed-jpa</Bundle-Name>
|
||||
<Bundle-Version>1.0.0</Bundle-Version>
|
||||
<Export-Package>
|
||||
org.hibernate.osgitest,
|
||||
org.hibernate.osgitest.entity
|
||||
</Export-Package>
|
||||
<Import-Package>
|
||||
org.apache.felix.service.command,
|
||||
org.apache.felix.gogo.commands,
|
||||
org.apache.karaf.shell.console,
|
||||
org.apache.karaf.shell.commands,
|
||||
javax.persistence;version="[1.0.0,2.1.0]",
|
||||
<!-- Needed for proxying's Javassist enhancement during runtime -->
|
||||
org.hibernate.proxy,
|
||||
javassist.util.proxy,
|
||||
*
|
||||
</Import-Package>
|
||||
<Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>
|
||||
</instructions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -1,37 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgitest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.osgitest.entity.DataPoint;
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public interface DataPointService {
|
||||
|
||||
public void add(DataPoint dp);
|
||||
|
||||
public List<DataPoint> getAll();
|
||||
|
||||
public void deleteAll();
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgitest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.hibernate.osgitest.entity.DataPoint;
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public class DataPointServiceImpl implements DataPointService {
|
||||
|
||||
private EntityManager entityManager;
|
||||
|
||||
public void add(DataPoint dp) {
|
||||
entityManager.persist( dp );
|
||||
entityManager.flush();
|
||||
}
|
||||
|
||||
public List<DataPoint> getAll() {
|
||||
return entityManager.createQuery( "select d from DataPoint d", DataPoint.class ).getResultList();
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
entityManager.createQuery( "delete from DataPoint" ).executeUpdate();
|
||||
entityManager.flush();
|
||||
}
|
||||
|
||||
public EntityManager getEntityManager() {
|
||||
return entityManager;
|
||||
}
|
||||
|
||||
public void setEntityManager(EntityManager entityManager) {
|
||||
this.entityManager = entityManager;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.hibernate.osgitest.command;
|
||||
|
||||
import org.apache.felix.gogo.commands.Action;
|
||||
import org.apache.felix.gogo.commands.Argument;
|
||||
import org.apache.felix.gogo.commands.Command;
|
||||
import org.apache.felix.service.command.CommandSession;
|
||||
import org.hibernate.osgitest.DataPointService;
|
||||
import org.hibernate.osgitest.entity.DataPoint;
|
||||
|
||||
@Command(scope = "dp", name = "add")
|
||||
public class AddCommand implements Action {
|
||||
@Argument(index=0, name="Name", required=true, description="Name", multiValued=false)
|
||||
String name;
|
||||
|
||||
private DataPointService dpService;
|
||||
|
||||
public void setDpService(DataPointService dpService) {
|
||||
this.dpService = dpService;
|
||||
}
|
||||
|
||||
public Object execute(CommandSession session) throws Exception {
|
||||
DataPoint dp = new DataPoint();
|
||||
dp.setName( name );
|
||||
dpService.add( dp );
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.hibernate.osgitest.command;
|
||||
|
||||
import org.apache.felix.gogo.commands.Action;
|
||||
import org.apache.felix.gogo.commands.Command;
|
||||
import org.apache.felix.service.command.CommandSession;
|
||||
import org.hibernate.osgitest.DataPointService;
|
||||
|
||||
@Command(scope = "dp", name = "deleteAll")
|
||||
public class DeleteAllCommand implements Action {
|
||||
private DataPointService dpService;
|
||||
|
||||
public void setDpService(DataPointService dpService) {
|
||||
this.dpService = dpService;
|
||||
}
|
||||
|
||||
public Object execute(CommandSession session) throws Exception {
|
||||
dpService.deleteAll();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.hibernate.osgitest.command;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.felix.gogo.commands.Action;
|
||||
import org.apache.felix.gogo.commands.Command;
|
||||
import org.apache.felix.service.command.CommandSession;
|
||||
import org.hibernate.osgitest.DataPointService;
|
||||
import org.hibernate.osgitest.entity.DataPoint;
|
||||
|
||||
@Command(scope = "dp", name = "getAll")
|
||||
public class GetAllCommand implements Action {
|
||||
private DataPointService dpService;
|
||||
|
||||
public void setDpService(DataPointService dpService) {
|
||||
this.dpService = dpService;
|
||||
}
|
||||
|
||||
public Object execute(CommandSession session) throws Exception {
|
||||
List<DataPoint> dps = dpService.getAll();
|
||||
for (DataPoint dp : dps) {
|
||||
System.out.println(dp.getId() + ", " + dp.getName());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgitest.entity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
@Entity
|
||||
public class DataPoint {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
version="1.0">
|
||||
<persistence-unit name="managed-jpa" transaction-type="JTA">
|
||||
<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/h2ds)</jta-data-source>
|
||||
|
||||
<properties>
|
||||
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
|
||||
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
|
||||
<property name="hibernate.archive.autodetection" value="class"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
|
@ -1,46 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<blueprint default-activation="eager"
|
||||
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.0.0"
|
||||
xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.0.0">
|
||||
|
||||
<!-- This gets the container-managed EntityManager and injects it into the DataPointServiceImpl bean. -->
|
||||
<bean id="dpService" class="org.hibernate.osgitest.DataPointServiceImpl">
|
||||
<jpa:context unitname="managed-jpa" property="entityManager"/>
|
||||
<tx:transaction method="*" value="Required"/>
|
||||
</bean>
|
||||
<service ref="dpService" interface="org.hibernate.osgitest.DataPointService"/>
|
||||
|
||||
<!-- This demonstrates how to register your custom implementations of Hibernate extension points, such as
|
||||
Integrator and TypeContributor. -->
|
||||
<!-- <bean id="integrator" class="your.package.IntegratorImpl"/>
|
||||
<service ref="integrator" interface="org.hibernate.integrator.spi.Integrator"/>
|
||||
<bean id="typeContributor" class="your.package.TypeContributorImpl"/>
|
||||
<service ref="typeContributor" interface="org.hibernate.metamodel.spi.TypeContributor"/> -->
|
||||
|
||||
<!-- This bundle makes use of Karaf commands to demonstrate core persistence operations. Feel free to remove it. -->
|
||||
<command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
|
||||
<command>
|
||||
<action class="org.hibernate.osgitest.command.AddCommand">
|
||||
<property name="dpService" ref="dpService"/>
|
||||
</action>
|
||||
</command>
|
||||
<command>
|
||||
<action class="org.hibernate.osgitest.command.GetAllCommand">
|
||||
<property name="dpService" ref="dpService"/>
|
||||
</action>
|
||||
</command>
|
||||
<command>
|
||||
<action class="org.hibernate.osgitest.command.DeleteAllCommand">
|
||||
<property name="dpService" ref="dpService"/>
|
||||
</action>
|
||||
</command>
|
||||
</command-bundle>
|
||||
</blueprint>
|
|
@ -1 +0,0 @@
|
|||
/target
|
|
@ -1,47 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<features>
|
||||
<feature name="hibernate-test">
|
||||
|
||||
<!-- JTA -->
|
||||
<bundle start-level="30">mvn:org.apache.geronimo.specs/geronimo-jta_1.1_spec/1.1.1</bundle>
|
||||
|
||||
<!-- JPA -->
|
||||
<bundle start-level="30">mvn:org.hibernate.javax.persistence/hibernate-jpa-2.1-api/1.0.0.Final</bundle>
|
||||
|
||||
<!-- Taken from Karaf-Tutorial -->
|
||||
<bundle>mvn:commons-collections/commons-collections/3.2.1</bundle>
|
||||
<bundle>mvn:commons-pool/commons-pool/1.5.4</bundle>
|
||||
<bundle>mvn:commons-dbcp/commons-dbcp/1.4</bundle>
|
||||
<bundle>mvn:commons-lang/commons-lang/2.6</bundle>
|
||||
<bundle>wrap:mvn:net.sourceforge.serp/serp/1.13.1</bundle>
|
||||
|
||||
<bundle>mvn:com.h2database/h2/1.3.170</bundle>
|
||||
|
||||
<!-- These do not natively support OSGi, so using 3rd party bundles. -->
|
||||
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.antlr/2.7.7_5</bundle>
|
||||
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.dom4j/1.6.1_5</bundle>
|
||||
|
||||
<!-- These do not natively support OSGi, so wrap with BND. -->
|
||||
<bundle>wrap:mvn:org.jboss/jandex/1.1.0.Final</bundle>
|
||||
|
||||
<bundle>mvn:com.fasterxml/classmate/0.8.0</bundle>
|
||||
<bundle>mvn:org.apache.logging.log4j/log4j-api/2.0</bundle>
|
||||
<bundle>mvn:log4j/log4j/1.2.17</bundle>
|
||||
<bundle>mvn:org.jboss.logging/jboss-logging/3.2.1.Final</bundle>
|
||||
<bundle>mvn:org.javassist/javassist/3.18.1-GA</bundle>
|
||||
|
||||
<bundle>mvn:org.hibernate.common/hibernate-commons-annotations/4.0.5.Final</bundle>
|
||||
|
||||
<bundle>mvn:org.hibernate/hibernate-core/5.0.0-SNAPSHOT</bundle>
|
||||
<bundle>mvn:org.hibernate/hibernate-entitymanager/5.0.0-SNAPSHOT</bundle>
|
||||
<bundle>mvn:org.hibernate/hibernate-osgi/5.0.0-SNAPSHOT</bundle>
|
||||
|
||||
<bundle>mvn:org.hibernate.osgi/unmanaged-jpa/1.0.0</bundle>
|
||||
</feature>
|
||||
</features>
|
|
@ -1,81 +0,0 @@
|
|||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.hibernate.osgi</groupId>
|
||||
<artifactId>unmanaged-jpa</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.hibernate.javax.persistence</groupId>
|
||||
<artifactId>hibernate-jpa-2.1-api</artifactId>
|
||||
<version>1.0.0.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.osgi</groupId>
|
||||
<artifactId>org.osgi.core</artifactId>
|
||||
<version>4.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.osgi</groupId>
|
||||
<artifactId>org.osgi.enterprise</artifactId>
|
||||
<version>4.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.karaf.shell</groupId>
|
||||
<artifactId>org.apache.karaf.shell.console</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-entitymanager</artifactId>
|
||||
<version>5.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.3.170</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-bundle-plugin</artifactId>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<instructions>
|
||||
<Bundle-SymbolicName>org.hibernate.osgi.unmanaged-jpa</Bundle-SymbolicName>
|
||||
<Bundle-Name>unmanaged-jpa</Bundle-Name>
|
||||
<Bundle-Version>1.0.0</Bundle-Version>
|
||||
<Export-Package>
|
||||
org.hibernate.osgitest,
|
||||
org.hibernate.osgitest.entity
|
||||
</Export-Package>
|
||||
<Import-Package>
|
||||
org.apache.felix.service.command,
|
||||
org.apache.felix.gogo.commands,
|
||||
org.apache.karaf.shell.console,
|
||||
org.apache.karaf.shell.commands,
|
||||
org.h2,
|
||||
javax.persistence;version="[1.0.0,2.1.0]",
|
||||
<!-- Needed for proxying's Javassist enhancement during runtime -->
|
||||
org.hibernate.proxy,
|
||||
javassist.util.proxy,
|
||||
*
|
||||
</Import-Package>
|
||||
</instructions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -1,41 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgitest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.osgitest.entity.DataPoint;
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public interface DataPointService {
|
||||
|
||||
public void add(DataPoint dp);
|
||||
|
||||
public void update(DataPoint dp);
|
||||
|
||||
public DataPoint get(long id);
|
||||
|
||||
public List<DataPoint> getAll();
|
||||
|
||||
public void deleteAll();
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgitest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.hibernate.osgitest.entity.DataPoint;
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public class DataPointServiceImpl implements DataPointService {
|
||||
|
||||
private HibernateUtil hibernateUtil = new HibernateUtil();
|
||||
|
||||
public void add(DataPoint dp) {
|
||||
EntityManager em = hibernateUtil.getEntityManager();
|
||||
em.getTransaction().begin();
|
||||
em.persist( dp );
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void update(DataPoint dp) {
|
||||
EntityManager em = hibernateUtil.getEntityManager();
|
||||
em.getTransaction().begin();
|
||||
em.merge( dp );
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
public DataPoint get(long id) {
|
||||
EntityManager em = hibernateUtil.getEntityManager();
|
||||
em.getTransaction().begin();
|
||||
DataPoint dp = (DataPoint) em.createQuery( "from DataPoint dp where dp.id=" + id ).getSingleResult();
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
return dp;
|
||||
}
|
||||
|
||||
public List<DataPoint> getAll() {
|
||||
EntityManager em = hibernateUtil.getEntityManager();
|
||||
em.getTransaction().begin();
|
||||
List list = em.createQuery( "from DataPoint" ).getResultList();
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
return list;
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
EntityManager em = hibernateUtil.getEntityManager();
|
||||
em.getTransaction().begin();
|
||||
em.createQuery( "delete from DataPoint" ).executeUpdate();
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgitest;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
|
||||
public class HibernateUtil {
|
||||
|
||||
private EntityManagerFactory emf;
|
||||
|
||||
public EntityManager getEntityManager() {
|
||||
return getEntityManagerFactory().createEntityManager();
|
||||
}
|
||||
|
||||
private EntityManagerFactory getEntityManagerFactory() {
|
||||
if ( emf == null ) {
|
||||
Bundle thisBundle = FrameworkUtil.getBundle( HibernateUtil.class );
|
||||
// Could get this by wiring up OsgiTestBundleActivator as well.
|
||||
BundleContext context = thisBundle.getBundleContext();
|
||||
|
||||
ServiceReference serviceReference = context.getServiceReference( PersistenceProvider.class.getName() );
|
||||
PersistenceProvider persistenceProvider = (PersistenceProvider) context.getService( serviceReference );
|
||||
|
||||
emf = persistenceProvider.createEntityManagerFactory( "unmanaged-jpa", null );
|
||||
}
|
||||
return emf;
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgitest;
|
||||
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.integrator.spi.Integrator;
|
||||
import org.hibernate.service.spi.SessionFactoryServiceRegistry;
|
||||
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public class TestIntegrator implements Integrator {
|
||||
|
||||
public void integrate(
|
||||
Metadata metadata,
|
||||
SessionFactoryImplementor sessionFactory,
|
||||
SessionFactoryServiceRegistry serviceRegistry) {
|
||||
System.out.println("Integrator#integrate");
|
||||
}
|
||||
|
||||
public void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
|
||||
System.out.println("Integrator#disintegrate");
|
||||
}
|
||||
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgitest;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.hibernate.boot.registry.selector.StrategyRegistration;
|
||||
import org.hibernate.boot.registry.selector.StrategyRegistrationProvider;
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public class TestStrategyRegistrationProvider implements StrategyRegistrationProvider {
|
||||
|
||||
public Iterable<StrategyRegistration> getStrategyRegistrations() {
|
||||
System.out.println("StrategyRegistrationProvider#getStrategyRegistrations");
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgitest;
|
||||
|
||||
import org.hibernate.boot.model.TypeContributions;
|
||||
import org.hibernate.boot.model.TypeContributor;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public class TestTypeContributor implements TypeContributor {
|
||||
|
||||
public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
|
||||
System.out.println("TypeContributor#contribute");
|
||||
}
|
||||
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.hibernate.osgitest.command;
|
||||
|
||||
import org.apache.felix.gogo.commands.Action;
|
||||
import org.apache.felix.gogo.commands.Argument;
|
||||
import org.apache.felix.gogo.commands.Command;
|
||||
import org.apache.felix.service.command.CommandSession;
|
||||
import org.hibernate.osgitest.DataPointService;
|
||||
import org.hibernate.osgitest.entity.DataPoint;
|
||||
|
||||
@Command(scope = "dp", name = "addJPA")
|
||||
public class AddCommand implements Action {
|
||||
@Argument(index=0, name="Name", required=true, description="Name", multiValued=false)
|
||||
String name;
|
||||
|
||||
private DataPointService dpService;
|
||||
|
||||
public void setDpService(DataPointService dpService) {
|
||||
this.dpService = dpService;
|
||||
}
|
||||
|
||||
public Object execute(CommandSession session) throws Exception {
|
||||
DataPoint dp = new DataPoint();
|
||||
dp.setName( name );
|
||||
dpService.add( dp );
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.hibernate.osgitest.command;
|
||||
|
||||
import org.apache.felix.gogo.commands.Action;
|
||||
import org.apache.felix.gogo.commands.Command;
|
||||
import org.apache.felix.service.command.CommandSession;
|
||||
import org.hibernate.osgitest.DataPointService;
|
||||
|
||||
@Command(scope = "dp", name = "deleteAllJPA")
|
||||
public class DeleteAllCommand implements Action {
|
||||
private DataPointService dpService;
|
||||
|
||||
public void setDpService(DataPointService dpService) {
|
||||
this.dpService = dpService;
|
||||
}
|
||||
|
||||
public Object execute(CommandSession session) throws Exception {
|
||||
dpService.deleteAll();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.hibernate.osgitest.command;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.felix.gogo.commands.Action;
|
||||
import org.apache.felix.gogo.commands.Command;
|
||||
import org.apache.felix.service.command.CommandSession;
|
||||
import org.hibernate.osgitest.DataPointService;
|
||||
import org.hibernate.osgitest.entity.DataPoint;
|
||||
|
||||
@Command(scope = "dp", name = "getAllJPA")
|
||||
public class GetAllCommand implements Action {
|
||||
private DataPointService dpService;
|
||||
|
||||
public void setDpService(DataPointService dpService) {
|
||||
this.dpService = dpService;
|
||||
}
|
||||
|
||||
public Object execute(CommandSession session) throws Exception {
|
||||
List<DataPoint> dps = dpService.getAll();
|
||||
for (DataPoint dp : dps) {
|
||||
System.out.println(dp.getId() + ", " + dp.getName());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.hibernate.osgitest.command;
|
||||
|
||||
import org.apache.felix.gogo.commands.Action;
|
||||
import org.apache.felix.gogo.commands.Argument;
|
||||
import org.apache.felix.gogo.commands.Command;
|
||||
import org.apache.felix.service.command.CommandSession;
|
||||
import org.hibernate.osgitest.DataPointService;
|
||||
import org.hibernate.osgitest.entity.DataPoint;
|
||||
|
||||
@Command(scope = "dp", name = "updateJPA")
|
||||
public class UpdateCommand implements Action {
|
||||
@Argument(index=0, name="Id", required=true, description="Id", multiValued=false)
|
||||
String id;
|
||||
|
||||
@Argument(index=1, name="Name", required=true, description="Name", multiValued=false)
|
||||
String name;
|
||||
|
||||
private DataPointService dpService;
|
||||
|
||||
public void setDpService(DataPointService dpService) {
|
||||
this.dpService = dpService;
|
||||
}
|
||||
|
||||
public Object execute(CommandSession session) throws Exception {
|
||||
DataPoint dp = dpService.get( Long.valueOf( id ) );
|
||||
dp.setName( name );
|
||||
dpService.update( dp );
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgitest.entity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
@Entity
|
||||
public class DataPoint {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
version="1.0">
|
||||
<persistence-unit name="unmanaged-jpa">
|
||||
<class>org.hibernate.osgitest.entity.DataPoint</class>
|
||||
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||
|
||||
<properties>
|
||||
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
|
||||
<property name="hibernate.connection.url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE"/>
|
||||
<property name="hibernate.connection.username" value="sa"/>
|
||||
<property name="hibernate.connection.password" value=""/>
|
||||
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
|
@ -1,50 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<blueprint default-activation="eager"
|
||||
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
|
||||
<bean id="dpService" class="org.hibernate.osgitest.DataPointServiceImpl"/>
|
||||
<service ref="dpService" interface="org.hibernate.osgitest.DataPointService"/>
|
||||
|
||||
<!-- This demonstrates how to register your custom implementations of Hibernate extension points. -->
|
||||
|
||||
<bean id="integrator" class="org.hibernate.osgitest.TestIntegrator"/>
|
||||
<service ref="integrator" interface="org.hibernate.integrator.spi.Integrator"/>
|
||||
|
||||
<bean id="strategyRegistrationProvider" class="org.hibernate.osgitest.TestStrategyRegistrationProvider"/>
|
||||
<service ref="strategyRegistrationProvider"
|
||||
interface="org.hibernate.boot.registry.selector.StrategyRegistrationProvider"/>
|
||||
|
||||
<bean id="typeContributor" class="org.hibernate.osgitest.TestTypeContributor"/>
|
||||
<service ref="typeContributor" interface="org.hibernate.boot.model.TypeContributor"/>
|
||||
|
||||
<!-- This bundle makes use of Karaf commands to demonstrate core persistence operations. Feel free to remove it. -->
|
||||
<command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
|
||||
<command>
|
||||
<action class="org.hibernate.osgitest.command.AddCommand">
|
||||
<property name="dpService" ref="dpService"/>
|
||||
</action>
|
||||
</command>
|
||||
<command>
|
||||
<action class="org.hibernate.osgitest.command.UpdateCommand">
|
||||
<property name="dpService" ref="dpService"/>
|
||||
</action>
|
||||
</command>
|
||||
<command>
|
||||
<action class="org.hibernate.osgitest.command.GetAllCommand">
|
||||
<property name="dpService" ref="dpService"/>
|
||||
</action>
|
||||
</command>
|
||||
<command>
|
||||
<action class="org.hibernate.osgitest.command.DeleteAllCommand">
|
||||
<property name="dpService" ref="dpService"/>
|
||||
</action>
|
||||
</command>
|
||||
</command-bundle>
|
||||
</blueprint>
|
|
@ -1 +0,0 @@
|
|||
/target
|
|
@ -1,79 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<features>
|
||||
<feature name="hibernate-test">
|
||||
|
||||
<!--<feature>karaf-framework</feature>-->
|
||||
|
||||
<!-- JTA -->
|
||||
<bundle start-level="30">mvn:org.apache.geronimo.specs/geronimo-jta_1.1_spec/1.1.1</bundle>
|
||||
|
||||
<!-- JPA -->
|
||||
<bundle start-level="30">mvn:org.hibernate.javax.persistence/hibernate-jpa-2.1-api/1.0.0.Final</bundle>
|
||||
|
||||
<!-- Taken from Karaf-Tutorial -->
|
||||
<bundle>mvn:commons-collections/commons-collections/3.2.1</bundle>
|
||||
<bundle>mvn:commons-pool/commons-pool/1.5.4</bundle>
|
||||
<bundle>mvn:commons-dbcp/commons-dbcp/1.4</bundle>
|
||||
<bundle>mvn:commons-lang/commons-lang/2.6</bundle>
|
||||
<bundle>wrap:mvn:net.sourceforge.serp/serp/1.13.1</bundle>
|
||||
|
||||
<bundle>mvn:com.h2database/h2/1.3.170</bundle>
|
||||
|
||||
<!-- These do not natively support OSGi, so using 3rd party bundles. -->
|
||||
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.antlr/2.7.7_5</bundle>
|
||||
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.dom4j/1.6.1_5</bundle>
|
||||
|
||||
<!-- These do not natively support OSGi, so wrap with BND. -->
|
||||
<bundle>wrap:mvn:org.jboss/jandex/1.1.0.Final</bundle>
|
||||
<!-- Optional. Needed to test C3P0 connection pools. -->
|
||||
<!-- <bundle>wrap:mvn:c3p0/c3p0/0.9.1</bundle> -->
|
||||
<!-- Optional. Needed to test Proxool connection pools. -->
|
||||
<!-- <bundle>wrap:mvn:proxool/proxool/0.8.3</bundle> -->
|
||||
<!-- Optional. Needed to test ehcache 2lc. -->
|
||||
<!-- <bundle>wrap:mvn:net.sf.ehcache/ehcache-core/2.4.3</bundle> -->
|
||||
|
||||
<bundle>mvn:com.fasterxml/classmate/0.8.0</bundle>
|
||||
<bundle>mvn:org.apache.logging.log4j/log4j-api/2.0</bundle>
|
||||
<bundle>mvn:log4j/log4j/1.2.17</bundle>
|
||||
<bundle>mvn:org.jboss.logging/jboss-logging/3.2.1.Final</bundle>
|
||||
<bundle>mvn:org.javassist/javassist/3.18.1-GA</bundle>
|
||||
|
||||
<bundle>mvn:org.hibernate.common/hibernate-commons-annotations/4.0.5.Final</bundle>
|
||||
|
||||
<!-- JACC is optional. -->
|
||||
<!--<bundle>mvn:javax.servlet/javax.servlet-api/3.0.1</bundle>
|
||||
<bundle>mvn:org.jboss.spec.javax.security.jacc/jboss-jacc-api_1.4_spec/1.0.2.Final</bundle>-->
|
||||
|
||||
<!-- hibernate-validator is optional. -->
|
||||
<!--<bundle>wrap:mvn:javax.validation/validation-api/1.0.0.GA</bundle>
|
||||
<bundle>mvn:org.hibernate/hibernate-validator/4.2.0.Final</bundle>-->
|
||||
|
||||
<!-- Optional. Needed to test infinispan 2lc. -->
|
||||
<!-- IMPORTANT: Infinispan requires the JRE sun.misc package. You
|
||||
MUST enable this in your OSGi container. For Karaf, add
|
||||
"org.osgi.framework.system.packages.extra=sun.misc" to etc/config.properties -->
|
||||
<!-- <bundle>wrap:mvn:org.jboss.marshalling/jboss-marshalling/1.3.17.GA</bundle>
|
||||
<bundle>wrap:mvn:org.jboss.marshalling/jboss-marshalling-river/1.3.17.GA</bundle>
|
||||
<bundle>wrap:mvn:org.jboss/staxmapper/1.1.0.Final</bundle>
|
||||
<bundle>mvn:org.jgroups/jgroups/3.2.8.Final</bundle>
|
||||
<bundle>mvn:org.infinispan/infinispan-core/5.2.0.Beta3</bundle> -->
|
||||
|
||||
<bundle>mvn:org.hibernate/hibernate-core/5.0.0-SNAPSHOT</bundle>
|
||||
<!-- TODO: Shouldn't need this, but hibernate-osgi's activator is a catch-all for SF and EMF. -->
|
||||
<bundle>mvn:org.hibernate/hibernate-entitymanager/5.0.0-SNAPSHOT</bundle>
|
||||
<bundle>mvn:org.hibernate/hibernate-envers/5.0.0-SNAPSHOT</bundle>
|
||||
<!-- <bundle>mvn:org.hibernate/hibernate-c3p0/5.0.0-SNAPSHOT</bundle> -->
|
||||
<!-- <bundle>mvn:org.hibernate/hibernate-proxool/5.0.0-SNAPSHOT</bundle> -->
|
||||
<!-- <bundle>mvn:org.hibernate/hibernate-ehcache/5.0.0-SNAPSHOT</bundle> -->
|
||||
<!-- <bundle>mvn:org.hibernate/hibernate-infinispan/5.0.0-SNAPSHOT</bundle> -->
|
||||
<bundle>mvn:org.hibernate/hibernate-osgi/5.0.0-SNAPSHOT</bundle>
|
||||
|
||||
<bundle>mvn:org.hibernate.osgi/unmanaged-native/1.0.0</bundle>
|
||||
</feature>
|
||||
</features>
|
|
@ -1,89 +0,0 @@
|
|||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.hibernate.osgi</groupId>
|
||||
<artifactId>unmanaged-native</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.hibernate.javax.persistence</groupId>
|
||||
<artifactId>hibernate-jpa-2.1-api</artifactId>
|
||||
<version>1.0.0.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.osgi</groupId>
|
||||
<artifactId>org.osgi.core</artifactId>
|
||||
<version>4.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.osgi</groupId>
|
||||
<artifactId>org.osgi.enterprise</artifactId>
|
||||
<version>4.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.karaf.shell</groupId>
|
||||
<artifactId>org.apache.karaf.shell.console</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>5.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-envers</artifactId>
|
||||
<version>5.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.3.170</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-bundle-plugin</artifactId>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<instructions>
|
||||
<Bundle-SymbolicName>org.hibernate.osgi.unmanaged-native</Bundle-SymbolicName>
|
||||
<Bundle-Name>unmanaged-native</Bundle-Name>
|
||||
<Bundle-Version>1.0.0</Bundle-Version>
|
||||
<Export-Package>
|
||||
org.hibernate.osgitest,
|
||||
org.hibernate.osgitest.entity
|
||||
</Export-Package>
|
||||
<Import-Package>
|
||||
org.apache.felix.service.command,
|
||||
org.apache.felix.gogo.commands,
|
||||
org.apache.karaf.shell.console,
|
||||
org.apache.karaf.shell.commands,
|
||||
org.h2,
|
||||
org.hibernate,
|
||||
org.hibernate.cfg,
|
||||
org.hibernate.service,
|
||||
javax.persistence;version="[1.0.0,2.1.0]",
|
||||
<!-- Needed for proxying's Javassist enhancement during runtime -->
|
||||
org.hibernate.proxy,
|
||||
javassist.util.proxy,
|
||||
*
|
||||
</Import-Package>
|
||||
</instructions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -1,47 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgitest;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.envers.DefaultRevisionEntity;
|
||||
import org.hibernate.osgitest.entity.DataPoint;
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public interface DataPointService {
|
||||
|
||||
public void add(DataPoint dp);
|
||||
|
||||
public void update(DataPoint dp);
|
||||
|
||||
public DataPoint get(long id);
|
||||
|
||||
public DataPoint load(long id);
|
||||
|
||||
public List<DataPoint> getAll();
|
||||
|
||||
public Map<Number, DefaultRevisionEntity> getRevisions(long id);
|
||||
|
||||
public void deleteAll();
|
||||
}
|
|
@ -1,103 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgitest;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.envers.AuditReader;
|
||||
import org.hibernate.envers.AuditReaderFactory;
|
||||
import org.hibernate.envers.DefaultRevisionEntity;
|
||||
import org.hibernate.osgitest.entity.DataPoint;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public class DataPointServiceImpl implements DataPointService {
|
||||
|
||||
private HibernateUtil hibernateUtil = new HibernateUtil();
|
||||
|
||||
public void add(DataPoint dp) {
|
||||
Session s = hibernateUtil.getSession();
|
||||
s.getTransaction().begin();
|
||||
s.persist( dp );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
public void update(DataPoint dp) {
|
||||
Session s = hibernateUtil.getSession();
|
||||
s.getTransaction().begin();
|
||||
s.update( dp );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
public DataPoint get(long id) {
|
||||
Session s = hibernateUtil.getSession();
|
||||
s.getTransaction().begin();
|
||||
DataPoint dp = (DataPoint) s.createCriteria( DataPoint.class ).add(
|
||||
Restrictions.eq( "id", id ) ).uniqueResult();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
return dp;
|
||||
}
|
||||
|
||||
// Test lazy loading (mainly to make sure the proxy classes work in OSGi)
|
||||
public DataPoint load(long id) {
|
||||
Session s = hibernateUtil.getSession();
|
||||
s.getTransaction().begin();
|
||||
DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long(id) );
|
||||
// initialize
|
||||
dp.getName();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
return dp;
|
||||
}
|
||||
|
||||
public List<DataPoint> getAll() {
|
||||
Session s = hibernateUtil.getSession();
|
||||
s.getTransaction().begin();
|
||||
List list = s.createQuery( "from DataPoint" ).list();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
return list;
|
||||
}
|
||||
|
||||
public Map<Number, DefaultRevisionEntity> getRevisions(long id) {
|
||||
Session s = hibernateUtil.getSession();
|
||||
AuditReader reader = AuditReaderFactory.get(s);
|
||||
List<Number> revisionNums = reader.getRevisions( DataPoint.class, id );
|
||||
return reader.findRevisions( DefaultRevisionEntity.class, new HashSet<Number>(revisionNums) );
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
Session s = hibernateUtil.getSession();
|
||||
s.getTransaction().begin();
|
||||
s.createQuery( "delete from DataPoint" ).executeUpdate();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgitest;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
|
||||
public class HibernateUtil {
|
||||
|
||||
private SessionFactory sf;
|
||||
|
||||
public Session getSession() {
|
||||
return getSessionFactory().openSession();
|
||||
}
|
||||
|
||||
private SessionFactory getSessionFactory() {
|
||||
if ( sf == null ) {
|
||||
Bundle thisBundle = FrameworkUtil.getBundle( HibernateUtil.class );
|
||||
// Could get this by wiring up OsgiTestBundleActivator as well.
|
||||
BundleContext context = thisBundle.getBundleContext();
|
||||
|
||||
ServiceReference sr = context.getServiceReference( SessionFactory.class.getName() );
|
||||
sf = (SessionFactory) context.getService( sr );
|
||||
}
|
||||
return sf;
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgitest;
|
||||
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.integrator.spi.Integrator;
|
||||
import org.hibernate.service.spi.SessionFactoryServiceRegistry;
|
||||
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public class TestIntegrator implements Integrator {
|
||||
|
||||
public void integrate(
|
||||
Metadata metadata,
|
||||
SessionFactoryImplementor sessionFactory,
|
||||
SessionFactoryServiceRegistry serviceRegistry) {
|
||||
System.out.println("Integrator#integrate");
|
||||
}
|
||||
|
||||
public void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
|
||||
System.out.println("Integrator#disintegrate");
|
||||
}
|
||||
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgitest;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.hibernate.boot.registry.selector.StrategyRegistration;
|
||||
import org.hibernate.boot.registry.selector.StrategyRegistrationProvider;
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public class TestStrategyRegistrationProvider implements StrategyRegistrationProvider {
|
||||
|
||||
public Iterable<StrategyRegistration> getStrategyRegistrations() {
|
||||
System.out.println("StrategyRegistrationProvider#getStrategyRegistrations");
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgitest;
|
||||
|
||||
import org.hibernate.boot.model.TypeContributions;
|
||||
import org.hibernate.boot.model.TypeContributor;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public class TestTypeContributor implements TypeContributor {
|
||||
|
||||
public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
|
||||
System.out.println("TypeContributor#contribute");
|
||||
}
|
||||
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.hibernate.osgitest.command;
|
||||
|
||||
import org.apache.felix.gogo.commands.Action;
|
||||
import org.apache.felix.gogo.commands.Argument;
|
||||
import org.apache.felix.gogo.commands.Command;
|
||||
import org.apache.felix.service.command.CommandSession;
|
||||
import org.hibernate.osgitest.DataPointService;
|
||||
import org.hibernate.osgitest.entity.DataPoint;
|
||||
|
||||
@Command(scope = "dp", name = "add")
|
||||
public class AddCommand implements Action {
|
||||
@Argument(index=0, name="Name", required=true, description="Name", multiValued=false)
|
||||
String name;
|
||||
|
||||
private DataPointService dpService;
|
||||
|
||||
public void setDpService(DataPointService dpService) {
|
||||
this.dpService = dpService;
|
||||
}
|
||||
|
||||
public Object execute(CommandSession session) throws Exception {
|
||||
DataPoint dp = new DataPoint();
|
||||
dp.setName( name );
|
||||
dpService.add( dp );
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.hibernate.osgitest.command;
|
||||
|
||||
import org.apache.felix.gogo.commands.Action;
|
||||
import org.apache.felix.gogo.commands.Command;
|
||||
import org.apache.felix.service.command.CommandSession;
|
||||
import org.hibernate.osgitest.DataPointService;
|
||||
|
||||
@Command(scope = "dp", name = "deleteAll")
|
||||
public class DeleteAllCommand implements Action {
|
||||
private DataPointService dpService;
|
||||
|
||||
public void setDpService(DataPointService dpService) {
|
||||
this.dpService = dpService;
|
||||
}
|
||||
|
||||
public Object execute(CommandSession session) throws Exception {
|
||||
dpService.deleteAll();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.hibernate.osgitest.command;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.felix.gogo.commands.Action;
|
||||
import org.apache.felix.gogo.commands.Command;
|
||||
import org.apache.felix.service.command.CommandSession;
|
||||
import org.hibernate.osgitest.DataPointService;
|
||||
import org.hibernate.osgitest.entity.DataPoint;
|
||||
|
||||
@Command(scope = "dp", name = "getAll")
|
||||
public class GetAllCommand implements Action {
|
||||
private DataPointService dpService;
|
||||
|
||||
public void setDpService(DataPointService dpService) {
|
||||
this.dpService = dpService;
|
||||
}
|
||||
|
||||
public Object execute(CommandSession session) throws Exception {
|
||||
List<DataPoint> dps = dpService.getAll();
|
||||
for (DataPoint dp : dps) {
|
||||
System.out.println(dp.getId() + ", " + dp.getName());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.hibernate.osgitest.command;
|
||||
|
||||
import org.apache.felix.gogo.commands.Action;
|
||||
import org.apache.felix.gogo.commands.Argument;
|
||||
import org.apache.felix.gogo.commands.Command;
|
||||
import org.apache.felix.service.command.CommandSession;
|
||||
import org.hibernate.osgitest.DataPointService;
|
||||
import org.hibernate.osgitest.entity.DataPoint;
|
||||
|
||||
@Command(scope = "dp", name = "get")
|
||||
public class GetCommand implements Action {
|
||||
@Argument(index = 0, name = "Id", required = true, description = "Id", multiValued = false)
|
||||
String id;
|
||||
|
||||
private DataPointService dpService;
|
||||
|
||||
public void setDpService(DataPointService dpService) {
|
||||
this.dpService = dpService;
|
||||
}
|
||||
|
||||
public Object execute(CommandSession session) throws Exception {
|
||||
DataPoint dp = dpService.get( Long.valueOf( id ) );
|
||||
System.out.println( dp.getId() + ", " + dp.getName() );
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.hibernate.osgitest.command;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.felix.gogo.commands.Action;
|
||||
import org.apache.felix.gogo.commands.Argument;
|
||||
import org.apache.felix.gogo.commands.Command;
|
||||
import org.apache.felix.service.command.CommandSession;
|
||||
import org.hibernate.envers.DefaultRevisionEntity;
|
||||
import org.hibernate.osgitest.DataPointService;
|
||||
|
||||
@Command(scope = "dp", name = "getRevisions")
|
||||
public class GetRevisionsCommand implements Action {
|
||||
@Argument(index=0, name="Id", required=true, description="Id", multiValued=false)
|
||||
String id;
|
||||
|
||||
private DataPointService dpService;
|
||||
|
||||
public void setDpService(DataPointService dpService) {
|
||||
this.dpService = dpService;
|
||||
}
|
||||
|
||||
public Object execute(CommandSession session) throws Exception {
|
||||
Map<Number, DefaultRevisionEntity> revisions = dpService.getRevisions(Long.valueOf( id ));
|
||||
for (Number revisionNum : revisions.keySet()) {
|
||||
DefaultRevisionEntity dre = revisions.get( revisionNum );
|
||||
System.out.println(revisionNum + ": " + dre.getId() + ", " + dre.getTimestamp());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.hibernate.osgitest.command;
|
||||
|
||||
import org.apache.felix.gogo.commands.Action;
|
||||
import org.apache.felix.gogo.commands.Argument;
|
||||
import org.apache.felix.gogo.commands.Command;
|
||||
import org.apache.felix.service.command.CommandSession;
|
||||
import org.hibernate.osgitest.DataPointService;
|
||||
import org.hibernate.osgitest.entity.DataPoint;
|
||||
|
||||
@Command(scope = "dp", name = "load")
|
||||
public class LoadCommand implements Action {
|
||||
@Argument(index = 0, name = "Id", required = true, description = "Id", multiValued = false)
|
||||
String id;
|
||||
|
||||
private DataPointService dpService;
|
||||
|
||||
public void setDpService(DataPointService dpService) {
|
||||
this.dpService = dpService;
|
||||
}
|
||||
|
||||
public Object execute(CommandSession session) throws Exception {
|
||||
DataPoint dp = dpService.load( Long.valueOf( id ) );
|
||||
System.out.println( dp.getId() + ", " + dp.getName() );
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.hibernate.osgitest.command;
|
||||
|
||||
import org.apache.felix.gogo.commands.Action;
|
||||
import org.apache.felix.gogo.commands.Argument;
|
||||
import org.apache.felix.gogo.commands.Command;
|
||||
import org.apache.felix.service.command.CommandSession;
|
||||
import org.hibernate.osgitest.DataPointService;
|
||||
import org.hibernate.osgitest.entity.DataPoint;
|
||||
|
||||
@Command(scope = "dp", name = "update")
|
||||
public class UpdateCommand implements Action {
|
||||
@Argument(index=0, name="Id", required=true, description="Id", multiValued=false)
|
||||
String id;
|
||||
|
||||
@Argument(index=1, name="Name", required=true, description="Name", multiValued=false)
|
||||
String name;
|
||||
|
||||
private DataPointService dpService;
|
||||
|
||||
public void setDpService(DataPointService dpService) {
|
||||
this.dpService = dpService;
|
||||
}
|
||||
|
||||
public Object execute(CommandSession session) throws Exception {
|
||||
DataPoint dp = dpService.get( Long.valueOf( id ) );
|
||||
dp.setName( name );
|
||||
dpService.update( dp );
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
|
||||
* as indicated by the @authors tag. All rights reserved.
|
||||
* See the copyright.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License, v. 2.1.
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT A
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License,
|
||||
* v.2.1 along with this distribution; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package org.hibernate.osgitest.entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.hibernate.envers.Audited;
|
||||
|
||||
/**
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
@Entity
|
||||
@Audited
|
||||
public class DataPoint implements Serializable {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<blueprint default-activation="eager"
|
||||
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
|
||||
>
|
||||
|
||||
<bean id="dpService" class="org.hibernate.osgitest.DataPointServiceImpl"/>
|
||||
<service ref="dpService" interface="org.hibernate.osgitest.DataPointService"/>
|
||||
|
||||
<!-- This demonstrates how to register your custom implementations of Hibernate extension points. -->
|
||||
|
||||
<bean id="integrator" class="org.hibernate.osgitest.TestIntegrator"/>
|
||||
<service ref="integrator" interface="org.hibernate.integrator.spi.Integrator"/>
|
||||
|
||||
<bean id="strategyRegistrationProvider" class="org.hibernate.osgitest.TestStrategyRegistrationProvider"/>
|
||||
<service ref="strategyRegistrationProvider"
|
||||
interface="org.hibernate.boot.registry.selector.StrategyRegistrationProvider"/>
|
||||
|
||||
<bean id="typeContributor" class="org.hibernate.osgitest.TestTypeContributor"/>
|
||||
<service ref="typeContributor" interface="org.hibernate.boot.model.TypeContributor"/>
|
||||
|
||||
<!-- This bundle makes use of Karaf commands to demonstrate core persistence operations. Feel free to remove it. -->
|
||||
<command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
|
||||
<command>
|
||||
<action class="org.hibernate.osgitest.command.AddCommand">
|
||||
<property name="dpService" ref="dpService"/>
|
||||
</action>
|
||||
</command>
|
||||
<command>
|
||||
<action class="org.hibernate.osgitest.command.UpdateCommand">
|
||||
<property name="dpService" ref="dpService"/>
|
||||
</action>
|
||||
</command>
|
||||
<command>
|
||||
<action class="org.hibernate.osgitest.command.GetCommand">
|
||||
<property name="dpService" ref="dpService"/>
|
||||
</action>
|
||||
</command>
|
||||
<command>
|
||||
<action class="org.hibernate.osgitest.command.LoadCommand">
|
||||
<property name="dpService" ref="dpService"/>
|
||||
</action>
|
||||
</command>
|
||||
<command>
|
||||
<action class="org.hibernate.osgitest.command.GetAllCommand">
|
||||
<property name="dpService" ref="dpService"/>
|
||||
</action>
|
||||
</command>
|
||||
<command>
|
||||
<action class="org.hibernate.osgitest.command.GetRevisionsCommand">
|
||||
<property name="dpService" ref="dpService"/>
|
||||
</action>
|
||||
</command>
|
||||
<command>
|
||||
<action class="org.hibernate.osgitest.command.DeleteAllCommand">
|
||||
<property name="dpService" ref="dpService"/>
|
||||
</action>
|
||||
</command>
|
||||
</command-bundle>
|
||||
</blueprint>
|
|
@ -1,94 +0,0 @@
|
|||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<ehcache>
|
||||
|
||||
<!-- Sets the path to the directory where cache .data files are created.
|
||||
|
||||
If the path is a Java System Property it is replaced by
|
||||
its value in the running VM.
|
||||
|
||||
The following properties are translated:
|
||||
user.home - User's home directory
|
||||
user.dir - User's current working directory
|
||||
java.io.tmpdir - Default temp file path -->
|
||||
<diskStore path="./target/tmp"/>
|
||||
|
||||
|
||||
<!--Default Cache configuration. These will applied to caches programmatically created through
|
||||
the CacheManager.
|
||||
|
||||
The following attributes are required for defaultCache:
|
||||
|
||||
maxInMemory - Sets the maximum number of objects that will be created in memory
|
||||
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
|
||||
is never expired.
|
||||
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
|
||||
if the element is not eternal. Idle time is now - last accessed time
|
||||
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
|
||||
if the element is not eternal. TTL is now - creation time
|
||||
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
|
||||
has reached the maxInMemory limit.
|
||||
|
||||
-->
|
||||
<defaultCache
|
||||
maxElementsInMemory="10000"
|
||||
eternal="false"
|
||||
timeToIdleSeconds="120"
|
||||
timeToLiveSeconds="120"
|
||||
overflowToDisk="true"
|
||||
/>
|
||||
|
||||
<!--Predefined caches. Add your cache configuration settings here.
|
||||
If you do not have a configuration for your cache a WARNING will be issued when the
|
||||
CacheManager starts
|
||||
|
||||
The following attributes are required for defaultCache:
|
||||
|
||||
name - Sets the name of the cache. This is used to identify the cache. It must be unique.
|
||||
maxInMemory - Sets the maximum number of objects that will be created in memory
|
||||
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
|
||||
is never expired.
|
||||
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
|
||||
if the element is not eternal. Idle time is now - last accessed time
|
||||
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
|
||||
if the element is not eternal. TTL is now - creation time
|
||||
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
|
||||
has reached the maxInMemory limit.
|
||||
|
||||
-->
|
||||
|
||||
<!-- Sample cache named sampleCache1
|
||||
This cache contains a maximum in memory of 10000 elements, and will expire
|
||||
an element if it is idle for more than 5 minutes and lives for more than
|
||||
10 minutes.
|
||||
|
||||
If there are more than 10000 elements it will overflow to the
|
||||
disk cache, which in this configuration will go to wherever java.io.tmp is
|
||||
defined on your system. On a standard Linux system this will be /tmp"
|
||||
-->
|
||||
<cache name="sampleCache1"
|
||||
maxElementsInMemory="10000"
|
||||
eternal="false"
|
||||
timeToIdleSeconds="300"
|
||||
timeToLiveSeconds="600"
|
||||
overflowToDisk="true"
|
||||
/>
|
||||
|
||||
<!-- Sample cache named sampleCache2
|
||||
This cache contains 1000 elements. Elements will always be held in memory.
|
||||
They are not expired. -->
|
||||
<cache name="sampleCache2"
|
||||
maxElementsInMemory="1000"
|
||||
eternal="true"
|
||||
timeToIdleSeconds="0"
|
||||
timeToLiveSeconds="0"
|
||||
overflowToDisk="false"
|
||||
/> -->
|
||||
|
||||
<!-- Place configuration for your caches following -->
|
||||
|
||||
</ehcache>
|
|
@ -1,52 +0,0 @@
|
|||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<!DOCTYPE hibernate-configuration PUBLIC
|
||||
"-//Hibernate/Hibernate Configuration DTD//EN"
|
||||
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
|
||||
|
||||
<hibernate-configuration>
|
||||
<session-factory>
|
||||
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
|
||||
<property name="hibernate.connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE</property>
|
||||
<property name="hibernate.connection.username">sa</property>
|
||||
<property name="hibernate.connection.password"></property>
|
||||
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
|
||||
<property name="hibernate.hbm2ddl.auto">create-drop</property>
|
||||
|
||||
<!-- <property name="hibernate.connection.pool_size">5</property>
|
||||
<property name="hibernate.c3p0.min_size">50</property>
|
||||
<property name="hibernate.c3p0.max_size">800</property>
|
||||
<property name="hibernate.c3p0.max_statements">50</property>
|
||||
<property name="hibernate.jdbc.batch_size">10</property>
|
||||
<property name="hibernate.c3p0.timeout">300</property>
|
||||
<property name="hibernate.c3p0.idle_test_period">3000</property>
|
||||
<property name="hibernate.c3p0.testConnectionOnCheckout">true</property> -->
|
||||
|
||||
<!-- <property name="hibernate.connection.pool_size">5</property>
|
||||
<property name="hibernate.jdbc.batch_size">10</property>
|
||||
<property name="hibernate.connection.provider_class">proxool</property>
|
||||
<property name="hibernate.proxool.properties">pool-one.properties</property>
|
||||
<property name="hibernate.proxool.pool_alias">pool-one</property> -->
|
||||
|
||||
<!-- <property name="hibernate.cache.region_prefix">hibernate.test</property>
|
||||
<property name="cache.use_query_cache">true</property>
|
||||
<property name="cache.use_second_level_cache">true</property>
|
||||
<property name="cache.use_structured_entries">true</property>
|
||||
<property name="cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>
|
||||
<property name="net.sf.ehcache.configurationResourceName">file:///[PATH]/unmanaged-jpa/src/main/resources/ehcache.xml</property> -->
|
||||
|
||||
<!-- <property name="hibernate.cache.region_prefix">hibernate.test</property>
|
||||
<property name="cache.use_query_cache">true</property>
|
||||
<property name="cache.use_second_level_cache">true</property>
|
||||
<property name="cache.use_structured_entries">true</property>
|
||||
<property name="cache.region.factory_class">org.hibernate.cache.infinispan.InfinispanRegionFactory</property> -->
|
||||
|
||||
<mapping class="org.hibernate.osgitest.entity.DataPoint"/>
|
||||
</session-factory>
|
||||
|
||||
</hibernate-configuration>
|
|
@ -1,13 +0,0 @@
|
|||
#
|
||||
# Hibernate, Relational Persistence for Idiomatic Java
|
||||
#
|
||||
# License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
# See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
#
|
||||
jdbc-0.proxool.alias=pool-one
|
||||
jdbc-0.proxool.driver-url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE
|
||||
jdbc-0.proxool.driver-class=org.h2.Driver
|
||||
jdbc-0.user=sa
|
||||
jdbc-0.password=
|
||||
jdbc-0.proxool.maximum-connection-count=2
|
||||
jdbc-0.proxool.house-keeping-test-sql=select CURRENT_DATE
|
Loading…
Reference in New Issue