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.