1

I've just started using Javers on my Application but I have entities annoted with @Transient that I thought Javers would ignore than, but no :(, instead it's throwing me an exception:

JaversException ENTITY_INSTANCE_WITH_NULL_ID: Found Entity instance 'ProductData' with null Id-property 'id'

Do you guy know if there is a way to Ignore those transient fields?

The Documentation says that the @Transient annotation is a synonym for @DiffIgnore. But i dont know if that is related to only comparacion, or during the audit flow as well.

Here is my code:

@Entity
public class ProductExternal extends AbstractEntity implements ExternalEntity {

    @Transient
    private ProductData productData;

    @NotNull
    @Column(unique=true)
    private Long externalId;

    public ProductExternal() { }

    //get set
}

--

@Entity
public class ProductData extends AbstractEntity {

private static final long serialVersionUID = 1L;

@Column
@NotNull
private String name;

public ProductData() { }


//get set
}

Parent class

@MappedSuperclass
public abstract class AbstractEntity implements Serializable  {

    public AbstractEntity() {}

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;

    @Version
    @Column(columnDefinition = "bigint default '0'")
    protected Long version;

    //get set

}
Pingolin
  • 3,161
  • 6
  • 25
  • 40
  • I had same problem with AbstractEntity class where I have Id annotation, if you add the Id annotation also on your class it will work. The main cause is that Javers looks like does not understand where is the id annotation – FarukT May 09 '22 at 12:37

1 Answers1

1

Your class and mapping (annotations) seems fine. The exception is saying:

Found Entity instance 'ProductData' with null Id-property 'id'

So you are trying to commit to Javers an object of class ProductData which has null id field. Obviously that's not possible. That's a common issue with Hibernate's @GeneratedValue magic. Your field is null at the first place, and then it's being updated later by Hibernate after calling DB sequence next val.

Generally, you should call Javers commit() after Hibernate is done with persisting your object. It can be easily achieved when using one of Javers' auto-audit aspects: @JaversAuditable or @JaversSpringDataAuditable. They are applied in the right phase and call Javers commit() for you. See https://javers.org/documentation/spring-integration/#auto-audit-aspect.

Bartek Walacik
  • 3,386
  • 1
  • 9
  • 14
  • But my object being a Transient object, it wont have an Id, even after the Hibernate Commit, hibernate will ignore it. Thus, there isn't a way to set the Id attribute if we follow the hibernate's flow. What i did as a paliative solution was setting a fake Id, but it would be awesome if i could ignore all @Transient fields. If don't, i'll check this request as resolved, i just need a final answer if it's possible or not. BTW, i'm already using `@JaversSpringDataAuditable` – Otávio Augusto Junior Nov 19 '19 at 13:04
  • You mean if you could ignore the @Transient annotation? – Bartek Walacik Nov 19 '19 at 17:30
  • Yes, ignore @Transient fields from being audited. Javers would ignore them, they are irrelevant for me. Nobody ever needed to ignore specific fields? – Otávio Augusto Junior Nov 19 '19 at 18:58
  • @OtávioAugustoJunior, Javers DOES ignore fields and getters with `@Transient` ann, that's pretty logical – Bartek Walacik Nov 20 '19 at 06:11
  • I haven't changed a thing from my code above, and if i don't set a fake ID on my transient Object, i got that `with null Id-property 'id'`error. And even setting an ID, the result from Javers changes are: https://i.imgur.com/wYf29Gh.png. It DOES NOT ignore the field. I'll debug Javers and try to understand the framework, maybe there's something else that i have to do. Tks. – Otávio Augusto Junior Nov 20 '19 at 16:31
  • I've found the problem, don't know to fix it yet :). But, the problem is that my `ProductExternal` class has an abstract parent class named `AbstractProduct` (It's a class between my `AbstractEntity` and ´ProductExternal´), and all my other entities makes reference to `AbstractProduct` and not `ProductExternal`. When Javers engine maps all entity classes, my `ProductExternal` is ignored. In the end, when Javers tries to generate the object id, javers retrives the AbstractProduct's ignoredProperties to filter (that's empty), and not `ProductExternal` that has my @Transient field. – Otávio Augusto Junior Nov 20 '19 at 21:56
  • If you want to continue this thread push a failing test case here https://github.com/javers/javers/tree/master/javers-core/src/test/groovy/org/javers/core/cases – Bartek Walacik Nov 21 '19 at 05:45