diff options
author | 2025-02-17 18:32:32 +0000 | |
---|---|---|
committer | 2025-02-17 18:32:32 +0000 | |
commit | 95ccf6ea91a5b995706302e2ef54a69151e8d87e (patch) | |
tree | abbea34b7025fdbf6812100bca0e30a6751331e4 | |
parent | 68f577fc89e430492cac762e6d295fa6072409d1 (diff) | |
parent | 25e2fab51edd290b0ca36f320fe0a4438667d318 (diff) |
Merge "Fix CpsSessionFactory for it to be used in prod code"
-rw-r--r-- | cps-ri/src/main/java/org/onap/cps/ri/utils/CpsSessionFactory.java | 26 | ||||
-rw-r--r-- | cps-ri/src/main/resources/hibernate.cfg.xml | 16 | ||||
-rw-r--r-- | integration-test/src/test/resources/hibernate.cfg.xml | 16 |
3 files changed, 6 insertions, 52 deletions
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 @@ -<?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 index 8d5139b605..0000000000 --- a/integration-test/src/test/resources/hibernate.cfg.xml +++ /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 |