Configure IP for server access using Hibernate

2

I have a Java application that uses Hibernate, for development I did the configuration of the database using the localhost however for use on other computers I will need to define the IP that will own the database (Server) in my project I use the Persistence.xml file

I thought through a home screen, the user would log the IP of the machine into a configuration file created in the installation.

But now I come across the situation of having to tell Persistence what IP is entered in the file created by the installation process.

Below is the example of my Persistence.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="ConexaoPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>br.com.exemplo.modelo.Exemplovenda</class>
<properties>
  <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/conexao?zeroDateTimeBehavior=convertToNull"/>
  <property name="javax.persistence.jdbc.password" value="root"/>
  <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  <property name="javax.persistence.jdbc.user" value="root"/>
  <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
  <property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>

    
asked by anonymous 02.06.2015 / 16:15

2 answers

3

If I understood the question well I went through a similar situation some time ago. In case the solution I used was as follows:

  • Create a map with properties other than the persistence.xml file, in your case, the database IP; and
  • Pass this map to the createEntityManagerFactory() function that will use the properties of the persistence.xml file and will replace only those that you defined on the map.

Example:

Map map = new HashMap();
map.put("javax.persistence.jdbc.url", "uma URL informado pelo usuário"); //Essa propriedade vai substituir aquela que está no arquivo.

EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("ConexaoPU",mapa);
EntityManager em = emf.createEntityManager();

Of course if the user just entered the IP you will have to treat the URL, for example:

String ip = "192.168.0.0:3306"; //Algum IP e porta informado pelo usuário
String url = "jdbc:mysql:" + ip + "/caminho para arquivo";
    
02.06.2015 / 19:16
3

First Option

You can pass during the creation of EntityManagerFactory a properties file obtained in your classpath as an example below:

Properties p = new Properties();

p.load(new FileInputStream("um/caminho/no/seu/classpath/persistence.properties"));

// ajuste detalhes que não deseja expor no arquivo de propriedades.

EntityManagerFactory factory = Persistence.createEntityManagerFactory("unitAppName", p);

If you need to enter the authentication data you can encrypt the file and decrypt it internally after loading it and before using it.

Second Option

In case you are not informing which entities in your persistence.xml file, you can create a new file with the custom parameters according to your installation, this file should be in the classpath of your application, in addition if you need more protection, you can protect this file in an encrypted Jar file, however you will need to register a specialized classloader to decrypt it.

    
02.06.2015 / 20:09