Replace Eclipselink with Hibernate
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaModel.java
index a322c16..5f72e2b 100644 (file)
@@ -1,6 +1,8 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2020,2023 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ *  Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 package org.onap.policy.models.tosca.simple.concepts;
 
 import java.util.List;
-
 import javax.persistence.CascadeType;
 import javax.persistence.Entity;
+import javax.persistence.FetchType;
 import javax.persistence.Inheritance;
 import javax.persistence.InheritanceType;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinColumns;
 import javax.persistence.OneToOne;
 import javax.persistence.Table;
-
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
-
-import org.onap.policy.common.utils.validation.Assertions;
+import org.onap.policy.common.parameters.annotations.Valid;
 import org.onap.policy.models.base.PfConcept;
 import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfKey;
 import org.onap.policy.models.base.PfModel;
 import org.onap.policy.models.base.PfModelService;
-import org.onap.policy.models.base.PfValidationResult;
 
 /**
- * A container class for a TOSCA model with multiple service templates. This class is a container
- * class that allows a model with many service templates to be constructed that contains a well
- * formed overall TOSCA model.
+ * A container class for a TOSCA model with multiple service templates. This class is a container class that allows a
+ * model with many service templates to be constructed that contains a well formed overall TOSCA model.
  *
  * <p>Validation runs {@link JpaToscaModel} validation on the model and all its sub concepts.
  */
@@ -57,20 +57,25 @@ import org.onap.policy.models.base.PfValidationResult;
 public class JpaToscaModel extends PfModel {
     private static final long serialVersionUID = 8800599637708309945L;
 
-    @OneToOne(cascade = CascadeType.ALL)
+    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
+    @JoinColumns({
+        @JoinColumn(name = "serviceTemplatesName", referencedColumnName = "name"),
+        @JoinColumn(name = "serviceTemplatesVersion", referencedColumnName = "version")
+    })
+    @Valid
     private JpaToscaServiceTemplates serviceTemplates;
 
     /**
-     * The Default Constructor creates a {@link JpaToscaModel} object with a null concept key and
-     * creates an empty TOSCA model.
+     * The Default Constructor creates a {@link JpaToscaModel} object with a null concept key and creates an empty TOSCA
+     * model.
      */
     public JpaToscaModel() {
         this(new PfConceptKey());
     }
 
     /**
-     * The Key Constructor creates a {@link JpaToscaModel} object with the given concept key and
-     * creates an empty TOSCA model.
+     * The Key Constructor creates a {@link JpaToscaModel} object with the given concept key and creates an empty TOSCA
+     * model.
      *
      * @param key the TOSCA model key
      */
@@ -96,6 +101,7 @@ public class JpaToscaModel extends PfModel {
      */
     public JpaToscaModel(@NonNull final JpaToscaModel copyConcept) {
         super(copyConcept);
+        this.serviceTemplates = new JpaToscaServiceTemplates(copyConcept.serviceTemplates);
     }
 
     @Override
@@ -118,13 +124,6 @@ public class JpaToscaModel extends PfModel {
         serviceTemplates.clean();
     }
 
-    @Override
-    public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
-        PfValidationResult result = super.validate(resultIn);
-
-        return serviceTemplates.validate(result);
-    }
-
     @Override
     public int compareTo(final PfConcept otherConcept) {
         if (otherConcept == null) {
@@ -136,25 +135,15 @@ public class JpaToscaModel extends PfModel {
         }
 
         if (getClass() != otherConcept.getClass()) {
-            return this.hashCode() - otherConcept.hashCode();
+            return getClass().getName().compareTo(otherConcept.getClass().getName());
         }
 
         final JpaToscaModel other = (JpaToscaModel) otherConcept;
-        if (!super.equals(other)) {
-            return super.compareTo(other);
+        int result = super.compareTo(other);
+        if (result != 0) {
+            return result;
         }
 
         return serviceTemplates.compareTo(other.serviceTemplates);
     }
-
-    @Override
-    public PfConcept copyTo(@NonNull final PfConcept targetObject) {
-        Assertions.instanceOf(targetObject, JpaToscaModel.class);
-
-        final JpaToscaModel copy = ((JpaToscaModel) targetObject);
-        super.copyTo(targetObject);
-        copy.setServiceTemplates(new JpaToscaServiceTemplates(serviceTemplates));
-
-        return copy;
-    }
 }