Como altero essa classe para não usar o arquivo persistence.xml?

// Qual alteração preciso fazer para não utilizar arquivo xml para configurar
// ele está ainda utilizando o arquivo persistence.xml dentro do método getHibernateSession
// não quero usar arquivos xml, somente código entro da classe
package erp.arquitetura;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import erp.arquitetura.gui.Msg;

public class Jpa {

	private static final EntityManagerFactory emf;

	static {
		try {
			emf = Persistence.createEntityManagerFactory("erp");

		} catch (Exception ex) {
			Msg.erroConectarDataBase();
			ex.printStackTrace();
			throw new ExceptionInInitializerError(ex);
		}
	}	

	public static Session getHibernateSession() {

		// O que devo alterar dentro desse método para não precisar de usar o arquivo persistence.xml?
		final SessionFactory sf = new Configuration().configure("persistence.xml").buildSessionFactory();

		// factory = new Configuration().configure().buildSessionFactory();
		final Session session = sf.openSession();
		return session;
	}
	
    private static final String PERSISTENCE_UNIT_NAME = "erp"; // Nome da unidade de persistência

    public static EntityManagerFactory getEntityManagerFactory() {
        Properties properties = new Properties();        
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
        properties.setProperty("hibernate.autocommit", "true");
        properties.setProperty("hibernate.connection.autocommit", "true");
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        properties.setProperty("javax.persistence.jdbc.driver", "org.hsqldb.jdbcDriver");
        properties.setProperty("javax.persistence.jdbc.user", "sa");
        properties.setProperty("javax.persistence.jdbc.password", "");
        properties.setProperty("javax.persistence.jdbc.url", "jdbc:hsqldb:file:db-despachante");
        properties.setProperty("hibernate.connection.CharSet", "utf8");
        properties.setProperty("hibernate.connection.characterEncoding", "utf8");
        properties.setProperty("hibernate.connection.useUnicode", "true");
        properties.setProperty("hibernate.show_sql", "true");
        properties.setProperty("hibernate.format_sql", "false");
        properties.setProperty("hibernate.use_sql_comments", "true");
        properties.setProperty("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
        properties.setProperty("hibernate.c3p0.max_size", "50");
        properties.setProperty("hibernate.c3p0.min_size", "2");
        properties.setProperty("hibernate.c3p0.acquire_increment", "1");
        properties.setProperty("hibernate.c3p0.idle_test_period", "3000");
        properties.setProperty("hibernate.c3p0.timeout", "600");
        properties.setProperty("hibernate.c3p0.max_statements", "20");

        return Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, properties);
    }
}

Fiz algumas alterações na classe porém dá esse erro:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named erp
segue abaixo alterações

package erp.arquitetura;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import erp.arquitetura.gui.Msg;

public class Jpa {

	private static final EntityManagerFactory emf;
	private static final Configuration configuration = new Configuration();

	static {
		try {
			configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
			configuration.setProperty("hibernate.autocommit", "true");
			configuration.setProperty("hibernate.connection.autocommit", "true");
			configuration.setProperty("hibernate.hbm2ddl.auto", "update");
			configuration.setProperty("javax.persistence.jdbc.driver", "org.hsqldb.jdbcDriver");
			configuration.setProperty("javax.persistence.jdbc.user", "sa");
			configuration.setProperty("javax.persistence.jdbc.password", "");
			configuration.setProperty("javax.persistence.jdbc.url", "jdbc:hsqldb:file:db-despachante");
			configuration.setProperty("hibernate.connection.CharSet", "utf8");
			configuration.setProperty("hibernate.connection.characterEncoding", "utf8");
			configuration.setProperty("hibernate.connection.useUnicode", "true");
			configuration.setProperty("hibernate.show_sql", "true");
			configuration.setProperty("hibernate.format_sql", "false");
			configuration.setProperty("hibernate.use_sql_comments", "true");
			configuration.setProperty("hibernate.connection.provider_class",
					"org.hibernate.connection.C3P0ConnectionProvider");
			configuration.setProperty("hibernate.c3p0.max_size", "50");
			configuration.setProperty("hibernate.c3p0.min_size", "2");
			configuration.setProperty("hibernate.c3p0.acquire_increment", "1");
			configuration.setProperty("hibernate.c3p0.idle_test_period", "3000");
			configuration.setProperty("hibernate.c3p0.timeout", "600");
			configuration.setProperty("hibernate.c3p0.max_statements", "20");

			emf = Persistence.createEntityManagerFactory("erp");

		} catch (Exception ex) {
			Msg.erroConectarDataBase();
			ex.printStackTrace();
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static Session getHibernateSession() {

		// O que devo alterar dentro desse método para não precisar de usar o arquivo
		// persistence.xml?
		final SessionFactory sf = configuration.buildSessionFactory();

		// factory = new Configuration().configure().buildSessionFactory();
		final Session session = sf.openSession();
		return session;
	}

	public static EntityManagerFactory getEntityManagerFactory() {

		return emf;
	}
}

Curioso pq nunca tinha pensado que seria possível fazer isso, mas pesquisando aqui parece que tem: https://www.baeldung.com/java-bootstrap-jpa.

1 curtida