List of usage examples for org.hibernate.envers AuditReaderFactory get
public static AuditReader get(EntityManager entityManager) throws AuditException
From source file:org.tomitribe.tribestream.registryng.repository.Repository.java
License:Apache License
/** * Returns the number of revisions available for the entity with the given id. * * @param entityClass the Entity class type to use to find revisions * @param id the ID of the entity * @return the number of revisions/* w w w . jav a 2s. c om*/ */ public <T> int getNumberOfRevisions(final Class<T> entityClass, final String id) { final AuditQuery query = AuditReaderFactory.get(em).createQuery().forRevisionsOfEntity(entityClass, true, true); query.add(AuditEntity.id().eq(id)); query.addProjection(AuditEntity.revisionNumber().count()); return ((Number) query.getSingleResult()).intValue(); }
From source file:org.tomitribe.tribestream.registryng.repository.Repository.java
License:Apache License
public Endpoint findEndpointByIdAndRevision(final String endpointId, final int revision) { final AuditReader auditReader = AuditReaderFactory.get(em); final Endpoint endpoint = auditReader.find(Endpoint.class, endpointId, revision); // Resolve the application here, because the caller will not be able to, as it will // be running in a different transaction endpoint.getApplication().getId();/*from w ww.java2 s.co m*/ return endpoint; }
From source file:org.tomitribe.tribestream.registryng.resources.EndpointHistoryResourceTest.java
License:Apache License
@Test public void loadRevisionHasPayload() { // Given: A random application final EndpointSearchResult searchResult = registry.withRetries(() -> { List<EndpointSearchResult> searchResults = getSearchPage().getResults().stream() .map(SearchResult::getEndpoints).flatMap(List::stream).collect(toList()); final EndpointSearchResult result = searchResults.get(random.nextInt(searchResults.size())); assertNotNull("endpoint exists", em.find(Endpoint.class, result.getEndpointId())); return result; });// www . j a va 2 s.co m final String applicationId = searchResult.getApplicationId(); final String endpointId = searchResult.getEndpointId(); final AuditReader auditReader = AuditReaderFactory.get(em); final Number revision = auditReader.getRevisions(Endpoint.class, endpointId).iterator().next(); final String json = registry.target() .path("api/history/application/{applicationId}/endpoint/{endpointId}/{revision}") .resolveTemplate("applicationId", applicationId).resolveTemplate("endpointId", endpointId) .resolveTemplate("revision", revision.intValue()).request(MediaType.APPLICATION_JSON_TYPE) .get(EndpointWrapper.class).getJson(); assertNotNull(json); assertFalse(json.trim().isEmpty()); try { // valid it is json new ObjectMapper().readTree(json); } catch (final IOException e) { fail(e.getMessage()); } }
From source file:pl.softwaremill.enversdemo.working.WorkingDemo.java
License:Open Source License
public void doRun() { em.getTransaction().begin();// ww w . j a va2 s . c o m WorkingAddress a1 = new WorkingAddress("street1"); em.persist(a1); WorkingAddress a2 = new WorkingAddress("street2"); em.persist(a2); WorkingPerson p1 = new WorkingPerson("john", "doe", 10); p1.setAddress(a1); em.persist(p1); em.getTransaction().commit(); // --- em.getTransaction().begin(); p1 = em.find(WorkingPerson.class, p1.getId()); p1.setAge(11); WorkingPerson p2 = new WorkingPerson("mary", "kowalski", 99); p2.setAddress(a1); em.persist(p2); em.getTransaction().commit(); // -- em.getTransaction().begin(); p1 = em.find(WorkingPerson.class, p1.getId()); p1.setAddress(a2); p1.setName("peter"); p2 = em.find(WorkingPerson.class, p2.getId()); p2.setAge(35); em.getTransaction().commit(); // -- AuditReader ar = AuditReaderFactory.get(em); WorkingPerson p1_rev1 = ar.find(WorkingPerson.class, p1.getId(), 1); assertEquals(p1_rev1.getAge(), 10); WorkingPerson p2_rev3 = ar.find(WorkingPerson.class, p2.getId(), 3); assertEquals(p2_rev3.getAge(), 35); // -- assertEquals(ar.getRevisions(WorkingPerson.class, p2.getId()), Arrays.asList(2, 3)); // -- assertEquals(ar.find(WorkingAddress.class, a1.getId(), 2).getPersons().size(), 2); }
From source file:py.una.pol.karaku.replication.server.EnversReplicationProvider.java
License:Open Source License
/** * @param clazz/*from w ww. j a v a 2 s.c om*/ * @param lastId * @return */ @SuppressWarnings("unchecked") private <T extends Shareable> Bundle<T> getDelta(Class<T> clazz, String lastId) { AuditReader ar = AuditReaderFactory.get(getSession()); Number number = getLastId(clazz, lastId); List<Object[]> entities = ar.createQuery().forRevisionsOfEntity(clazz, false, false) .add(AuditEntity.revisionNumber().gt(number)).getResultList(); Bundle<T> bundle = new Bundle<T>(lastId); for (Object[] o : entities) { if (o == null) { continue; } bundle.add((T) notNull(o[0]), notNull(getId(o[1]))); } return bundle; }
From source file:py.una.pol.karaku.replication.server.EnversReplicationProvider.java
License:Open Source License
/** * @param clazz//from ww w . j a v a 2s. com * @param reader * @param lastId * @return */ @SuppressWarnings("unchecked") private <T extends Shareable> boolean isUnknown(Class<T> clazz, String lastId) { if (Bundle.FIRST_CHANGE.equals(lastId)) { return false; } AuditReader reader = AuditReaderFactory.get(getSession()); List<T> entitiesAtRevision = reader.createQuery().forRevisionsOfEntity(clazz, false, false) .add(AuditEntity.revisionNumber().eq(getLastId(clazz, lastId))).getResultList(); return (entitiesAtRevision == null) || entitiesAtRevision.isEmpty(); }
From source file:py.una.pol.karaku.replication.server.EnversReplicationProvider.java
License:Open Source License
@Nonnull private <T extends Shareable> Bundle<T> getAll(@Nonnull Class<T> clazz) { AuditReader reader = AuditReaderFactory.get(getSession()); Number prior = (Number) reader.createQuery().forRevisionsOfEntity(clazz, false, true) .addProjection(AuditEntity.revisionNumber().max()).getSingleResult(); String lastId;/*from w w w . ja v a 2 s. c o m*/ // previous revision, la actual no ser persistida. if (prior == null) { lastId = Bundle.FIRST_CHANGE; } else { lastId = String.valueOf(prior); } return firstChangeProviderHandler.getAll(clazz, notNull(lastId)); }
From source file:sample.data.jpa.SampleDataJpaApplicationTests.java
License:Apache License
@Test public void testEnvers() { FooService fooService = context.getBean(FooService.class); Foo foo = new Foo(); foo.setDescription("Foo"); Foo saved = fooService.save(foo);//w w w.ja va 2 s. c o m saved.setDescription("Foo1"); saved = fooService.save(saved); AuditReader auditReader = AuditReaderFactory.get(emf.createEntityManager()); Foo oldFoo = (Foo) auditReader.createQuery().forEntitiesAtRevision(Foo.class, new Integer(1)) .getSingleResult(); Foo newFoo = fooService.load(1); Assert.assertEquals("Foo", oldFoo.getDescription()); Assert.assertEquals("Foo1", newFoo.getDescription()); }
From source file:sample.data.jpa.service.CityRepositoryIntegrationTests.java
License:Apache License
private void checkRevisions(long cityId) { AuditReader reader = AuditReaderFactory.get(entityManager); City city_rev1 = reader.find(City.class, cityId, 1); assertThat(city_rev1.getState(), is(WRONG_STATE)); City city_rev2 = reader.find(City.class, cityId, 2); assertThat(city_rev2.getState(), is(OXFORDSHIRE_STATE)); }
From source file:sample.data.jpa.service.CityRepositoryIntegrationTests.java
License:Apache License
private void checkUsers() { AuditReader reader = AuditReaderFactory.get(entityManager); AuditQuery query = reader.createQuery().forRevisionsOfEntity(City.class, false, false); //This return a list of array triplets of changes concerning the specified revision. // The array triplet contains the entity, entity revision information and at last the revision type. Object[] obj = (Object[]) query.getSingleResult(); //In this case we want the entity revision information object, which is the second object of the array. UserRevEntity userRevEntity = (UserRevEntity) obj[1]; String user = userRevEntity.getUsername(); assertThat(user, is(UserRevisionListener.USERNAME)); }