forked from mrigor87/CalorieManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeal.java
More file actions
113 lines (91 loc) · 3.22 KB
/
Meal.java
File metadata and controls
113 lines (91 loc) · 3.22 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
package ru.javawebinar.topjava.model;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
/**
* GKislin
* 11.01.2015.
*/
@SuppressWarnings("JpaQlInspection")
@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 BETWEEN :startDate AND :endDate 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 BaseEntity {
public static final String GET = "Meal.get";
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
private LocalDateTime dateTime;
@Column(name = "description", nullable = false)
@NotEmpty
private String description;
@Column(name = "calories", nullable = false)
@Range(min = 10, max = 5000)
protected int calories;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
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(int 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 +
'}';
}
}