我有一个用户实体和一个订单实体。 订单实体中的一个字段是日期。 直到现在我让用户输入日期。 现在我希望在发出请求后,日期会自动设置为当前日期并存储在数据库中。
尝试使用@Prepersist注释但是因为这是我使用springBoot开发的第一个API,所以我真的不知道如何使用它。
用户实体
@Entity
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private BigInteger id;
@NotEmpty(message = "Name is compulsory")
@Pattern(regexp="^[A-Za-z]*[A-Za-z-'. ]*[A-Za-z]*$",message = "Name has invalid characters")
private String username;
//@NotEmpty(message = "Phone Number is compulsary")
@Range(min = 6400000000L ,max=9999999999L)
private Long phoneNumber;
@NotEmpty(message = "Address is compulsary")
private String address;
public User(){}
public User(BigInteger id, String username, Long phoneNumber, String address) {
super();
this.id = id;
this.username = username;
this.phoneNumber = phoneNumber;
this.address = address;
}
public BigInteger getId() {
return id;
}
public void setId(BigInteger id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Long getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(Long phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
订单实体
@Entity
public class Orders {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private BigInteger id;
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
private Date date;
@ManyToOne
private User user;
public Orders(){}
public Orders(BigInteger id, Date date,BigInteger userId) {
super();
this.id = id;
this.date = date;
this.user=new User(userId," ",0000000000L," ");
}
public BigInteger getId() {
return id;
}
public void setId(BigInteger id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
订单控制器
@RestController
public class OrdersController {
@Autowired
private OrdersService ordersService;
@ApiOperation(value="Show all orders")
@RequestMapping("/orders")
public Iterable<Orders> getAllOrders()
{
return ordersService.getAllOrders();
}
@ApiOperation(value="Show a particular Order")
@RequestMapping("/orders/{orderId}")
public Orders getOrderById(@PathVariable BigInteger orderId)
{
return ordersService.getOrderById(orderId);
}
@ApiOperation(value="Show all orders of a particular User")
@RequestMapping("/users/{id}/orders")
public List<Orders> getOrders(@PathVariable BigInteger id) {
return ordersService.getOrders(id);
}
@ApiOperation(value="Show an order for a User")
@RequestMapping("/users/{userId}/orders/{id}")
public Orders getOrder(@PathVariable BigInteger id){
return ordersService.getOrder(id);
}
@ApiOperation(value="Adds a new Order")
@RequestMapping(method = RequestMethod.POST,value = "/users/{userId}/orders")
public Orders addOrder(@PathVariable BigInteger userId,@RequestBody Orders orders) {
orders.setUser(new User(userId," ",0000000000L," "));
return ordersService.addOrder(orders);
}
@ApiOperation(value="Alter an Order")
@RequestMapping(method = RequestMethod.PUT, value="/users/{userId}/orders/{id}")
public Orders updateOrder(@RequestBody Orders order,@PathVariable BigInteger id,@PathVariable BigInteger userId)throws Exception {
order.setUser(new User(userId," ",0000000000L," "));
return ordersService.updateOrder(order, id);
}
@ApiOperation(value="Delete an Order")
@RequestMapping(method = RequestMethod.DELETE, value="/orders/{id}")
public void deleteOrder(@PathVariable BigInteger id){
ordersService.deleteOrder(id);
}
}
答案 0 :(得分:0)
@PrePersist
是JPA注释,因此应该适用于所有兼容的持久性框架。它指示应在特定实体生命周期事件上调用的方法。 (其他事件在Hibernate用户指南here中有详细记录)。
将此添加到您的实体:
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
private Date timestamp;
@PrePersist
private void onCreate() {
timestamp = new Date();
}
对于在控制器中分配/创建实体,最好在控制器中使用DTO(数据传输对象)(@RequestBody OrderDto orderDto
),然后使用某种方法用这些实体填充新的实体实例值。最常见的选择是
手动
...
Order o = new Order();
o.user = userDao.findById(orderDto.getUserId());
...
// persist o