From e16a11f4ec5a243c6753c7296b5faa3d46c9ba7c Mon Sep 17 00:00:00 2001 From: Vitaly Litvak Date: Tue, 28 Apr 2015 00:53:15 +0300 Subject: [PATCH] For #150 - updated equals() and hashCode() methods of entities classes so they will work properly with hibernate proxies, changed collections in 'User' entity from Lists (bag was actually used) to Set --- .../org/traccar/web/shared/model/Device.java | 6 ++--- .../traccar/web/shared/model/DeviceEvent.java | 22 +++++++++---------- .../traccar/web/shared/model/GeoFence.java | 14 ++++++------ .../shared/model/NotificationTemplate.java | 6 ++--- .../org/traccar/web/shared/model/User.java | 20 ++++++++--------- .../traccar/web/shared/model/UserTest.java | 12 +++++----- 6 files changed, 39 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/traccar/web/shared/model/Device.java b/src/main/java/org/traccar/web/shared/model/Device.java index 890d337e..b317678f 100644 --- a/src/main/java/org/traccar/web/shared/model/Device.java +++ b/src/main/java/org/traccar/web/shared/model/Device.java @@ -173,17 +173,17 @@ public void setIconType(DeviceIconType iconType) { @Override public boolean equals(Object o) { if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (o == null || !(o instanceof Device)) return false; Device device = (Device) o; - if (uniqueId != null ? !uniqueId.equals(device.uniqueId) : device.uniqueId != null) return false; + if (getUniqueId() != null ? !getUniqueId().equals(device.getUniqueId()) : device.getUniqueId() != null) return false; return true; } @Override public int hashCode() { - return uniqueId != null ? uniqueId.hashCode() : 0; + return getUniqueId() != null ? getUniqueId().hashCode() : 0; } } diff --git a/src/main/java/org/traccar/web/shared/model/DeviceEvent.java b/src/main/java/org/traccar/web/shared/model/DeviceEvent.java index 7367e908..5725566d 100644 --- a/src/main/java/org/traccar/web/shared/model/DeviceEvent.java +++ b/src/main/java/org/traccar/web/shared/model/DeviceEvent.java @@ -118,26 +118,26 @@ public void setNotificationSent(boolean notificationSent) { @Override public boolean equals(Object o) { if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (o == null || !(o instanceof DeviceEvent)) return false; DeviceEvent that = (DeviceEvent) o; - if (device != null ? !device.equals(that.device) : that.device != null) return false; - if (geoFence != null ? !geoFence.equals(that.geoFence) : that.geoFence != null) return false; - if (position != null ? !position.equals(that.position) : that.position != null) return false; - if (!time.equals(that.time)) return false; - if (type != that.type) return false; + if (getDevice() != null ? !getDevice().equals(that.getDevice()) : that.getDevice() != null) return false; + if (getGeoFence() != null ? !getGeoFence().equals(that.getGeoFence()) : that.getGeoFence() != null) return false; + if (getPosition() != null ? !getPosition().equals(that.getPosition()) : that.getPosition() != null) return false; + if (!getTime().equals(that.getTime())) return false; + if (getType() != that.getType()) return false; return true; } @Override public int hashCode() { - int result = time.hashCode(); - result = 31 * result + (device != null ? device.hashCode() : 0); - result = 31 * result + type.hashCode(); - result = 31 * result + (position != null ? position.hashCode() : 0); - result = 31 * result + (geoFence != null ? geoFence.hashCode() : 0); + int result = getTime().hashCode(); + result = 31 * result + (getDevice() != null ? getDevice().hashCode() : 0); + result = 31 * result + getType().hashCode(); + result = 31 * result + (getPosition() != null ? getPosition().hashCode() : 0); + result = 31 * result + (getGeoFence() != null ? getGeoFence().hashCode() : 0); return result; } } diff --git a/src/main/java/org/traccar/web/shared/model/GeoFence.java b/src/main/java/org/traccar/web/shared/model/GeoFence.java index 56e98744..7ee181fb 100644 --- a/src/main/java/org/traccar/web/shared/model/GeoFence.java +++ b/src/main/java/org/traccar/web/shared/model/GeoFence.java @@ -176,22 +176,22 @@ public void setTransferDevices(Set transferDevices) { @Override public boolean equals(Object o) { if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (o == null || !(o instanceof GeoFence)) return false; GeoFence geoFence = (GeoFence) o; - if (id != geoFence.id) return false; - if (name != null ? !name.equals(geoFence.name) : geoFence.name != null) return false; - if (type != geoFence.type) return false; + if (getId() != geoFence.getId()) return false; + if (getName() != null ? !getName().equals(geoFence.getName()) : geoFence.getName() != null) return false; + if (getType() != geoFence.getType()) return false; return true; } @Override public int hashCode() { - int result = (int) (id ^ (id >>> 32)); - result = 31 * result + (name != null ? name.hashCode() : 0); - result = 31 * result + (type != null ? type.hashCode() : 0); + int result = (int) (getId() ^ (getId() >>> 32)); + result = 31 * result + (getName() != null ? getName().hashCode() : 0); + result = 31 * result + (getType() != null ? getType().hashCode() : 0); return result; } diff --git a/src/main/java/org/traccar/web/shared/model/NotificationTemplate.java b/src/main/java/org/traccar/web/shared/model/NotificationTemplate.java index 55b4995e..1df4c5e4 100644 --- a/src/main/java/org/traccar/web/shared/model/NotificationTemplate.java +++ b/src/main/java/org/traccar/web/shared/model/NotificationTemplate.java @@ -68,12 +68,12 @@ public void setType(DeviceEventType type) { @Override public boolean equals(Object o) { if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (o == null || !(o instanceof NotificationTemplate)) return false; NotificationTemplate that = (NotificationTemplate) o; - if (!settings.equals(that.settings)) return false; - return type == that.type; + if (!getSettings().equals(that.getSettings())) return false; + return getType() == that.getType(); } diff --git a/src/main/java/org/traccar/web/shared/model/User.java b/src/main/java/org/traccar/web/shared/model/User.java index feaadb31..840f329f 100644 --- a/src/main/java/org/traccar/web/shared/model/User.java +++ b/src/main/java/org/traccar/web/shared/model/User.java @@ -16,8 +16,6 @@ package org.traccar.web.shared.model; import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; import java.util.Set; import javax.persistence.*; @@ -136,13 +134,13 @@ public void setManager(Boolean manager) { foreignKey = @ForeignKey(name = "users_devices_fkey_users_id"), joinColumns = { @JoinColumn(name = "users_id", table = "users", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "devices_id", table = "devices", referencedColumnName = "id") }) - private List devices = new LinkedList(); + private Set devices = new HashSet(); - public void setDevices(List devices) { + public void setDevices(Set devices) { this.devices = devices; } - public List getDevices() { + public Set getDevices() { return devices; } @@ -163,13 +161,13 @@ public Set getAllAvailableDevices() { foreignKey = @ForeignKey(name = "users_geofences_fkey_user_id"), joinColumns = { @JoinColumn(name = "user_id", table = "users", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "geofence_id", table = "geofences", referencedColumnName = "id") }) - private List geoFences; + private Set geoFences; - public void setGeoFences(List geoFences) { + public void setGeoFences(Set geoFences) { this.geoFences = geoFences; } - public List getGeoFences() { + public Set getGeoFences() { return geoFences; } @@ -333,17 +331,17 @@ public void setReadOnly(boolean readOnly) { @Override public boolean equals(Object o) { if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (o == null || !(o instanceof User)) return false; User user = (User) o; - if (login != null ? !login.equals(user.login) : user.login != null) return false; + if (getLogin() != null ? !getLogin().equals(user.getLogin()) : user.getLogin() != null) return false; return true; } @Override public int hashCode() { - return login != null ? login.hashCode() : 0; + return getLogin() != null ? getLogin().hashCode() : 0; } } diff --git a/src/test/java/org/traccar/web/shared/model/UserTest.java b/src/test/java/org/traccar/web/shared/model/UserTest.java index ac7058b2..abec6d20 100644 --- a/src/test/java/org/traccar/web/shared/model/UserTest.java +++ b/src/test/java/org/traccar/web/shared/model/UserTest.java @@ -72,12 +72,12 @@ public void testAvailableGeoFences() { GeoFence g6 = new GeoFence(6, "g6"); GeoFence g7 = new GeoFence(7, "g7"); - m1.setGeoFences(Arrays.asList(g1, g7)); - m2.setGeoFences(Arrays.asList(g1, g2)); - m3.setGeoFences(Arrays.asList(g5)); - u1.setGeoFences(Arrays.asList(g3)); - u2.setGeoFences(Arrays.asList(g4)); - u3.setGeoFences(Arrays.asList(g6)); + m1.setGeoFences(new HashSet(Arrays.asList(g1, g7))); + m2.setGeoFences(new HashSet(Arrays.asList(g1, g2))); + m3.setGeoFences(new HashSet(Arrays.asList(g5))); + u1.setGeoFences(new HashSet(Arrays.asList(g3))); + u2.setGeoFences(new HashSet(Arrays.asList(g4))); + u3.setGeoFences(new HashSet(Arrays.asList(g6))); // test assertEquals(set(g1, g2, g3, g4, g5, g6, g7), m1.getAllAvailableGeoFences());