Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Hibernate – apply @Where when fetching

I am using following annotation on my Hibernate entity class

@Table(name="cms_user", schema="public")
@Where(clause = "deleted = false")
public class User {
  ..
}

So it does not load records that are "soft deleted". But when I fetch this entity as part of another query:

queryRoot.fetch("cmsUser", JoinType.INNER);

this where clause is not applied and also deleted users are visible. Of course I can filter them out then, but I am looking for an efficient way to set this deleted = false on single place and @Where or some other hibernate class annotation is a great candiadate.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there any way how can I annotate my hibernate class so deleted = false is applied also for fetches and joins?

>Solution :

I didn’t test it but try to add @Where annotation to your joined entity instead, in other words to your @OneToMany annotation.

@Where(clause = "deleted = false")
@OneToMany(mappedBy = "joinedEntity", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<User> userList;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading