Example usage for org.hibernate.envers.query AuditEntity id

List of usage examples for org.hibernate.envers.query AuditEntity id

Introduction

In this page you can find the example usage for org.hibernate.envers.query AuditEntity id.

Prototype

public static AuditId id() 

Source Link

Usage

From source file:org.jboss.pressgang.ccms.model.contentspec.CSNodeToPropertyTag.java

License:Open Source License

@Override
protected boolean testUnique(final EntityManager entityManager, final Number revision) {
    if (propertyTag.getPropertyTagIsUnique()) {
        /*/*from  ww w  . ja va 2 s  .c  om*/
         * Since having to iterate over thousands of entities is slow, use a HQL query to find the count for us.
         */
        final Long count;
        if (revision == null) {
            final String query = CSNodeToPropertyTag.SELECT_SIZE_QUERY
                    + " WHERE csNodeToPropertyTag.propertyTag = :propertyTagId AND"
                    + " csNodeToPropertyTag.value = :value";
            final Query entityQuery = entityManager.createQuery(query);
            entityQuery.setParameter("value", getValue());
            entityQuery.setParameter("propertyTagId", getPropertyTag().getId());
            count = (Long) entityQuery.getSingleResult();
        } else {
            final AuditReader reader = AuditReaderFactory.get(entityManager);
            final AuditQueryCreator queryCreator = reader.createQuery();
            final AuditQuery query = queryCreator.forEntitiesAtRevision(CSNodeToPropertyTag.class, revision)
                    .addProjection(AuditEntity.id().count("csNodeToPropertyTagId"))
                    .add(AuditEntity.relatedId("propertyTag").eq(getPropertyTag().getId()))
                    .add(AuditEntity.property("value").eq(getValue()));
            query.setCacheable(true);
            count = (Long) query.getSingleResult();
        }

        if (count > 1)
            return false;
    }

    return true;
}

From source file:org.jboss.pressgang.ccms.model.contentspec.TranslatedContentSpec.java

License:Open Source License

@Transient
public ContentSpec getEnversContentSpec(final EntityManager entityManager) {
    if (enversContentSpec == null) {
        /* Find the envers contentSpec */
        final AuditReader reader = AuditReaderFactory.get(entityManager);
        final AuditQuery query = reader.createQuery()
                .forEntitiesAtRevision(ContentSpec.class, contentSpecRevision)
                .add(AuditEntity.id().eq(contentSpecId));
        enversContentSpec = (ContentSpec) query.getSingleResult();
    }//from ww  w .ja  va2  s .co  m
    return enversContentSpec;
}

From source file:org.jboss.pressgang.ccms.model.contentspec.TranslatedCSNode.java

License:Open Source License

@Transient
public CSNode getEnversCSNode(final EntityManager entityManager) {
    if (enversCSNode == null) {
        /* Find the envers topic */
        final AuditReader reader = AuditReaderFactory.get(entityManager);
        final AuditQuery query = reader.createQuery()
                .forEntitiesAtRevision(CSNode.class, contentSpecNodeRevision)
                .add(AuditEntity.id().eq(contentSpecNodeId));
        enversCSNode = (CSNode) query.getSingleResult();
    }//from ww w. java 2 s  .  c  om
    return enversCSNode;
}

From source file:org.jboss.pressgang.ccms.model.TagToPropertyTag.java

License:Open Source License

@Override
protected boolean testUnique(final EntityManager entityManager, final Number revision) {
    if (propertyTag.getPropertyTagIsUnique()) {
        /*/*from  ww w. j  av  a  2 s.c  o  m*/
         * Since having to iterate over thousands of entities is slow, use a HQL query for the latest versions, for
         * revisions though we still have to do it the slow way since we don't know the revision number.
         */
        final Long count;
        if (revision == null) {
            final String query = TagToPropertyTag.SELECT_SIZE_QUERY
                    + " WHERE tagToPropertyTag.propertyTag.propertyTagId = "
                    + ":propertyTagId AND tagToPropertyTag.value = :value";
            final Query entityQuery = entityManager.createQuery(query);
            entityQuery.setParameter("value", getValue());
            entityQuery.setParameter("propertyTagId", getPropertyTag().getId());
            count = (Long) entityQuery.getSingleResult();
        } else {
            final AuditReader reader = AuditReaderFactory.get(entityManager);
            final AuditQueryCreator queryCreator = reader.createQuery();
            final AuditQuery query = queryCreator.forEntitiesAtRevision(TagToPropertyTag.class, revision)
                    .addProjection(AuditEntity.id().count("tagToPropertyTagID"))
                    .add(AuditEntity.relatedId("propertyTag").eq(propertyTag.getId()))
                    .add(AuditEntity.property("value").eq(getValue()));
            count = (Long) query.getSingleResult();
        }

        if (count > 1)
            return false;
    }

    return true;
}

