Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.technologies.Lunch_Voting.model;
- import javax.persistence.*;
- /**
- * Created by medniy on 19.01.2017.
- */
- @MappedSuperclass
- @Access(AccessType.FIELD)
- public class BaseEntity {
- public static final int START_SEQ = 100000;
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- protected Integer id;
- public BaseEntity() {
- }
- public BaseEntity(Integer id) {
- this.id = id;
- }
- public int getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public boolean isNew(){
- return (this.id == null);
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
- BaseEntity that = (BaseEntity) o;
- if (id == null || that.id == null) {
- return false;
- }
- return id.equals(that.id);
- }
- @Override
- public int hashCode() {
- return (id == null) ? 0 : id;
- }
- @Override
- public String toString() {
- return "BaseEntity{" +
- "id=" + id +
- '}';
- }
- }
- package com.technologies.Lunch_Voting.model;
- import org.hibernate.validator.constraints.NotEmpty;
- import javax.persistence.Column;
- import javax.persistence.MappedSuperclass;
- /**
- * Created by medniy on 19.01.2017.
- */
- @MappedSuperclass
- public class NamedEntity extends BaseEntity{
- @NotEmpty
- @Column(name = "name", nullable = false)
- protected String name;
- public NamedEntity() {
- }
- public NamedEntity(int id, String name) {
- super(id);
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return name;
- }
- }
- package com.technologies.Lunch_Voting.model;
- import org.hibernate.annotations.Cache;
- import org.hibernate.annotations.CacheConcurrencyStrategy;
- import org.hibernate.validator.constraints.Email;
- import org.hibernate.validator.constraints.Length;
- import org.hibernate.validator.constraints.NotEmpty;
- import javax.persistence.*;
- import java.time.LocalDateTime;
- import java.util.Set;
- /**
- * Created by medniy on 19.01.2017.
- */
- @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
- @Entity
- @Table(name = "users", uniqueConstraints = {@UniqueConstraint(columnNames = "email", name = "users_unique_email_idx")})
- public class User extends NamedEntity {
- @Column(name = "password", nullable = false)
- @NotEmpty
- @Length(min = 5)
- private String password;
- @Column(name = "email", nullable = false, unique = true)
- @Email
- @NotEmpty
- private String email;
- @Column(name = "enabled", nullable = false)
- private boolean enabled = true;
- @Column(name = "registered", columnDefinition = "timestamp default now()")
- private LocalDateTime registered;
- @Enumerated(EnumType.STRING)
- @CollectionTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"))
- @Column(name = "role")
- @ElementCollection(fetch = FetchType.EAGER)
- @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
- private Set<Role> roles;
- @OneToOne(fetch= FetchType.LAZY, mappedBy="user")
- private Vote vote;
- public User(){
- }
- public User(Integer id, String name, String password, String email, boolean enabled, LocalDateTime registered, Set<Role> roles, Vote vote) {
- super(id, name);
- this.password = password;
- this.email = email;
- this.enabled = enabled;
- this.registered = registered;
- this.roles = roles;
- this.vote = vote;
- }
- public User(String name, String password, String email, boolean enabled, LocalDateTime registered, Set<Role> roles) {
- this(null,name,password,email,enabled,registered,roles,null);
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public boolean isEnabled() {
- return enabled;
- }
- public void setEnabled(boolean enabled) {
- this.enabled = enabled;
- }
- public LocalDateTime getRegistered() {
- return registered;
- }
- public void setRegistered(LocalDateTime registered) {
- this.registered = registered;
- }
- public Set<Role> getRoles() {
- return roles;
- }
- public void setRoles(Set<Role> roles) {
- this.roles = roles;
- }
- public Vote getVote() {
- return vote;
- }
- public void setVote(Vote vote) {
- this.vote = vote;
- }
- @Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", name=" + name +
- ", email='" + email +
- ", enabled=" + enabled +
- ", registered=" + registered +
- ", roles=" + roles +
- ", vote=" + vote +
- "} ";
- }
- }
- package com.technologies.Lunch_Voting.model;
- import org.hibernate.validator.constraints.Range;
- import javax.persistence.*;
- import java.time.LocalDateTime;
- /**
- * Created by medniy on 19.01.2017.
- */
- @Entity
- @Table(name = "votes", uniqueConstraints = {@UniqueConstraint(columnNames = "user_id", name = "vote_unique_user_idx")})
- public class Vote extends BaseEntity{
- @Column(name = "mark", nullable = false)
- @Range(min = 0, max = 100)
- private Integer mark;
- @Column(name = "marked", columnDefinition = "timestamp default now()")
- private LocalDateTime marked;
- @OneToOne(fetch= FetchType.LAZY)
- @JoinColumn(name="user_id")
- private User user;
- @ManyToOne(fetch=FetchType.LAZY)
- @JoinColumn(name="restaurant_id")
- private Restaurant restaurant;
- public Vote() {
- }
- public Vote(Integer id, Integer mark, LocalDateTime marked) {
- super(id);
- this.mark = mark;
- this.marked = marked;
- }
- public Vote(Integer mark, LocalDateTime marked) {
- this(null,mark,marked);
- }
- public Integer getMark() {
- return mark;
- }
- public void setMark(Integer mark) {
- this.mark = mark;
- }
- public LocalDateTime getMarked() {
- return marked;
- }
- public void setMarked(LocalDateTime marked) {
- this.marked = marked;
- }
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- public Restaurant getRestaurant() {
- return restaurant;
- }
- public void setRestaurant(Restaurant restaurant) {
- this.restaurant = restaurant;
- }
- @Override
- public String toString() {
- return "Vote{" +
- "id=" + id +
- ", mark=" + mark +
- ", marked=" + marked +
- '}';
- }
- }
- package com.technologies.Lunch_Voting.model;
- import org.hibernate.validator.constraints.NotEmpty;
- import javax.persistence.*;
- import javax.validation.constraints.NotNull;
- import java.time.LocalDateTime;
- import java.util.Collections;
- import java.util.List;
- /**
- * Created by medniy on 19.01.2017.
- */
- @Entity
- @Table(name = "restaurants")
- public class Restaurant extends NamedEntity {
- @Column(name = "created", nullable = false)
- @NotNull
- private LocalDateTime created;
- @Column(name = "description", nullable = false)
- @NotEmpty
- private String description;
- @OneToMany(mappedBy="restaurant")
- private List<Dish> menu;
- @OneToMany(mappedBy="restaurant")
- private List<Vote> votes;
- @ManyToOne(fetch= FetchType.LAZY)
- @JoinColumn(name="user_id")
- private User user;
- public Restaurant() {
- }
- public Restaurant(Integer id, String name, LocalDateTime created, String description, List<Dish> menu, List<Vote> votes) {
- super(id, name);
- this.created = created;
- this.description = description;
- this.menu = menu;
- this.votes = votes;
- }
- public Restaurant(String name, LocalDateTime registered, String description) {
- this(null,name,registered,description, Collections.EMPTY_LIST, Collections.EMPTY_LIST);
- }
- public LocalDateTime getCreated() {
- return created;
- }
- public void setCreated(LocalDateTime created) {
- this.created = created;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- public List<Dish> getMenu() {
- return menu;
- }
- public void setMenu(List<Dish> menu) {
- this.menu = menu;
- }
- public List<Vote> getVotes() {
- return votes;
- }
- public void setVotes(List<Vote> votes) {
- this.votes = votes;
- }
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- public int getTotalMark(){
- int mark = 0;
- if (votes.size()>1)
- mark = this.votes.stream().mapToInt((v) -> v.getMark()).sum();
- return mark;
- }
- @Override
- public String toString() {
- return "Restaurant{" +
- "id=" + id +
- ", name=" + name +
- ", created=" + created +
- ", description=" + description +
- ", menu=" + menu +
- ",v otes=" + votes +
- '}';
- }
- }
- package com.technologies.Lunch_Voting.model;
- import org.hibernate.validator.constraints.NotEmpty;
- import org.hibernate.validator.constraints.Range;
- import javax.persistence.*;
- /**
- * Created by medniy on 19.01.2017.
- */
- @Entity
- @Table(name = "dishes", uniqueConstraints = {@UniqueConstraint(columnNames = {"id", "restaurant_id"}, name = "dishes_unique_rest_id_idx")})
- public class Dish extends NamedEntity{
- @Column(name = "description", nullable = false)
- @NotEmpty
- private String description;
- @Column(name = "price", nullable = false)
- @Range(min = 0, max = 50000)
- private Integer price;
- @ManyToOne(fetch= FetchType.EAGER)
- @JoinColumn(name="restaurant_id")
- private Restaurant restaurant;
- public Dish(){
- }
- public Dish(Integer id ,String name, String description, Integer price) {
- super(id, name);
- this.description = description;
- this.price = price;
- }
- public Dish(String name, String description, Integer price) {
- this(null,name,description,price);
- }
- public Integer getPrice() {
- return price;
- }
- public void setPrice(Integer price) {
- this.price = price;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- public Restaurant getRestaurant() {
- return restaurant;
- }
- public void setRestaurant(Restaurant restaurant) {
- this.restaurant = restaurant;
- }
- @Override
- public String toString() {
- return "Dish{" +
- "id=" + id +
- ", name='" + name +
- ", description='" + description +
- ", price='" + price +
- '}';
- }
- }
- public class Main {
- public static void main(String[] args) {
- try (ConfigurableApplicationContext appCtx = new ClassPathXmlApplicationContext("spring/spring_app.xml", "spring/spring_db.xml")) {
- System.out.println("Bean definition names: " + Arrays.toString(appCtx.getBeanDefinitionNames()));
- AdminRestController adminUserController = appCtx.getBean(AdminRestController.class);
- System.out.println(adminUserController.get(100001));
- VoteRestController voteRestController = appCtx.getBean(VoteRestController.class);
- System.out.println(voteRestController.get());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement