Repository navigation
Expand file tree
/
Copy pathMeal.java
More file actions
107 lines (85 loc) · 2.85 KB
/
Copy pathMeal.java
File metadata and controls
107 lines (85 loc) · 2.85 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
package ru.javawebinar.topjava.model;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.Range;
import javax.persistence.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
/**
* GKislin
* 11.01.2015.
*/
@NamedQueries({
@NamedQuery(name = Meal.DELETE, query = "DELETE FROM Meal m WHERE m.id=:id AND m.user.id=:user_id"),
@NamedQuery(name = Meal.GET_BETWEEN, query = "SELECT m FROM Meal m WHERE m.user.id=:user_id AND m.dateTime BETWEEN " +
":start_date AND :end_date ORDER BY m.dateTime DESC"),
@NamedQuery(name = Meal.ALL_SORTED, query = "SELECT m FROM Meal m WHERE m.user.id=:user_id ORDER BY m.dateTime DESC"),
})
@Entity
@Table(name = "meals")
public class Meal extends BaseEntity {
public static final String DELETE = "Meal.delete";
public static final String GET_BETWEEN = "Meal.getBetween";
public static final String ALL_SORTED = "Meal.getAllSorted";
@Column(name = "date_time", nullable = false, columnDefinition = "timestamp default now()")
private LocalDateTime dateTime;
@Column(name = "description", nullable = false)
@NotBlank
private String description;
@Column(name = "calories", nullable = false)
@Range(min = 10, max = 10000)
private int calories;
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.REMOVE})
@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 +
'}';
}
}