HHH-6225 Add support to find entities by date in one single query.

This commit is contained in:
Naros 2016-01-03 01:23:52 -06:00 committed by Brett Meyer
parent abace003e6
commit 6e31e61f1a
3 changed files with 46 additions and 5 deletions

View File

@ -42,6 +42,27 @@ public interface AuditReader {
<T> T find(Class<T> cls, Object primaryKey, Number revision) throws
IllegalArgumentException, NotAuditedException, IllegalStateException;
/**
* Find an entity by primary key on the given date. The date specifies restricting
* the result to any entity created on or before the date with the highest revision
* number.
*
* @param cls Class of the entity.
* @param primaryKey Primary key of the entity.
* @param date Date for which to get entity revision.
*
* @return The found entity instance at created on or before the specified date with the highest
* revision number or null, if an entity with the id had not be created on or before the
* specified date.
*
* @throws IllegalArgumentException if cls, primaryKey, or date is null.
* @throws NotAuditedException When entities of the given class are not audited.
* @throws RevisionDoesNotExistException If the given date is before the first revision.
* @throws IllegalStateException If the associated entity manager is closed.
*/
<T> T find(Class<T> cls, Object primaryKey, Date date) throws
IllegalArgumentException, NotAuditedException, RevisionDoesNotExistException, IllegalStateException;
/**
* Find an entity by primary key at the given revision with the specified entityName.
*

View File

@ -6,12 +6,17 @@
*/
package org.hibernate.envers.internal.reader;
import static org.hibernate.envers.internal.tools.ArgumentsTools.checkNotNull;
import static org.hibernate.envers.internal.tools.ArgumentsTools.checkPositive;
import static org.hibernate.envers.internal.tools.EntityTools.getTargetClassIfProxied;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.NoResultException;
import org.hibernate.Criteria;
@ -30,10 +35,6 @@ import org.hibernate.envers.query.AuditQueryCreator;
import org.hibernate.event.spi.EventSource;
import org.hibernate.proxy.HibernateProxy;
import static org.hibernate.envers.internal.tools.ArgumentsTools.checkNotNull;
import static org.hibernate.envers.internal.tools.ArgumentsTools.checkPositive;
import static org.hibernate.envers.internal.tools.EntityTools.getTargetClassIfProxied;
/**
* @author Adam Warski (adam at warski dot org)
* @author Hern&aacute;n Chanfreau
@ -139,6 +140,12 @@ public class AuditReaderImpl implements AuditReaderImplementor {
return this.getRevisions( cls, cls.getName(), primaryKey );
}
@Override
public <T> T find(Class<T> cls, Object primaryKey, Date date)
throws IllegalArgumentException, NotAuditedException, RevisionDoesNotExistException, IllegalStateException {
return find(cls, primaryKey, getRevisionNumberForDate(date));
}
@Override
@SuppressWarnings({"unchecked"})
public List<Number> getRevisions(Class<?> cls, String entityName, Object primaryKey)

View File

@ -25,6 +25,7 @@ public class RevisionForDate extends BaseEnversJPAFunctionalTestCase {
private long timestamp2;
private long timestamp3;
private long timestamp4;
private Integer id;
@Override
protected Class<?>[] getAnnotatedClasses() {
@ -43,7 +44,7 @@ public class RevisionForDate extends BaseEnversJPAFunctionalTestCase {
em.getTransaction().begin();
StrTestEntity rfd = new StrTestEntity( "x" );
em.persist( rfd );
Integer id = rfd.getId();
id = rfd.getId();
em.getTransaction().commit();
timestamp2 = System.currentTimeMillis();
@ -73,6 +74,11 @@ public class RevisionForDate extends BaseEnversJPAFunctionalTestCase {
public void testTimestamps1() {
getAuditReader().getRevisionNumberForDate( new Date( timestamp1 ) );
}
@Test(expected = RevisionDoesNotExistException.class)
public void testTimestampslWithFind() {
getAuditReader().find( StrTestEntity.class, id, new Date( timestamp1 ) );
}
@Test
public void testTimestamps() {
@ -80,6 +86,13 @@ public class RevisionForDate extends BaseEnversJPAFunctionalTestCase {
assert getAuditReader().getRevisionNumberForDate( new Date( timestamp3 ) ).intValue() == 2;
assert getAuditReader().getRevisionNumberForDate( new Date( timestamp4 ) ).intValue() == 3;
}
@Test
public void testEntitiesForTimestamps() {
assert "x".equals( getAuditReader().find( StrTestEntity.class, id, new Date( timestamp2 ) ).getStr() );
assert "y".equals( getAuditReader().find( StrTestEntity.class, id, new Date( timestamp3 ) ).getStr() );
assert "z".equals( getAuditReader().find( StrTestEntity.class, id, new Date( timestamp4 ) ).getStr() );
}
@Test
public void testDatesForRevisions() {