Creating the JPA/Hibernate "persistence.xml" file

Asked

Viewed 2,589 times

0

I wonder if there is a way to setup the creation(s) of the persistence.xml at the time of project creation.

Example of the aqruivo:

<!--  propriedades do hibernate -->
  <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
  <property name="hibernate.show_sql" value="true" />
  <property name="hibernate.format_sql" value="true" /> 
  <!--  atualiza o banco, gera as tabelas se for preciso -->
  <property name="hibernate.hbm2ddl.auto" value="create-drop" />

would like to change the property

<property name="hibernate.hbm2ddl.auto" value="create-drop" />

for

<property name="hibernate.hbm2ddl.auto" value="update" />

Only of value="create-drop" for value="update"

  • André, you want every time you create a new project the value of the property hibernate.hbm2ddl.auto be it update?

  • Tiago, that’s right !

1 answer

1

From what I understand your question is that this is about automation.

We can automate the project creation process in many ways, but I will focus and use the resources of the Maven tool. I will assume that we will take a new project from scratch as a model. The solution is much broader than just creating the file configuration file persistence.xml.

Building an Archetype [Balaji Varanasi, Sudha Belida - 2014, 49 p.]

1) Create the project: seu-archetype-app-modelo

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp  \
-DgroupId=br.com.seu_archetype_app_modelo \
-DartifactId=seu-archetype-app-modelo \
-DgeneratePom=true \
-Dversion=1.0-SNAPSHOT

Access the folder: seu-archetype-app-modelo.

2) Create the folder src/META-INF/ and its file src/META-INF/persistence.xml, with your settings.

Example:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="jasper_book_chapter_4" transaction-type="JTA">
        <properties>
            <!--  propriedades do hibernate -->
            <property name="hibernate.dialect" 
                     value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" /> 
            <!--  atualiza o banco, gera as tabelas se for preciso -->
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

3) Modify the file pom.xml as needed. In the example here, we will define the plugin: tomcat7-maven-plugin and dependency: Servlet 3.0.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>br.com.seu_archetype_app_modelo</groupId>
  <artifactId>seu-archetype-app-modelo</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>seu-archetype-app-modelo Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
     <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>seu-archetype-app-modelo</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
      </plugin>
    </plugins>
  </build>
</project>

4) Create the folders test/java and test/resources in the briefcase src similar to the folder structure src/main.

5) Creating the package br.com.seu_archetype_app_modelo.web.servlet (has to be the same package as the above command -DgroupId=br.com.seu_archetype_app_modelo) in the briefcase src/main/java.

Execute the following command:

$mkdir -p br/com/seu_archetype_app_modelo/web/servlet

NOTE 1: create the same package structure in the folder src/test/java.

6) Create the archive: AppStatusServlet.java in the package: br.com.seu_archetype_app_modelo.web.servlet;

package br.com.seu_archetype_app_modelo.web.servlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

@WebServlet("/status")
public class AppStatusServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws IOException {

    PrintWriter writer = response.getWriter();
    writer.println("OK");
    response.setStatus(response.SC_OK);

  } 
}

Here in this step we already have the template project ready. Go back to folder: seu-archetype-app-modelo.

7) Execute the command:

mvn archetype:create-from-project

After finishing the above command, the seu-archetype-app-modelo/target/generated-sources/archetype.

8) In any other folder outside the seu-archetype-app-modelo, create a folder seu-appweb-archetype, execute the command:

$cd ~/projetos && mkdir seu-appweb-archetype

9) Copy seu-archetype-app-modelo/target/generated-sources/archetype for ~/projetos/seu-appweb-archetype, perform the command:

$cp -Rf seu-archetype-app-modelo/target/generated-sources/archetype/ ~/projetos/seu-appweb-archetype

After copying, we should remove the folder: ~/projetos/seu-appweb-archetype/target.

10) We must edit the file: ~/projetos/seu-appweb-archetype/src/main/resources/archetype-resources/pom.xml. Replace "your-archetype-app-model" for ${artifactId}.

11) We must edit the file: ~/projetos/seu-appweb-archetype/src/main/resources/archetype-resources/src/main/java/AppStatusServlet.java. Supersede "package ${package};" for "package ${package}.web.Servlet;".

Ready! To finish! Access the folder ~/projetos/seu-appweb-archetype and perform:

mvn clean install


Using the Archetype

In our case the Archetype is in ~/projetos/seu-appweb-archetype, then we must access ~/projetos and execute the command:

mvn archetype:generate -DarchetypeCatalog=local \
-DgroupId=br.com.nome_novo_projeto \
-DartifactId=nome-novo-projeto \
-Dversion=1.0-SNAPSHOT

He’ll ask you to choose an option:

Choose archetype:
1: local -> br.com.seu_archetype_app_modelo:seu-archetype-app-modelo-archetype (seu-archetype-app-modelo-archetype)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): :

Just type 1

Execute the commands:

cd nome-novo-projeto && mvn tomcat7:run

And then access the link: http://localhost:8080/nome-novo-projeto/status



NOTE 2: Now we have the Model Project: seu-archetype-app-modelo which was established on the basis of maven-archetype-webapp.

NOTE 3: Any improvement of model design (seu-archetype-app-modelo), We must repeat steps 7-11.

NOTE 4: You can better the solution by creating a shell script with the commands, but I preferred to perform the steps manually to better demonstrate the operation of the use of Archetype of the Maven.


Reference:

[Gonçalves, Antonio - 2013], Apress, 2013, Beginning Java EE 7 (Expert Voice in Java).
[Juneau, Josh - 2013], Apress, 2013, Java EE 7 Recipes: A problem-Solution Approach - Proven Solutions for Java Enterprise Edition 7 Developement.
[Michal Cmil et al - 2014], Copyright 2014 Packt Publishing, Java EE 7 Development with Wildfly: Leverage the power of the Wildfly application server from Jboss to develop Modern Java EE 7 Applications.
[Tim O'Brien et al - 2010], Copyright 2010 Sonatype, Inc., The Maven Cookbook: A Sonatype Open Book Mountain View, CA.
[Balaji Varanasi, Sudha Belida - 2014], Copyright 2014 by Balaji Varanasi and Sudha Belida, Introducing Maven

Browser other questions tagged

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