Reputation: 3244
Our repository entities have audit/log properties like (createdDate
, updatedDate
...) that are assigned by the application and should be read-only properties to the user. A little bit of research on Jackson showed this feature is not supported (or is it?). Wondering if SDR could be of any help here?
@Document(collection="employees")
public class Employee {
@Id
private String id;
private firstName;
private lastName;
//read-only attribute. initially assigned by application and should not be changed
private Date createdDate;
//read-only attribute. initially assigned by application and should not be changed
private Employee createdBy;
//getters setters truncated....
}
Upvotes: 4
Views: 4816
Reputation: 21074
The Jackson issue you linked to (jackson-databind#95) has since been implemented. That feature allows using @JsonIgnoreProperties(allowGetters=true)
to include the data when writing the JSON, but exclude it when reading it.
This feature alone isn't sufficient to get Spring Data REST to ignore properties, since it'll result in these properties always being null
when updated.
Upvotes: 1
Reputation: 1948
org.springframework.data.annotation.ReadOnlyProperty is added in spring-data-commons 1.9.0.
This serves the purpose. Please refer:
Upvotes: 2
Reputation: 4341
Using spring-data-rest with spring-data-jpa, i have had success using jpa @Column(updatable = false).
Looks like you're not using JPA (@Document), does your persistence framework have an annotation to indicate a read-only field?
Furthermore, having a getter but not a setter could be part of the puzzle. In my case spring-data-jpa AuditingEntityListener sets the field, so i do not need a setter in the application code.
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import org.joda.time.DateTime;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
@MappedSuperclass
public abstract class AuditedEntity extends AuditedUpdateEntity {
@CreatedDate
@Column(updatable = false)
private DateTime createdTs;
@CreatedBy
@Column(updatable = false)
private String createdBy;
public String getCreatedBy() {
return createdBy;
}
public DateTime getCreatedTs() {
return createdTs;
}
}
Upvotes: 0