0

I have an Entity which for a number of reasons (Why should anybody put annotations on the getters or setters when using JPA to map the classes?) we are annotating the getter method instead of the field:

protected Long id;
...

@Id
@GeneratedValue(...)
@SequenceGenerator(...)
@Column(name = "id")
public Long getId()
{
    return id;
}

public void setId(Long id)
{
    this.id = id;
}

When calling:

javers.findChanges(QueryBuilder.byInstanceId(5, MyModel.class).build())

JaVers is throwing the following exception:

JaversException: ENTITY_WITHOUT_ID Class 'com.myproject.model.MyModel' mapped as Entity has no Id property. Use @Id annotation to mark unique and not-null Entity identifier.

Is it supported?

Community
  • 1
  • 1
patri
  • 59
  • 9

3 Answers3

3

JaVers do supports mapping on getters & setters, it's called BEAN mapping style, see: http://javers.org/documentation/domain-configuration/#property-mapping-style.

By default, FIELD mapping is used but you can switch to BEAN as follows:

Javers javers = JaversBuilder
               .javers()
               .withMappingStyle(MappingStyle.BEAN)
               .build();
Bartek Walacik
  • 3,386
  • 1
  • 9
  • 14
  • Ive read the documentation and it states that @Id can be used on both field and getter. I did it before as you suggested (withMappingStyle ..BEAN) but is still throwing an error. This my getter: (added to original question) – patri May 31 '16 at 10:14
  • @parti as I said before, JaVers supports annotations on getters. Maybe there is a bug and it doesn't work in some corner cases. If you think you found a bug you should report it here https://github.com/javers/javers/issues . Please follow Guidelines for Bug Reporting - https://github.com/javers/javers/blob/master/CONTRIBUTING.md – Bartek Walacik May 31 '16 at 18:04
  • The project I'm dealing with has JPA entity annotations in both fields and properties (i.e. getter methods), so I have entities that were annotated in getter methods whilst others have been annotated in the fields, then, this solution does not work for me, either I'll have to choose one strategy for all my entities, or the better, Javers should've been able to read the annotation `@javax.persistence.Access(FIELD)` that indicates where the annotations should be read from. – s_bighead Jun 02 '20 at 21:38
0

I had set javers.mappingStyle=BEAN to application.properties and it works.

-1

I stand corrected. JaVers supports both Bean and field Mapping style as per documentation http://javers.org/documentation/domain-configuration/#property-mapping-style. Still, when using the BEAN mapping as suggested, it is still not working. Maybe it is conflicting with other annotations? Here is my code and where Ive placed the JaVers @Id

I changed the MappingStyle to BEAN style and still it doesnt work. Here is the JaVers @Id on my getter field:

@Id
@GeneratedValue(generator = "...")
@SequenceGenerator(...
@Column(name = "id")
@org.javers.core.metamodel.annotation.Id
public Long getId()
{
    return id;
}
patri
  • 59
  • 9