Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- вот класс который описывает промежуточную таблицу для отношения (многие ко многим) юзер и сервис .............
- package mytech.dao.entities.customers;
- @Entity
- @Table(name = "user_service" )
- @IdClass(UserServiceIds.class)
- public class UserServices implements Serializable {
- @Column(name = "service_id")
- private long serviceId;
- @Column(name = "user_id")
- private long userId;
- @Column(name = "duration")
- private Timestamp duration;
- @Column(name = "is_active")
- private boolean isActive;
- //relations
- /* 1 relation and 2 relation provide a (many to many)
- relation between (user and service)*/
- //--1 relation
- @Id
- @ManyToOne
- @JoinColumn(name = "service_id", nullable = false, insertable = false, updatable = false)
- private Services service;
- //--2relation
- @Id
- @ManyToOne
- @JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false)
- private Users user;
- //relations
- public UserServices(Users user, Services service) {
- this.service = service;
- this.user = user;
- }
- public UserServices() {
- }
- public Services getService() {
- return service;
- }
- public long getServiceId() {
- return serviceId;
- }
- public long getUserId() {
- return userId;
- }
- public Timestamp getDuration() {
- return duration;
- }
- public boolean isActive() {
- return isActive;
- }
- public UserServices setServiceId(long serviceId) {
- this.serviceId = serviceId;
- return this;
- }
- public UserServices setUserId(long userId) {
- this.userId = userId;
- return this;
- }
- public UserServices setDuration(Timestamp duration) {
- this.duration = duration;
- return this;
- }
- public UserServices setActive(boolean active) {
- isActive = active;
- return this;
- }
- public Users getUser() {
- return user;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof UserServices)) return false;
- UserServices that = (UserServices) o;
- return serviceId == that.serviceId &&
- userId == that.userId;
- }
- @Override
- public int hashCode() {
- return Objects.hash(serviceId, userId);
- }
- }
- вот класс айди........................................................................
- package mytech.dao.entities.customers;
- import java.io.Serializable;
- import java.util.Objects;
- /*this class exists for UserService.class to implement a many to many relation
- between Service and User */
- public class UserServiceIds implements Serializable {
- private Users user;
- private Services service;
- @Override
- public boolean equals(Object object) {
- if (object instanceof UserServiceIds == false) return false;
- UserServiceIds userServiceIds = (UserServiceIds) object;
- if ((userServiceIds.getService().getId() == this.getService().getId())
- && (userServiceIds.getUser().getId() == this.getUser().getId())) return true;
- return false;
- }
- @Override
- public int hashCode() {
- return Objects.hash(user.getId(), service.getId());//SHOULD TO REWRITE
- }
- public Users getUser() {
- return user;
- }
- public Services getService() {
- return service;
- }
- public UserServiceIds(Users user, Services service) {
- this.user = user;
- this.service = service;
- }
- public UserServiceIds() {
- }
- }
Add Comment
Please, Sign In to add comment