Version 是否应该是 api 返回的 DTO 的一部分?
Should Version be part of the DTO returned by the api?
我有带有版本字段的实体。
@Data
@NoArgsConstructor
@AllArgsConstructor
@javax.persistence.Entity
@Table(name = "meals", catalog = "nutrientTracker")
public class Meal implements Entity<Long> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "comment")
private String comment;
@Column(name = "date")
private LocalDateTime date;
@Column(name = "foods")
private String foods;
@Column(name = "eaten")
private boolean eaten;
@Column(name = "price")
private float price;
@Column(name = "latitude")
private double latitude;
@Column(name = "longitude")
private double longitude;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
@Version
@Column(name = "VERSION")
private long version;
@Override
public Long getId() {
return this.id;
}
}
在我的应用程序上下文中,我将版本字段视为 ETag。
我的DTO如下:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MealDTO implements DTO<Meal, Long> {
@ApiModelProperty(required = true, example = "-1", value = "id")
private long id;
@ApiModelProperty(required = true, example = "tasty", value = "comment")
private String comment;
@ApiModelProperty(example = "1994-11-05T13:15:30Z", value = "date")
private LocalDateTime date;
@ApiModelProperty(example = "1605294438", value = "dateEpoch")
private long dateEpoch;
@ApiModelProperty(required = true, example = "banana,apple", value = "foods")
private String foods;
@ApiModelProperty(example = "false", value = "eaten")
private boolean eaten;
@ApiModelProperty(example = "6.9", value = "price")
private float price;
@ApiModelProperty(example = "69.6969696969", value = "latitude")
private double latitude;
@ApiModelProperty(example = "42.0420420420", value = "longitude")
private double longitude;
@ApiModelProperty(example = "1", value = "userId")
private long userId;
@ApiModelProperty(example = "1", value = "etag")
private String etag;
}
我在哪里将版本映射到 ETag 字段。
我的问题如下:
我应该 return 来自 API 的 ETag 作为字段吗?还是只放在 Headers 中?将它放在实体本身中是不好的做法吗?
在 client-side 我做了类似的事情:
const result: Meal = response.data;
result.etag = response.headers['etag'];
return result;
总之。这也是不好的做法吗?
没有
版本列用于对数据库执行某些操作,因此不应向 API 的用户公开。根据 @Version 文档:
Specifies the version field or property of an entity class that serves as its optimistic lock value. The version is used to ensure integrity when performing the merge operation and for optimistic concurrency control.
我有带有版本字段的实体。
@Data
@NoArgsConstructor
@AllArgsConstructor
@javax.persistence.Entity
@Table(name = "meals", catalog = "nutrientTracker")
public class Meal implements Entity<Long> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "comment")
private String comment;
@Column(name = "date")
private LocalDateTime date;
@Column(name = "foods")
private String foods;
@Column(name = "eaten")
private boolean eaten;
@Column(name = "price")
private float price;
@Column(name = "latitude")
private double latitude;
@Column(name = "longitude")
private double longitude;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
@Version
@Column(name = "VERSION")
private long version;
@Override
public Long getId() {
return this.id;
}
}
在我的应用程序上下文中,我将版本字段视为 ETag。 我的DTO如下:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MealDTO implements DTO<Meal, Long> {
@ApiModelProperty(required = true, example = "-1", value = "id")
private long id;
@ApiModelProperty(required = true, example = "tasty", value = "comment")
private String comment;
@ApiModelProperty(example = "1994-11-05T13:15:30Z", value = "date")
private LocalDateTime date;
@ApiModelProperty(example = "1605294438", value = "dateEpoch")
private long dateEpoch;
@ApiModelProperty(required = true, example = "banana,apple", value = "foods")
private String foods;
@ApiModelProperty(example = "false", value = "eaten")
private boolean eaten;
@ApiModelProperty(example = "6.9", value = "price")
private float price;
@ApiModelProperty(example = "69.6969696969", value = "latitude")
private double latitude;
@ApiModelProperty(example = "42.0420420420", value = "longitude")
private double longitude;
@ApiModelProperty(example = "1", value = "userId")
private long userId;
@ApiModelProperty(example = "1", value = "etag")
private String etag;
}
我在哪里将版本映射到 ETag 字段。
我的问题如下:
我应该 return 来自 API 的 ETag 作为字段吗?还是只放在 Headers 中?将它放在实体本身中是不好的做法吗? 在 client-side 我做了类似的事情:
const result: Meal = response.data;
result.etag = response.headers['etag'];
return result;
总之。这也是不好的做法吗?
没有
版本列用于对数据库执行某些操作,因此不应向 API 的用户公开。根据 @Version 文档:
Specifies the version field or property of an entity class that serves as its optimistic lock value. The version is used to ensure integrity when performing the merge operation and for optimistic concurrency control.