-->
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.  [ 3 posts ] 
Author Message
 Post subject: Envers: QueryException on superclass lazy collection
PostPosted: Mon Aug 21, 2017 9:03 am 
Newbie

Joined: Wed Feb 21, 2007 1:48 pm
Posts: 8
Hello,

This is my first post in the hibernate forum, so please, bear with me :)

I am trying to implement Hibernate Envers in an application I developed.
I am using Hibernate 5.2.7 and Postgres 9.4.
I have used the @Audited and @NotAudited tags, and follows the documentation, and everything gets saved beautifully on the database, but I cannot retrieve my entities.

Please see my entities:
Code:
@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Audited
public class PersistentObject {
   @Id
   @GeneratedValue(generator = "increment")
   @GenericGenerator(name = "increment", strategy = "increment")
   private Long id;


Code:
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Entity
@Audited
public abstract class Agent extends PersistentObject implements Comparable<Agent>, Identifiable {
   @ManyToMany(mappedBy = "assignedTo")
   private List<Agent> assignedAgents = new ArrayList<>();

   @ManyToMany
   private List<Agent> assignedTo = new ArrayList<>();

        ...


Code:
@Entity(name = "User")
@Table(name = "SNSUser")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Audited
public class User extends Agent {
   @Column(unique = true)
   private String username;

   private String email;

        ....


Now, as I modify my users, I can see all the modifications bring written in SNSUser_AUD table.
I can also list revisions, and everything works fine.

However, once I retrieve my entity, and try to access the method entity.getAssignedAgents(), the following exception is thrown:
Code:
Caused by: org.hibernate.QueryException: could not resolve property: originalId.assignedAgents_id of: Agent_Agent_AUD [select new list(ee__, e__) from Agent_Agent_AUD ee__, de.ccsys.sns.models.Agent_AUD e__ where ee__.originalId.assignedAgents_id = :assignedAgents_id and e__.originalId.REV.id = (select max(e2__.originalId.REV.id) from de.ccsys.sns.models.Agent_AUD e2__ where e2__.originalId.REV.id <= :revision and e__.originalId.id = e2__.originalId.id) and ee__.originalId.REV.id = (select max(ee2__.originalId.REV.id) from Agent_Agent_AUD ee2__ where ee2__.originalId.REV.id <= :revision and ee__.originalId.assignedAgents_id = ee2__.originalId.assignedAgents_id and ee__.originalId.Agent_id = ee2__.originalId.Agent_id) and ee__.REVTYPE != :delrevisiontype and e__.REVTYPE != :delrevisiontype and (ee__.originalId.Agent_id = e__.originalId.id or (ee__.originalId.Agent_id is null and e__.originalId.id is null))]
   at org.hibernate.QueryException.generateQueryException(QueryException.java:120)
   at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
   at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:217)



The database was implemented by hibernate schema generator itself (hbm2dll.auto=update).
Here stands the current table:

Code:
CREATE TABLE agent_agent_aud
(
  rev integer NOT NULL,
  assignedagents_id bigint NOT NULL,
  assignedto_id bigint NOT NULL,
  revtype smallint,
  CONSTRAINT agent_agent_aud_pkey PRIMARY KEY (rev, assignedagents_id, assignedto_id),
  CONSTRAINT fkccop2twpfcyjf0jy9n27a0hj2 FOREIGN KEY (rev)
      REFERENCES snsrevision (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)


Could anyone point me in the right direction here?


Top
 Profile  
 
 Post subject: Re: Envers: QueryException on superclass lazy collection
PostPosted: Wed Aug 23, 2017 9:35 am 
Hibernate Team
Hibernate Team

Joined: Wed Jun 15, 2016 9:04 am
Posts: 24
The simplest way to fix this is to make use of the @AuditMappedBy annotation to give Envers a hint in this use-case.
Code:
@Entity(name = "Agent")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Audited
public abstract class Agent extends PersistentObject implements Comparable<Agent>, Identifable {
  @ManyToMany(mappedBy = "assignedTo")
  @AuditMappedBy(mappedBy = "assignedTo")
  private List<Agent> assignedAgents = new ArrayList<>();
 
  ...
}

Unfortunately there are some use cases where Envers uses a custom annotation to drive "hints" rather than basing its influence off the standard annotations. Some of this is simply legacy decisions that I want to try and deprecate and move away from while others still will have some merit until we look at some internal changes with how the mappings are managed between associations.

For now however, the @AuditMappedBy annotation should suffice to get you around this hurdle.


Top
 Profile  
 
 Post subject: Re: Envers: QueryException on superclass lazy collection
PostPosted: Mon Oct 09, 2017 12:26 pm 
Newbie

Joined: Wed Feb 21, 2007 1:48 pm
Posts: 8
Dear Naros,

THANK YOU very much for this comment. It worked perfectly.
I completely missed this in the documentation... maybe this should be more highlighted somewhere.

Great to see hibernate receiving such great support!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.