How to initialize persistence.xml at the beginning of the program?

Asked

Viewed 75 times

0

I’m making a insert within a Postgresql database, however, as I have to test what connection to the database I will make, I end up having to configure the access based on each environment.

The problem is that I did it in a not very good way, since for each insertion I set up the bank again.

How can I load this setting only once, not at each insertion? I can’t leave the password hardcoded in the code as well.

What I’m currently doing:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class ConteudoService extends AbstractService {
@Autowired
private ConteudoRepository conteudoRepository;

public EntityTransaction insertInDb(String something, String somethingElse) {

    Map<String, String> env = System.getenv();
    Map<String, Object> configOverrides = new HashMap<>();

    // Trocando as configurações do persistance.xml
    for(Map.Entry<String, String> entry: env.entrySet()) {
        if (entry.getKey().contains("DATABASE_URL")) {
            configOverrides.put("javax.persistence.jdbc.url", env.get(entry.getValue()));
        } else if (entry.getKey().contains("DATABASE_USER")) {
            configOverrides.put("javax.persistence.jdbc.user", env.get(entry.getValue()));
        } else if (entry.getKey().contains("DATABASE_PASSWORD")) {
            configOverrides.put("javax.persistence.jdbc.password", env.get(entry.getValue()));
        }
    }

    // Cria um factory dando override nas variáveis.
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("default", configOverrides);
    EntityManager conteudoManager = factory.createEntityManager();

    Query query = conteudoManager.createNativeQuery("INSERT INTO something(" +
            "SOMETHING," +
            "SOMETHING_ELSE)" +
            " VALUES (" +
            ":something," +
            ":somethingElse)");

    conteudoManager.getTransaction().begin();

    query.setParameter("something", something);
    query.setParameter("somethingElse", somethingElse);
    query.executeUpdate();
    return conteudoManager.getTransaction();

}

public Conteudo getContent() {
    return conteudoRepository.getContent();
}

My persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
             http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="javax.persistence.jdbc.url" value="url" />
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.password" value="password" />
            <property name="javax.persistence.jdbc.user" value="login" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>
  • Why down vote? at least speak..

  • Which application server you are using?

  • I’m using the Kubernets

  • I say which container, Tomcat, Jboss Wildfly, etc..

  • Oh yes, it’s Tomcat

  • You could put your settings in variable Tomcat, ai vc le them in your code, you would need to create in the settings in your Tomcat system properties, if you think this is feasible, I create a detailed answer

  • unfortunately I do not have access to Tomcat settings.

  • A pity, because usually, I work with the datasource within the application server settings.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.