]> git.mxchange.org Git - jproduct-core.git/blobdiff - src/org/mxchange/jshopcore/model/category/ProductCategory.java
added missing equals() hashCode() methods as this is needed for JPA to work correctly
[jproduct-core.git] / src / org / mxchange / jshopcore / model / category / ProductCategory.java
index 721e638fef6e124c198b8e8a7bf4e723a71f451f..dd7bd44d5106162170c48e025a09fceaeecc072f 100644 (file)
@@ -35,7 +35,7 @@ import javax.persistence.Table;
  */
 @Entity (name = "category")
 @Table (name = "category")
-public class ProductCategory implements Category, Comparable<Category> {
+public class ProductCategory implements Category {
 
        /**
         * Serial number
@@ -47,7 +47,7 @@ public class ProductCategory implements Category, Comparable<Category> {
         */
        @Id
        @GeneratedValue (strategy = GenerationType.IDENTITY)
-       @Column (name = "category_id", length = 20, nullable = false)
+       @Column (name = "category_id", nullable = false)
        private Long categoryId;
 
        /**
@@ -61,14 +61,14 @@ public class ProductCategory implements Category, Comparable<Category> {
         * Parent category
         */
        @JoinColumn (name = "parent_id")
-       @OneToOne (targetEntity = ProductCategory.class, cascade = CascadeType.MERGE)
+       @OneToOne (targetEntity = ProductCategory.class, cascade = CascadeType.REFRESH)
        private Category parentCategory;
 
        /**
         * Constructor which accepts all database fields
         * <p>
-        * @param categoryId Id number of database record
-        * @param categoryTitle Category categoryTitle
+        * @param categoryId     Id number of database record
+        * @param categoryTitle  Category categoryTitle
         * @param parentCategory Parent category
         */
        public ProductCategory (final Long categoryId, final String categoryTitle, final Category parentCategory) {
@@ -85,30 +85,39 @@ public class ProductCategory implements Category, Comparable<Category> {
        }
 
        @Override
-       public int compareTo (final Category category) {
-               // category should not be null
-               if (null == category) {
-                       throw new NullPointerException("category is null"); //NOI18N
+       public void copyAll (final Category category) {
+               // Copy all data
+               this.setParentCategory(category.getParentCategory());
+               this.setCategoryTitle(category.getCategoryTitle());
+       }
+
+       @Override
+       public boolean equals (final Object object) {
+               if (this == object) {
+                       return true;
+               } else if (null == object) {
+                       return false;
+               } else if (this.getClass() != object.getClass()) {
+                       return false;
                }
 
-               // Is the categoryId the same?
-               if (Objects.equals(this.getCategoryId(), category.getCategoryId())) {
-                       // Same categoryId, means same category
-                       return 0;
-               } else if (this.getCategoryId() > category.getCategoryId()) {
-                       // This categoryId is larger than compared to
-                       return -1;
+               final Category other = (Category) object;
+
+               if (!Objects.equals(this.getCategoryTitle(), other.getCategoryTitle())) {
+                       return false;
+               } else if (!Objects.equals(this.getCategoryId(), other.getCategoryId())) {
+                       return false;
                }
 
-               // The other categoryId is larger
-               return 1;
+               return true;
        }
 
        @Override
-       public void copyAll (final Category category) {
-               // Copy all data
-               this.setParentCategory(category.getParentCategory());
-               this.setCategoryTitle(category.getCategoryTitle());
+       public int hashCode () {
+               int hash = 7;
+               hash = 13 * hash + Objects.hashCode(this.getCategoryId());
+               hash = 13 * hash + Objects.hashCode(this.getCategoryTitle());
+               return hash;
        }
 
        @Override
@@ -140,4 +149,5 @@ public class ProductCategory implements Category, Comparable<Category> {
        public void setParentCategory (final Category parentCategory) {
                this.parentCategory = parentCategory;
        }
+
 }