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

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

Introduction

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

Prototype

public static AuditRelatedId relatedId(String propertyName) 

Source Link

Document

Create restrictions on an id of a related entity.

Usage

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 w w w  . j ava 2s  . com*/
         * 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 ava 2  s  .  com
         * 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;
}