From 25e2fab51edd290b0ca36f320fe0a4438667d318 Mon Sep 17 00:00:00 2001 From: mpriyank Date: Thu, 13 Feb 2025 11:16:44 +0000 Subject: Fix CpsSessionFactory for it to be used in prod code - 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 --- .../org/onap/cps/ri/utils/CpsSessionFactory.java | 26 +++++----------------- cps-ri/src/main/resources/hibernate.cfg.xml | 16 ------------- 2 files changed, 6 insertions(+), 36 deletions(-) delete mode 100644 cps-ri/src/main/resources/hibernate.cfg.xml (limited to 'cps-ri/src') diff --git a/cps-ri/src/main/java/org/onap/cps/ri/utils/CpsSessionFactory.java b/cps-ri/src/main/java/org/onap/cps/ri/utils/CpsSessionFactory.java index c0291176f4..d4cd366157 100644 --- a/cps-ri/src/main/java/org/onap/cps/ri/utils/CpsSessionFactory.java +++ b/cps-ri/src/main/java/org/onap/cps/ri/utils/CpsSessionFactory.java @@ -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. @@ -20,22 +20,20 @@ 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 index 1b822b9de9..0000000000 --- a/cps-ri/src/main/resources/hibernate.cfg.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - org.postgresql.Driver - jdbc:postgresql://${DB_HOST}:${DB_PORT:5432}/cpsdb - ${DB_USERNAME} - ${DB_PASSWORD} - org.hibernate.dialect.PostgreSQLDialect - true - update - - \ No newline at end of file -- cgit