2

I'm using Spring boot 1.5.3 with Spring Data Rest and JPA. I'm experiencing a very strange problem.

This is the superclass of my entity beans:

@EntityListeners({ AuditingEntityListener.class })
@MappedSuperclass
@Audited
public abstract class AbstractEntity extends AbstractPersistable<Long> {
    private static final long serialVersionUID = 1L;

    /* "UUID" and "UID" are Oracle reserved keywords -> "sid" */
    @Column(name = "sid", unique = true, nullable = false, updatable = false, length = 36)
    private String sid;

    @CreatedBy
    private String createdBy;

    @CreatedDate
    @Column(updatable = false)
    private LocalDateTime createdDate;

    @LastModifiedDate
    private LocalDateTime lastModifiedDate;

    @LastModifiedBy
    private String lastModifiedBy;

    // Trick to start version counting from 1 instead of 0
    @Version
    private int version = 1;

    public AbstractEntity() {
    }

    @PrePersist
    public void initializeUUID() {
        if (sid == null) {
            sid = UUID.randomUUID().toString();
        }
    }

    public String getSid() {
        return sid;
    }

    public LocalDateTime getCreatedDate() {
        return createdDate;
    }

    public LocalDateTime getLastModifiedDate() {
        return lastModifiedDate;
    }

    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public int getVersion() {
        return version;
    }

    @Override
    public boolean equals(Object o) {
        return (o == this || (o instanceof AbstractEntity && sid.equals(((AbstractEntity) o).sid)));
    }

    @Override
    public int hashCode() {
        return sid.hashCode();
    }

}

This is a entity I persist:

@Entity
public class Country extends AbstractEntity {
    private static final long serialVersionUID = 4487829062553023011L;

    @Audited
    @NotNull(message = "The name cannot be empty")
    @Column(nullable = false)
    private String name;

    @Audited
    @NotNull(message = "The ISO code cannot be empty")
    @Size(min = 2, max = 2)
    @Column(nullable = false, unique = true)
    private String isoCode;

    public Country() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIsoCode() {
        return isoCode;
    }

    public void setIsoCode(String isoCode) {
        this.isoCode = isoCode;
    }

}

When I look at Swagger I see a correct definition of the model:

Country {
    createdDate (string, optional),
    id (integer, optional),
    isoCode (string),
    lastModifiedBy (string, optional),
    lastModifiedDate (string, optional),
    name (string),
    new (boolean, optional),
    sid (string, optional),
    version (integer, optional)
}

But when I asked for the all countries or for a specific country the version field is not visible:

{
  "sid": "cd2527a0-4667-4d25-a098-70f12bfed2da",
  "createdDate": "2017-06-22T11:02:48",
  "lastModifiedDate": "2017-06-22T11:02:48",
  "lastModifiedBy": null,
  "name": "Italy",
  "isoCode": "IT",
  "new": false,
  "_links": {
    "self": {
      "href": "http://localhost:8080/api/v1/countries/1"
    },
    "country": {
      "href": "http://localhost:8080/api/v1/countries/1"
    }
  }

}

I thought it was a problem of a reserved keyword but it's not. Also renaming the field it is present in the model but not in the return data.

Am I doing something wrong? I need the version because Hibernate needs that to persist correctly.

veljkost
  • 1,748
  • 21
  • 25
drenda
  • 5,846
  • 11
  • 68
  • 141
  • 1
    Is the version field added to the table DDL? BTW I would use a long instead of an int – Narmer Jun 22 '17 at 09:29
  • For sure, the version is present in the DDL and in the database as well. Thanks for the suggestion, I changed to long but of course this doesn't solve the problem. – drenda Jun 22 '17 at 09:32
  • Are you using the the annotation @Version from javax.persistence interface and not the spring-data rest jpa framework? https://stackoverflow.com/questions/10648515/using-version-in-spring-data-project – Eduardo Sanchez-Ros Jun 22 '17 at 09:36
  • 3
    Have a look at [this](https://stackoverflow.com/questions/36853343/with-spring-data-rest-why-is-the-version-property-becoming-an-etag-and-not-inc), seems exactly your problem. – Narmer Jun 22 '17 at 09:36
  • @Narmer Thanks a lot! That's the solution! – drenda Jun 22 '17 at 09:45
  • @Narmer please post the solution as an answer and accept it so others can benefit from the answer without having to look in comments. – kewne Jun 22 '17 at 10:55

0 Answers0