Dumbass229

Untitled

Nov 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1. вот класс который описывает промежуточную  таблицу для отношения (многие ко многим) юзер и сервис .............
  2. package mytech.dao.entities.customers;
  3.  
  4.  
  5.  
  6. @Entity
  7. @Table(name = "user_service" )
  8. @IdClass(UserServiceIds.class)
  9. public class UserServices implements Serializable {
  10.  
  11.     @Column(name = "service_id")
  12.     private long serviceId;
  13.     @Column(name = "user_id")
  14.     private long userId;
  15.     @Column(name = "duration")
  16.     private Timestamp duration;
  17.     @Column(name = "is_active")
  18.     private boolean isActive;
  19.     //relations
  20.     /* 1 relation and  2 relation provide a (many to many)
  21.     relation between (user and service)*/
  22.     //--1 relation
  23.     @Id
  24.     @ManyToOne
  25.     @JoinColumn(name = "service_id", nullable = false, insertable = false, updatable = false)
  26.     private Services service;
  27.     //--2relation
  28.     @Id
  29.     @ManyToOne
  30.     @JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false)
  31.     private Users user;
  32.  
  33.     //relations
  34.     public UserServices(Users user, Services service) {
  35.         this.service = service;
  36.         this.user = user;
  37.     }
  38.  
  39.     public UserServices() {
  40.     }
  41.  
  42.     public Services getService() {
  43.         return service;
  44.     }
  45.  
  46.     public long getServiceId() {
  47.         return serviceId;
  48.     }
  49.  
  50.     public long getUserId() {
  51.         return userId;
  52.     }
  53.  
  54.     public Timestamp getDuration() {
  55.         return duration;
  56.     }
  57.  
  58.     public boolean isActive() {
  59.         return isActive;
  60.     }
  61.  
  62.     public UserServices setServiceId(long serviceId) {
  63.         this.serviceId = serviceId;
  64.         return this;
  65.     }
  66.  
  67.     public  UserServices setUserId(long userId) {
  68.         this.userId = userId;
  69.         return this;
  70.     }
  71.  
  72.     public  UserServices setDuration(Timestamp duration) {
  73.         this.duration = duration;
  74.         return this;
  75.     }
  76.  
  77.     public  UserServices setActive(boolean active) {
  78.         isActive = active;
  79.         return this;
  80.     }
  81.  
  82.     public Users getUser() {
  83.         return user;
  84.     }
  85.  
  86.     @Override
  87.     public boolean equals(Object o) {
  88.         if (this == o) return true;
  89.         if (!(o instanceof UserServices)) return false;
  90.         UserServices that = (UserServices) o;
  91.         return serviceId == that.serviceId &&
  92.                 userId == that.userId;
  93.     }
  94.  
  95.     @Override
  96.     public int hashCode() {
  97.         return Objects.hash(serviceId, userId);
  98.     }
  99. }
  100. вот класс айди........................................................................
  101. package mytech.dao.entities.customers;
  102.  
  103. import java.io.Serializable;
  104. import java.util.Objects;
  105.  
  106. /*this class exists for  UserService.class to implement a many to many relation
  107. between Service and User */
  108. public class UserServiceIds implements Serializable {
  109.     private Users user;
  110.     private Services service;
  111.  
  112.     @Override
  113.    public boolean equals(Object object) {
  114.         if (object instanceof UserServiceIds == false) return false;
  115.         UserServiceIds userServiceIds = (UserServiceIds) object;
  116.         if ((userServiceIds.getService().getId() == this.getService().getId())
  117.                 && (userServiceIds.getUser().getId() == this.getUser().getId())) return true;
  118.         return false;
  119.     }
  120.  
  121.     @Override
  122.     public int hashCode() {
  123.         return Objects.hash(user.getId(), service.getId());//SHOULD TO REWRITE
  124.     }
  125.  
  126.     public Users getUser() {
  127.         return user;
  128.     }
  129.  
  130.     public Services getService() {
  131.         return service;
  132.     }
  133.  
  134.     public UserServiceIds(Users user, Services service) {
  135.         this.user = user;
  136.         this.service = service;
  137.     }
  138.  
  139.     public UserServiceIds() {
  140.     }
  141. }
Add Comment
Please, Sign In to add comment