1 Reply Latest reply on Dec 9, 2010 4:39 PM by adamw

    Trouble with AuditEntity.property

    hoss1968

      Hi there,

       

      I have a little trouble with accessing an Embedded attribute with AuditEntity.property in a AuditQuery. The simple question is how it is done?

       

      I have following class as Embeddable

      @Embeddable
      public class AuditAttributesEmbedded implements java.io.Serializable {
      
         private static final long serialVersionUID = 3703981250807089499L;
      
         @Basic(optional = false)
         @Column(name = "VALID_FROM")
         @Temporal(TemporalType.DATE)
         private Date validFrom;
      
         @Basic(optional = false)
         @Column(name = "VALID_TO")
         @Temporal(TemporalType.DATE)
         private Date validTo;
         
         @Basic(optional = false)
         @Column(name = "CREATED_AT")
         @Temporal(TemporalType.DATE)
         private Date createdAt;
         
         @Basic
         @Column(name = "MODIFIED_AT")
         @Temporal(TemporalType.DATE)
         private Date modifiedAt;
         
         @Basic(optional = false)
         @Column(name = "USER")
         private String user;
      
         /**
          * Default constructor for Serialization.
          */
         public AuditAttributesEmbedded() {
      
         }
      
         /**
          * @return the validFrom
          */
         public Date getValidFrom() {
         
            return validFrom;
         }
      
         /**
          * @param validFrom the validFrom to set
          */
         public void setValidFrom(Date validFrom) {
         
            this.validFrom = validFrom;
         }
      
         /**
          * @return the validTo
          */
         public Date getValidTo() {
         
            return validTo;
         }
      
         /**
          * @param validTo the validTo to set
          */
         public void setValidTo(Date validTo) {
         
            this.validTo = validTo;
         }
      
         /**
          * @return the createdAt
          */
         public Date getCreatedAt() {
         
            return createdAt;
         }
      
         /**
          * @param createdAt the createdAt to set
          */
         public void setCreatedAt(Date createdAt) {
         
            this.createdAt = createdAt;
         }
      
         /**
          * @return the modifiedAt
          */
         public Date getModifiedAt() {
         
            return modifiedAt;
         }
      
         /**
          * @param modifiedAt the modifiedAt to set
          */
         public void setModifiedAt(Date modifiedAt) {
         
            this.modifiedAt = modifiedAt;
         }
      
         /**
          * @return the user
          */
         public String getUser() {
         
            return user;
         }
      
         /**
          * @param user the user to set
          */
         public void setUser(String user) {
         
            this.user = user;
         }
      
         /**
          * Undocumented constructor.
          */
         public AuditAttributesEmbedded(Date validFrom, Date validTo, Date createdAt, Date modifiedAt, String user) {
      
            super();
            this.validFrom = validFrom;
            this.validTo = validTo;
            this.createdAt = createdAt;
            this.modifiedAt = modifiedAt;
            this.user = user;
         }
      
         /* (non-Javadoc)
          * @see java.lang.Object#toString()
          */
         @Override
         public String toString() {
      
            return "AuditAttributesEmbedded [createdAt=" + createdAt + ", modifiedAt=" + modifiedAt
                  + ", user=" + user + ", validFrom=" + validFrom + ", validTo=" + validTo + "]";
         }
      }
      

       

       

      And this is the entity where the Embedded class is used:

       

      package com.corfja.ccp.server.persistence.reloaded.entities.agent;
      
      import static javax.persistence.CascadeType.ALL;
      
      import java.util.Collection;
      
      import javax.persistence.AttributeOverride;
      import javax.persistence.AttributeOverrides;
      import javax.persistence.Basic;
      import javax.persistence.Column;
      import javax.persistence.Embedded;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.NamedQueries;
      import javax.persistence.NamedQuery;
      import javax.persistence.OneToMany;
      import javax.persistence.SequenceGenerator;
      import javax.persistence.Table;
      
      import org.hibernate.envers.Audited;
      
      import com.corfja.ccp.server.persistence.reloaded.entities.attribute.AgentAttributeEntity;
      import com.corfja.ccp.server.persistence.reloaded.entities.common.AuditAttributesEmbedded;
      import com.corfja.ccp.server.persistence.reloaded.entities.contract.ContractRoleEntity;
      
      @Entity
      @Audited
      @NamedQueries({
         @NamedQuery(name = AgentEntity.NAMED_QUERY_FIND_AGENT_BY_AGENT_ID, query = "SELECT p FROM AgentEntity p WHERE p.agentId = :agentId"),
         @NamedQuery(name = AgentEntity.NAMED_QUERY_FIND_AGENT_BY_EXTERNAL_PARTNER_ID, query = "SELECT p FROM AgentEntity p WHERE p.externalPartnerId = :externalPartnerId")
      })
      @Table(name = "AGENT")
      public class AgentEntity implements java.io.Serializable {
         
         public static final String NAMED_QUERY_FIND_AGENT_BY_AGENT_ID = "findAgentByAgentId";
         public static final String NAMED_QUERY_FIND_AGENT_BY_EXTERNAL_PARTNER_ID = "findAgentByExternalPartnerId";
         
         private static final long serialVersionUID = 5587980614216695759L;
      
         @Id
         @SequenceGenerator(name = "AGENT_SEQ", sequenceName = "AGENT_SEQ")
         @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AGENT_SEQ")
         @Column(name = "AGT_ID")
         private Long agentId;
      
         @Basic(optional = false)
         @Column(name = "AGT_STATE")
         private Short state;
      
         @Basic(optional = false)
         @Column(name = "AGT_TYPE")
         private Short type;
         
         @Basic(optional = false)
         @Column(name = "AGT_EXTERNAL_PARTNER_ID")
         private String externalPartnerId;
         
         @OneToMany(cascade = ALL, mappedBy = "agent")
         private Collection<AgentAttributeEntity> attributes;
         
         @OneToMany(cascade = ALL, mappedBy = "agent")
         private Collection<AgentGroupRoleEntity> agentGroupRoles;
         
         @OneToMany(cascade = ALL, mappedBy = "agent")
         private Collection<ContractRoleEntity> contractRoles;
      
         @Embedded
         @AttributeOverrides({ 
            @AttributeOverride(name = "validFrom", column = @Column(name = "AGT_VALID_FROM")),
            @AttributeOverride(name = "validTo", column = @Column(name = "AGT_VALID_TO")),
            @AttributeOverride(name = "createdAt", column = @Column(name = "AGT_CREATED_AT")),
            @AttributeOverride(name = "modifiedAt", column = @Column(name = "AGT_MODIFIED_AT")),
            @AttributeOverride(name = "user", column = @Column(name = "AGT_USER")) 
         })
         private AuditAttributesEmbedded auditAttributes;
      
          public AgentEntity() {
          }
      
         /**
          * @return the agentId
          */
         public Long getAgentId() {
         
            return agentId;
         }
      
         /**
          * @param agentId the agentId to set
          */
         public void setAgentId(Long agentId) {
         
            this.agentId = agentId;
         }
      
         /**
          * @return the state
          */
         public Short getState() {
         
            return state;
         }
      
         /**
          * @param state the state to set
          */
         public void setState(Short state) {
         
            this.state = state;
         }
      
         /**
          * @return the type
          */
         public Short getType() {
         
            return type;
         }
      
         /**
          * @param type the type to set
          */
         public void setType(Short type) {
         
            this.type = type;
         }
      
         /**
          * @return the externalPartnerId
          */
         public String getExternalPartnerId() {
         
            return externalPartnerId;
         }
      
         /**
          * @param externalPartnerId the externalPartnerId to set
          */
         public void setExternalPartnerId(String externalPartnerId) {
         
            this.externalPartnerId = externalPartnerId;
         }
         
         /**
          * @return the attributes
          */
         public Collection<AgentAttributeEntity> getAttributes() {
         
            return attributes;
         }
      
         /**
          * @param attributes the attributes to set
          */
         public void setAttributes(Collection<AgentAttributeEntity> attributes) {
         
            this.attributes = attributes;
         }
      
         /**
          * @return the agentGroupRoles
          */
         public Collection<AgentGroupRoleEntity> getAgentGroupRoles() {
         
            return agentGroupRoles;
         }
      
         /**
          * @param agentGroupRoles the agentGroupRoles to set
          */
         public void setAgentGroupRoles(Collection<AgentGroupRoleEntity> agentGroupRoles) {
         
            this.agentGroupRoles = agentGroupRoles;
         }
      
         /**
          * @return the contractRoles
          */
         public Collection<ContractRoleEntity> getContractRoles() {
         
            return contractRoles;
         }
      
         /**
          * @param contractRoles the contractRoles to set
          */
         public void setContractRoles(Collection<ContractRoleEntity> contractRoles) {
         
            this.contractRoles = contractRoles;
         }
      
         /**
          * @return the auditAttributes
          */
         public AuditAttributesEmbedded getAuditAttributes() {
         
            return auditAttributes;
         }
      
         /**
          * @param auditAttributes the auditAttributes to set
          */
         public void setAuditAttributes(AuditAttributesEmbedded auditAttributes) {
         
            this.auditAttributes = auditAttributes;
         }
      
         /* (non-Javadoc)
          * @see java.lang.Object#toString()
          */
         @Override
         public String toString() {
            return "AgentEntity [agentId=" + agentId + ", externalPartnerId=" + externalPartnerId 
               + ", state=" + state + ", type=" + type + ", auditAttributes=" + auditAttributes.toString() 
               + ", attributes=" + attributes + ", agentGroupRoles=" + agentGroupRoles 
               + ", contractRoles=" + contractRoles + "]";
         }
      }
      

       

       

      And finally here comes my not so successful approach to build a AuditQuery on 2 of the embedded attributes:

       

      public <T, K> List<T> getEntitiesByKeyAndDate(Class<T> c, K key, Date date) 
         throws BusinessException, TechnicalException {
            try {
               AuditReader reader = AuditReaderFactory.get(em);
               // 1. Schritt - Alle Revisions zum Key   
               List<Number> revisions = reader.getRevisions(c, key);
               List<T> entities = new ArrayList<T>();
               for (Number r : revisions ) {
                  AuditQuery query = reader.createQuery().forEntitiesAtRevision(c, r);
                  query.add(AuditEntity.id().eq(key));
                  query.add(AuditEntity.property("validFrom").le(date));
                  query.add(AuditEntity.property("validTo").ge(date));
                  entities.add((T) query.getResultList());
               }
               return entities;  
            } catch (RuntimeException e) {
               throw new TechnicalException(MessageCode.GENERAL_UNRECOVERABLE_ERROR, e);
            }      
         }
      

       

      And this is the part that didn't work. Anbody knows the correct way to access these two attributes?!

                  query.add(AuditEntity.property("validFrom").le(date));
                  query.add(AuditEntity.property("validTo").ge(date));

       

      Something like this didn't work either:

                  query.add(AuditEntity.property("auditAttributes.validFrom").le(date));
                  query.add(AuditEntity.property("auditAttributes.validTo").ge(date));

       

      Maybe I'm dumb or it's because its Friday afternoon, but I don't get it. Any help welcome.

       

      Thanks in advance,

      Dominik

        • 1. Re: Trouble with AuditEntity.property
          adamw

          I would bet that either auditAttributes.validFrom or validFrom should work ... Maybe try something like auditAttributes_validFrom? In fact we would need to see what is the Hibernate mapping generated for the audit entities - you can comment out the writeDocument calls in EntitiesConfigurator.

           

          Adam