Home > Back-end >  can't figure out how to audit for null value in not owned entity using hibernate envers
can't figure out how to audit for null value in not owned entity using hibernate envers

Time:09-17

What my project have:

  1. rsqlParser in order to parse complicated queries
  2. Hibernate envers for audit purposes
  3. Pretty stupid middle developer who don't know how to implement isNull rsql query

I have two Object with strict one-to-one relationship: object A which contains object B, and object B, which contains object A. In RDS it's looks like object B has an object_a_id field

Object_A entity class

@Entity
@Getter
@Setter
@Audited
@NoArgsConstructor
public class Object_A {

    @OneToOne(mappedBy = "object_a")
    private Object_B object_b;
}

Object_B entity

@Entity
@Getter
@Setter
@Audited
@NoArgsConstructor
public class Object_B {

    @OneToOne
    @JoinColumn(
        name = "object_a_id",
        referencedColumnName = "id",
        foreignKey = @ForeignKey(name = "object_b_object_a_fk")
    )
    private Object_A object_a;

Clearly you see that Object_B OWNS Object_A and when I try to perfom something simple like

return auditProperty.isNull();

I get

This type of relation (object_b) isn't supported and can't be used in queries

I guess I need somehow to make custom query where I add some object_b subselect beforehand but can't figure out how to write it.

CodePudding user response:

You should probably create an issue in the issue tracker(https://hibernate.atlassian.net) with a test case(https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) that shows this limitation and ask for an improvement for this.

Usually, in ORM this is handled on the SQL level by introducing an exists subquery like:

where not exists (select 1 from object_b b where b.object_a_id = rootAlias.id)

Not sure how that works exactly in your case, but you could try to do something similar in your query.

  • Related