Repository navigation
Expand file tree
/
Copy pathMeal.java
More file actions
125 lines (103 loc) · 3.9 KB
/
Copy pathMeal.java
File metadata and controls
125 lines (103 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package ru.javawebinar.topjava.model;
import com.fasterxml.jackson.annotation.JsonBackReference;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.validator.constraints.Range;
import org.hibernate.validator.constraints.SafeHtml;
import org.springframework.format.annotation.DateTimeFormat;
import ru.javawebinar.topjava.View;
import ru.javawebinar.topjava.util.DateTimeUtil;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import static org.hibernate.validator.constraints.SafeHtml.WhiteListType.NONE;
@NamedQueries({
@NamedQuery(name = Meal.ALL_SORTED, query = "SELECT m FROM Meal m WHERE m.user.id=:userId ORDER BY m.dateTime DESC"),
@NamedQuery(name = Meal.DELETE, query = "DELETE FROM Meal m WHERE m.id=:id AND m.user.id=:userId"),
@NamedQuery(name = Meal.GET_BETWEEN, query = """
SELECT m FROM Meal m
WHERE m.user.id=:userId AND m.dateTime >= :startDateTime AND m.dateTime < :endDateTime ORDER BY m.dateTime DESC
"""),
// @NamedQuery(name = Meal.UPDATE, query = "UPDATE Meal m SET m.dateTime = :datetime, m.calories= :calories," +
// "m.description=:desc where m.id=:id and m.user.id=:userId")
})
@Entity
@Table(name = "meals", uniqueConstraints = {@UniqueConstraint(columnNames = {"user_id", "date_time"}, name = "meals_unique_user_datetime_idx")})
public class Meal extends AbstractBaseEntity {
public static final String ALL_SORTED = "Meal.getAll";
public static final String DELETE = "Meal.delete";
public static final String GET_BETWEEN = "Meal.getBetween";
@Column(name = "date_time", nullable = false)
@NotNull
@DateTimeFormat(pattern = DateTimeUtil.DATE_TIME_PATTERN)
private LocalDateTime dateTime;
@Column(name = "description", nullable = false)
@NotBlank
@Size(min = 2, max = 120)
@SafeHtml(groups = {View.Web.class}, whitelistType = NONE)
private String description;
@Column(name = "calories", nullable = false)
@NotNull
@Range(min = 10, max = 5000)
private Integer calories;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonBackReference
@NotNull(groups = View.Persist.class)
private User user;
public Meal() {
}
public Meal(LocalDateTime dateTime, String description, int calories) {
this(null, dateTime, description, calories);
}
public Meal(Integer id, LocalDateTime dateTime, String description, int calories) {
super(id);
this.dateTime = dateTime;
this.description = description;
this.calories = calories;
}
public LocalDateTime getDateTime() {
return dateTime;
}
public String getDescription() {
return description;
}
public int getCalories() {
return calories;
}
public LocalDate getDate() {
return dateTime.toLocalDate();
}
public LocalTime getTime() {
return dateTime.toLocalTime();
}
public void setDateTime(LocalDateTime dateTime) {
this.dateTime = dateTime;
}
public void setDescription(String description) {
this.description = description;
}
public void setCalories(Integer calories) {
this.calories = calories;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String toString() {
return "Meal{" +
"id=" + id +
", dateTime=" + dateTime +
", description='" + description + '\'' +
", calories=" + calories +
'}';
}
}