Fix CpsSessionFactory for it to be used in prod code 18/140218/4
authormpriyank <priyank.maheshwari@est.tech>
Thu, 13 Feb 2025 11:16:44 +0000 (11:16 +0000)
committermpriyank <priyank.maheshwari@est.tech>
Mon, 17 Feb 2025 10:18:52 +0000 (10:18 +0000)
- Injecting SessionFactory bean from the spring container instead of
  manually creating it.
- Removing explicit addition of Annotated class as anyways the classes
  are Entity classes and added to the metadata. So no need to add it
  explicitly
- also removing the unused hibernate config file

Issue-ID: CPS-2629
Change-Id: Ife2ae3828d009893de70d9c755d6a07cef2f8d07
Signed-off-by: mpriyank <priyank.maheshwari@est.tech>
cps-ri/src/main/java/org/onap/cps/ri/utils/CpsSessionFactory.java
cps-ri/src/main/resources/hibernate.cfg.xml [deleted file]
integration-test/src/test/resources/hibernate.cfg.xml [deleted file]

index c029117..d4cd366 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2022 Nordix Foundation
+ *  Copyright (C) 2022-2025 Nordix Foundation
  *  ================================================================================
  *  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.cps.ri.utils;
 
+import lombok.RequiredArgsConstructor;
 import org.hibernate.HibernateException;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
-import org.onap.cps.ri.models.AnchorEntity;
-import org.onap.cps.ri.models.DataspaceEntity;
-import org.onap.cps.ri.models.SchemaSetEntity;
-import org.onap.cps.ri.models.YangResourceEntity;
 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
 import org.springframework.context.annotation.Scope;
 import org.springframework.stereotype.Component;
 
 @Component
 @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
+@RequiredArgsConstructor
 public class CpsSessionFactory {
 
-    private SessionFactory sessionFactory = null;
+    private final SessionFactory sessionFactory;
 
     /**
      * Open a session from session factory.
@@ -44,7 +42,7 @@ public class CpsSessionFactory {
      * @throws HibernateException hibernate exception
      */
     public Session openSession() throws HibernateException {
-        return getSessionFactory().openSession();
+        return sessionFactory.openSession();
     }
 
     /**
@@ -53,18 +51,6 @@ public class CpsSessionFactory {
      * @throws HibernateException hibernate exception
      */
     public void closeSessionFactory() throws HibernateException {
-        getSessionFactory().close();
-    }
-
-    private SessionFactory getSessionFactory() {
-        if (sessionFactory == null) {
-            sessionFactory = new org.hibernate.cfg.Configuration().configure("hibernate.cfg.xml")
-                    .addAnnotatedClass(AnchorEntity.class)
-                    .addAnnotatedClass(DataspaceEntity.class)
-                    .addAnnotatedClass(SchemaSetEntity.class)
-                    .addAnnotatedClass(YangResourceEntity.class)
-                    .buildSessionFactory();
-        }
-        return sessionFactory;
+        sessionFactory.close();
     }
 }
diff --git a/cps-ri/src/main/resources/hibernate.cfg.xml b/cps-ri/src/main/resources/hibernate.cfg.xml
deleted file mode 100644 (file)
index 1b822b9..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE hibernate-configuration PUBLIC
-        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
-        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
-
-<hibernate-configuration>
-    <session-factory>
-        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
-        <property name="hibernate.connection.url">jdbc:postgresql://${DB_HOST}:${DB_PORT:5432}/cpsdb</property>
-        <property name="hibernate.connection.username">${DB_USERNAME}</property>
-        <property name="hibernate.connection.password">${DB_PASSWORD}</property>
-        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
-        <property name="show_sql">true</property>
-        <property name="hibernate.hbm2ddl.auto">update</property>
-    </session-factory>
-</hibernate-configuration>
\ No newline at end of file
diff --git a/integration-test/src/test/resources/hibernate.cfg.xml b/integration-test/src/test/resources/hibernate.cfg.xml
deleted file mode 100644 (file)
index 8d5139b..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE hibernate-configuration PUBLIC
-        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
-        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
-
-<hibernate-configuration>
-    <session-factory>
-        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
-        <property name="hibernate.connection.url">${DB_URL}</property>
-        <property name="hibernate.connection.username">${DB_USERNAME}</property>
-        <property name="hibernate.connection.password">${DB_PASSWORD}</property>
-        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
-        <property name="show_sql">true</property>
-        <property name="hibernate.hbm2ddl.auto">none</property>
-    </session-factory>
-</hibernate-configuration>
\ No newline at end of file