Creating the "persistence.xml" JPA / Hibernate file

0

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

Example of 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" />

I would like to change the property

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

for

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

Only from value="create-drop" to value="update"

    
asked by anonymous 14.06.2017 / 15:34

1 answer

0

What I understood from your question is that it is about automation.

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

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 src/META-INF/ folder and its src/META-INF/persistence.xml file, 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% wizard% as required. In the example here, we will define the plugin: pom.xml 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 tomcat7-maven-plugin and test/java folders in the test/resources folder similar to the src folder structure.

5) Create the package src/main (it must be the same as the package of the command above br.com.seu_archetype_app_modelo.web.servlet ) in the -DgroupId=br.com.seu_archetype_app_modelo folder.

Run the following command:

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

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

6) Create the file: src/test/java in package: AppStatusServlet.java ;

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 at this step we already have the model project ready. Go back to folder: br.com.seu_archetype_app_modelo.web.servlet .

7) Run the command:

mvn archetype:create-from-project

After completing the above command, seu-archetype-app-modelo will be created.

8) In any other folder outside seu-archetype-app-modelo/target/generated-sources/archetype , create a seu-archetype-app-modelo folder, run the command:

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

9) Copy seu-appweb-archetype to seu-archetype-app-modelo/target/generated-sources/archetype , execute the following:

$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 .

10) We should edit the file: ~/projetos/seu-appweb-archetype/target . Replace " your-archetype-app-template " to $ {artifactId}.

11) We should edit the file: ~/projetos/seu-appweb-archetype/src/main/resources/archetype-resources/pom.xml .

  • Ready ! To finish! Access the ~/projetos/seu-appweb-archetype/src/main/resources/archetype-resources/src/main/java/AppStatusServlet.java folder and run:

    mvn clean install
    

    Using Archetype

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

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

    You will be prompted 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 ~/projetos

    Run the commands:

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

    And then access the link: 1

    NOTE 2 : Now we have the project Model: http://localhost:8080/nome-novo-projeto/status that was created based on seu-archetype-app-modelo .

    NOTE 3 : Any improvement of the model project (% with%), we must repeat steps 7-11.

    4

    You can do better by creating a script shell with commands, but I preferred to perform the steps manually to demonstrate better use of Archetype of maven .

    Reference :

    Apress, 2013, Beginning Java EE 7 (Expert Voice in Java).
    Juneau, Josh - 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, # : Leverage the power of the WildFly application server from JBoss to develop Java EE 7 applications Tim O'Brien ], Copyright © 2010 Sonatype, Inc., The Maven C ookbook : A Sonatype Open Book Mountain View, CA.
    [ Balaji Varanasi, Sudha Belida - 2014 ], Copyright © 2014 by Balaji Varanasi and Sudha Belida, Introducing Maven

        
  • 14.12.2017 / 12:46