From source file:org.jboss.pressgang.ccms.model.TopicToPropertyTag.java

License:Open Source License

@Override
protected boolean testUnique(final EntityManager entityManager, final Number revision) {
    if (propertyTag.getPropertyTagIsUnique()) {
        /*/*from  w  w  w  .  j  a v a 2s.co  m*/
         * Since having to iterate over thousands of entities is slow, use a HQL query to find the count for us.
         */
        final Long count;
        if (revision == null) {
            final String query = TopicToPropertyTag.SELECT_SIZE_QUERY
                    + " WHERE topicToPropertyTag.propertyTag.propertyTagId = "
                    + ":propertyTagId AND topicToPropertyTag.value = :value";
            final Query entityQuery = entityManager.createQuery(query);
            entityQuery.setParameter("value", getValue());
            entityQuery.setParameter("propertyTagId", getPropertyTag().getId());
            count = (Long) entityQuery.getSingleResult();
        } else {
            final AuditReader reader = AuditReaderFactory.get(entityManager);
            final AuditQueryCreator queryCreator = reader.createQuery();
            final AuditQuery query = queryCreator.forEntitiesAtRevision(TopicToPropertyTag.class, revision)
                    .addProjection(AuditEntity.id().count("topicToPropertyTagID"))
                    .add(AuditEntity.relatedId("propertyTag").eq(getPropertyTag().getId()))
                    .add(AuditEntity.property("value").eq(getValue()));
            query.setCacheable(true);
            count = (Long) query.getSingleResult();
        }

        if (count > 1)
            return false;
    }

    return true;
}

From source file:org.jboss.pressgang.ccms.model.TranslatedTopic.java

License:Open Source License

@Transient
public Topic getEnversTopic(final EntityManager entityManager) {
    if (enversTopic == null) {
        /* Find the envers topic */
        final AuditReader reader = AuditReaderFactory.get(entityManager);
        final AuditQuery query = reader.createQuery().forEntitiesAtRevision(Topic.class, topicRevision)
                .add(AuditEntity.id().eq(topicId));
        enversTopic = (Topic) query.getSingleResult();
    }/*  www .  j  av a  2 s. c om*/
    return enversTopic;
}

From source file:org.jboss.pressgang.ccms.model.utils.EnversUtilities.java

License:Open Source License

public static <T extends AuditedEntity> Number getClosestRevision(final AuditReader reader,
        final Class<T> entityClass, final Integer id, final Number revision) {
    // Find the closest revision that is less than or equal to the revision specified.
    final Number closestRevision = (Number) reader.createQuery().forRevisionsOfEntity(entityClass, false, true)
            .addProjection(AuditEntity.revisionNumber().max()).add(AuditEntity.id().eq(id))
            .add(AuditEntity.revisionNumber().le(revision)).getSingleResult();
    return closestRevision;
}

From source file:org.tomitribe.tribestream.registryng.repository.Repository.java

License:Apache License

public <T> List<HistoryEntry<T>> getRevisions(final Class<T> entityClass, final String id, final int first,
        final int pageSize) throws NoResultException {
    AuditReader auditReader = AuditReaderFactory.get(em);
    AuditQuery query = auditReader.createQuery().forRevisionsOfEntity(entityClass, false, true);
    query.add(AuditEntity.id().eq(id));
    query.addOrder(AuditEntity.revisionNumber().desc());
    query.setFirstResult(first).setMaxResults(pageSize);

    List<Object[]> objects = query.getResultList();
    return objects.stream().map(HistoryEntry<T>::new).collect(toList());
}

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/*from  w  w w  . ja  v  a  2  s . co  m*/
 */
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();
}