Skip to content

Commit

Permalink
Initial draft of Envers support for Spring Data JPA.
Browse files Browse the repository at this point in the history
Added an implementation of the Spring Data Commons historiography API based on Hibernate Envers. The implementation is a contribution of @hygl to a large degree.
  • Loading branch information
odrotbohm committed Mar 21, 2012
0 parents commit 5cf6501
Show file tree
Hide file tree
Showing 19 changed files with 1,410 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
.classpath
.project
.springBeans
.settings/
target/
291 changes: 291 additions & 0 deletions etc/eclipse-formatter.xml

Large diffs are not rendered by default.

140 changes: 140 additions & 0 deletions pom.xml
@@ -0,0 +1,140 @@
<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.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
<version>0.1.0.BUILD-SNAPSHOT</version>

<properties>
<spring.version>3.1.1.RELEASE</spring.version>
<spring.data.jpa.version>1.1.0.BUILD-SNAPSHOT</spring.data.jpa.version>
<file.encoding>UTF-8</file.encoding>
</properties>

<repositories>
<repository>
<id>spring-releases</id>
<url>http://repo.springsource.org/libs-release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>spring-plugins</id>
<url>http://repo.springsource.org/libs-plugin</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

<developers>
<developer>
<name>Oliver Gierke</name>
<email>ogierke@vmware.com</email>
<organization>SpringSource, a division of VMware</organization>
<organizationUrl>www.springsource.org</organizationUrl>
</developer>
<developer>
<name>Philip Huegelmeyer</name>
<email>philip.huegelmeyer@ble.de</email>
<organization>BLE</organization>
<organizationUrl>www.ble.de</organizationUrl>
</developer>
</developers>

<scm>
<url>https://github.com/SpringSource/spring-data-envers</url>
</scm>

<dependencies>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring.data.jpa.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- Logging -->

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.6.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.157</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
37 changes: 37 additions & 0 deletions readme.md
@@ -0,0 +1,37 @@
# Spring Data Envers #

This project is an extension of the [Spring Data JPA](http://github.com/SpringSource/spring-data-jpa) project to allow access to entity revisions managed by Hibernate Envers. The sources mostly originate from a contribution of Philip Hügelmeyer [@hygl](https://github.com/hygl).

The core feature of the module consists of an implementation of the `RevisionRepository` of Spring Data Commons.

```java
public interface RevisionRepository<T, ID extends Serializable, N extends Number & Comparable<N>> {

Revision<N, T> findLastChangeRevision(ID id);

Revisions<N, T> findRevisions(ID id);

Page<Revision<N, T>> findRevisions(ID id, Pageable pageable);
}
```

You can pull in this functionality to your repositories by simply additionally extending the interface just mentioned:


```java
interface PersonRepository extend RevisionRepository<Person, Long, Long>, CrudRepository<Person, Long> {

// Your query methods go here
}
```

To successfully activate the Spring Data Envers repository factory use the Spring Data JPA repositories namespace element's `factory-class` attribute:

```xml
<jpa:repositories base-package="com.acme.repositories"
factory-class="….EnversRevisionRepositoryFactoryBean" />
```

# Contributing to the project

If you're an Eclipse user make sure you activate automatic application of the formatter (located at `etc/eclipse-formatter.xml`) and activate automatic formatting and organizing imports on save.
@@ -0,0 +1,51 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed 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.springframework.data.envers.repository.support;

import org.hibernate.envers.DefaultRevisionEntity;
import org.springframework.data.repository.history.support.RevisionEntityInformation;

/**
* {@link RevisionEntityInformation} for {@link DefaultRevisionEntity}.
*
* @author Oliver Gierke
*/
class DefaultRevisionEntityInformation implements RevisionEntityInformation {

/*
* (non-Javadoc)
* @see org.springframework.data.repository.history.support.RevisionEntityInformation#getRevisionNumberType()
*/
public Class<?> getRevisionNumberType() {
return Integer.class;
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.history.support.RevisionEntityInformation#isDefaultRevisionEntity()
*/
public boolean isDefaultRevisionEntity() {
return true;
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.history.support.RevisionEntityInformation#getRevisionEntityClass()
*/
public Class<?> getRevisionEntityClass() {
return DefaultRevisionEntity.class;
}
}
@@ -0,0 +1,68 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed 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.springframework.data.envers.repository.support;

import org.hibernate.envers.DefaultRevisionEntity;
import org.joda.time.DateTime;
import org.springframework.data.history.RevisionMetadata;
import org.springframework.util.Assert;

/**
* {@link RevisionMetadata} working with a {@link DefaultRevisionEntity}.
*
* @author Oliver Gierke
* @author Philip Huegelmeyer
*/
public class DefaultRevisionMetadata implements RevisionMetadata<Integer> {

private final DefaultRevisionEntity entity;

/**
* Creates a new {@link DefaultRevisionMetadata}.
*
* @param entity must not be {@literal null}.
*/
public DefaultRevisionMetadata(DefaultRevisionEntity entity) {

Assert.notNull(entity);
this.entity = entity;
}

/*
* (non-Javadoc)
* @see org.springframework.data.history.RevisionMetadata#getRevisionNumber()
*/
public Integer getRevisionNumber() {
return entity.getId();
}

/*
* (non-Javadoc)
* @see org.springframework.data.history.RevisionMetadata#getRevisionDate()
*/
public DateTime getRevisionDate() {
return new DateTime(entity.getTimestamp());
}

/*
* (non-Javadoc)
* @see org.springframework.data.history.RevisionMetadata#getDelegate()
*/
@SuppressWarnings("unchecked")
public <T> T getDelegate() {
return (T) entity;
}
}
@@ -0,0 +1,34 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed 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.springframework.data.envers.repository.support;

import java.io.Serializable;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.history.RevisionRepository;

/**
* Convenience interface to allow pulling in {@link JpaRepository} and {@link RevisionRepository} functionality in one
* go.
*
* @author Oliver Gierke
*/
@NoRepositoryBean
public interface EnversRevisionRepository<T, ID extends Serializable, N extends Number & Comparable<N>> extends
RevisionRepository<T, ID, N>, JpaRepository<T, ID> {

}

0 comments on commit 5cf6501

Please sign in to comment.