]> 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 65a40c8bf26959643e4a0562e56a23e79217e8a7..dd7bd44d5106162170c48e025a09fceaeecc072f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Roland Haeder
+ * Copyright (C) 2016 Roland Haeder
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -18,6 +18,7 @@ package org.mxchange.jshopcore.model.category;
 
 import java.util.Objects;
 import javax.persistence.Basic;
+import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
@@ -29,12 +30,12 @@ import javax.persistence.Table;
 
 /**
  * A product category
- *
+ * <p>
  * @author Roland Haeder<roland@mxchange.org>
  */
 @Entity (name = "category")
 @Table (name = "category")
-public class ProductCategory implements Category, Comparable<Category> {
+public class ProductCategory implements Category {
 
        /**
         * Serial number
@@ -46,34 +47,34 @@ 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;
 
        /**
-        * Parent category categoryId
+        * Title of category
         */
-       @JoinColumn (name = "parent_id")
-       @OneToOne (targetEntity = ProductCategory.class)
-       private Category parentCategory;
+       @Basic (optional = false)
+       @Column (name = "category_title", length = 100, nullable = false, unique = true)
+       private String categoryTitle;
 
        /**
-        * Title of category
+        * Parent category
         */
-       @Basic (optional = false)
-       @Column (name = "title", length = 100, nullable = false, unique = true)
-       private String title;
+       @JoinColumn (name = "parent_id")
+       @OneToOne (targetEntity = ProductCategory.class, cascade = CascadeType.REFRESH)
+       private Category parentCategory;
 
        /**
         * Constructor which accepts all database fields
-        *
-        * @param categoryId Id number of database record
-        * @param title Category title
+        * <p>
+        * @param categoryId     Id number of database record
+        * @param categoryTitle  Category categoryTitle
         * @param parentCategory Parent category
         */
-       public ProductCategory (final Long categoryId, final String title, final Category parentCategory) {
+       public ProductCategory (final Long categoryId, final String categoryTitle, final Category parentCategory) {
                // Set all here
                this.categoryId = categoryId;
-               this.title = title;
+               this.categoryTitle = categoryTitle;
                this.parentCategory = parentCategory;
        }
 
@@ -84,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.setTitle(category.getTitle());
+       public int hashCode () {
+               int hash = 7;
+               hash = 13 * hash + Objects.hashCode(this.getCategoryId());
+               hash = 13 * hash + Objects.hashCode(this.getCategoryTitle());
+               return hash;
        }
 
        @Override
@@ -121,22 +131,23 @@ public class ProductCategory implements Category, Comparable<Category> {
        }
 
        @Override
-       public Category getParentCategory () {
-               return this.parentCategory;
+       public String getCategoryTitle () {
+               return this.categoryTitle;
        }
 
        @Override
-       public void setParentCategory (final Category parentCategory) {
-               this.parentCategory = parentCategory;
+       public void setCategoryTitle (final String categoryTitle) {
+               this.categoryTitle = categoryTitle;
        }
 
        @Override
-       public String getTitle () {
-               return this.title;
+       public Category getParentCategory () {
+               return this.parentCategory;
        }
 
        @Override
-       public void setTitle (final String title) {
-               this.title = title;
+       public void setParentCategory (final Category parentCategory) {
+               this.parentCategory = parentCategory;
        }
+
 }