-->
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: Envers: Empty Foreign keys on audit tables
PostPosted: Tue Oct 10, 2017 8:59 am 
Newbie

Joined: Tue Oct 10, 2017 8:52 am
Posts: 1
I have the following relation and the foreign key is always empty in the audit table after the new revision:

@ManyToOne
@Audited(targetAuditMode=RelationshipTargetAuditMode.NOT_AUDITED)
@JoinColumn(name="mail_iid")
@private Mail mail;

...

@OneToMany(cascade=Cascade.ALL, orphanRemoval = true, fetch= fetchType.EAGER)
@JoinColumn(name="mail_iid")
private List<Attachments> attachments;
After the insertion of a new register, the original table have the iid but not the revision one.

Somebody knows about this issue.


Top
 Profile  
 
 Post subject: Re: Envers: Empty Foreign keys on audit tables
PostPosted: Wed Oct 18, 2017 9:42 am 
Hibernate Team
Hibernate Team

Joined: Wed Jun 15, 2016 9:04 am
Posts: 24
There is only one way for this to happen, which is not managing bidirectional relationships properly.

I suspect you are never calling `Attachments#setMail` to assign the newly created `Mail` entity to the `Attachments` entities and instead simply add the `Attachments` entity to the collection that your `Mail` entity cascades.

This type of one-sided maintenance of bidirectional relationships is wrong and can lead to really incorrect results, particularly if entity instances are being inspected from the 1LC and are never being refreshed from the database; which is precisely why you're seeing the audit table with `null` in your `mail_iid` field.

Your code should make sure that both sides of the relationship get set properly
Code:
// setup bidirectional mappings
attachments.setMail( mail );
mail.getAttachments().add( attachments );

When you do it this way, you'll end up with `mail_iid` being populated in your audit table as you would have expected and also avoids any issues when traversing cached instances of an entity's object graph that is already loaded in the 1LC.


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.