Configure IP for server access using Hibernate

Asked

Viewed 784 times

2

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

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

But now I come across the situation of having to inform to Persistence which IP informed in the file created by the installation process.

Follow the example of my Persistence below.

<?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>

  • did not understand the question, you made the database on localhost and want to add to an external server?

  • for development I created a local database, even in the configuration I used the localhost, but for production I need to inform externally what is the IP of my server.

  • Have you tried replacing localhost with your IP with the database? For example //meuip.com:port/nameBD ?

  • yes, and it works without problems, only I do not know if in the client it will keep the same ip that I fixed in the programming, in this case I get very tied.

2 answers

3


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

  • Create a map with different file properties persistence.xml, in your case, the database IP; and
  • Move that map to the function createEntityManagerFactory() that will use the file properties persistence.xml and will replace only those you set 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 typed only the IP you will have to handle 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";

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 inform the authentication data you can encrypt the file and decrypt it internally after loading it and before using it.

Second Option

If you are not informing which entities are in your file persistence.xml, you can create a new file with the custom parameters as per your installation, this file must be in the classpath of your application, in addition if you need further protection, you can protect this file in an encrypted Jar file, but you will need to register a specialized classloader to decrypt it.

Browser other questions tagged

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