/*
* ============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.
* @throws HibernateException hibernate exception
*/
public Session openSession() throws HibernateException {
- return getSessionFactory().openSession();
+ return sessionFactory.openSession();
}
/**
* @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();
}
}
+++ /dev/null
-<?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
+++ /dev/null
-<?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