-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate criteria implementation for this HQL (self join)
PostPosted: Thu Jul 24, 2014 7:49 am 
Newbie

Joined: Tue Mar 18, 2014 10:01 am
Posts: 4
Given the following entity one-to-many model:

One Repository can be linked to many AuditRecords.

Many AuditRecords can all link to the same Repository

Code:
    @Entity
    class AuditRecordEntity {
      private AuditRepositoryEntity auditRepository; 
   
      @ManyToOne(cascade = CascadeType.ALL)
      @JoinColumn(name = AUDIT_REPOSITORY_DB_COLUMN_NAME, nullable = false, updatable = false)
      public AuditRepositoryEntity getAuditRepository() {
        return auditRepository;
      }
      ...
    }


Code:
   @Entity
    class AuditRepositoryEntity {
      private List<AuditRecordEntity> auditRecords = new ArrayList<AuditRecordEntity>();
   
      @OneToMany(mappedBy = "auditRepository")
      public List<AuditRecordEntity> getAuditRecords() {
        return auditRecords;
      }
      ...
    }


Minor correction, in ERD diagram below, for 'repositoryId', read 'auditRepository'

Image

I am trying to get the Criteria API implementation to:

Get the latest (by accessTime) AuditRecord for each distinct Repository? I.e. a list of AuditRecords, one for each Repository, where the AuditRecord is the last AuditRecord for that Repository (in the case where a Repository has many AuditRecords).

I have the HQL query to do this:

Code:
select auditRecord from AuditRecordEntity auditRecord where auditRecord.accessTime =
(select max(auditRecord2.accessTime) from AuditRecordEntity auditRecord2 where  auditRecord2.auditRepository = auditRecord.auditRepository)


But need to use the Criteria API instead, any help is much appreciated!:

CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Object> query = builder.createQuery();
Root<AuditRecordEntity> root = query.from(AuditRecordEntity.class);
// what next?


Top
 Profile  
 
 Post subject: Re: Hibernate criteria implementation for this HQL (self join)
PostPosted: Thu Jul 24, 2014 10:20 am 
Newbie

Joined: Tue Mar 18, 2014 10:01 am
Posts: 4
I have got this to work(around) by using the output from the HQL query as input to the criteria API:

Code:
    final List<UUID> auditRecordIds = execute("select auditRecord from AuditRecordEntity auditRecord where auditRecord.accessTime =
    (select max(auditRecord2.accessTime) from AuditRecordEntity auditRecord2 where 
    auditRecord2.auditRepository = auditRecord.auditRepository)")
   
    Root<AuditRecordEntity> root = criteriaQuery.from(AuditRecordEntity.class);
    criteriaQuery.select(root);
   
    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(root.get("id").in(auditRecordIds.toArray()));
    entitySearchCriteria.addPredicates(predicates);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.