当@Id在Getter上而不是在属性上时,JaVers异常

时间:2016-05-30 10:01:06

标签: java annotations javers

我有一个实体,由于多种原因(Why should anybody put annotations on the getters or setters when using JPA to map the classes?)我们正在注释getter方法而不是字段:

protected Long id;
...

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

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

致电:

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

JaVers抛出以下异常:

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.

是否支持?

3 个答案:

答案 0 :(得分:3)

JaVers确实支持getter& amp; setters,它叫做BEAN映射样式,请参阅:http://javers.org/documentation/domain-configuration/#property-mapping-style

默认使用FIELD映射,但您可以按如下方式切换到BEAN:

Javers javers = JaversBuilder
               .javers()
               .withMappingStyle(MappingStyle.BEAN)
               .build();

答案 1 :(得分:0)

我已经将javers.mappingStyle=BEAN设置为application.properties,它可以正常工作。

答案 2 :(得分:-1)

我的立场得到了纠正。 JaVers根据文档http://javers.org/documentation/domain-configuration/#property-mapping-style支持Bean和字段映射样式。尽管如此,当按照建议使用BEAN映射时,它仍然无法正常工作。也许它与其他注释相冲突?这是我的代码,我在哪里放置了JaVers @Id

我将MappingStyle更改为BEAN样式但仍然无效。以下是我的getter字段中的JaVers @Id:

@Id
@GeneratedValue(generator = "...")
@SequenceGenerator(...
@Column(name = "id")
@org.javers.core.metamodel.annotation.Id
public Long getId()
{
    return id;
}
相关问题