📜 ⬆️ ⬇️

Create a simple maven project using Java EE + WildFly10 + JPA (Hibernate) + Postgresql + EJB + IntelliJ IDEA

In this article I will explain how to configure a project on JBoss using JPA. In details JPA, Hibernate, EJB I will not climb, it is a separate subject. I’ll just show the project structure, how to set up a datasource on WildFly and run it all in IntelliJ IDEA. This framework, I think, will be useful for beginners working with JavaEE and JPA.

Install WildFly10


Go to the official WildFly website and download version 10.1.0.Final. (maybe another one will work, but in this project I used it).

Unpack the archive in any directory on your computer.
Next, create a new user. To do this, run bin / add-user.bat. Everything is quite simple there. Follow the instructions and remember the entered username and password.
')

Create datasource


The next step is to create a datasource on the server. The easiest way is to use the admin console provided by WildFly.

In order to log in, you first need to start the server /bin/standalone.bat and go to 127.0.0.1 : 9990. Use the username and password that you just created.

Go to the Deployments-> Add-> Upload a new deployment section.

Now download the jdbc driver from the official postgresql site. I downloaded postgresql-42.2.4.jar. We add it to deployments. The name can be given any.

Further Configuration-> Subsystems-> Datasources-> Non-XA-> Add.

We select Postgresql Datasource and our downloaded driver. We set the url of our database, login and password. (not to be confused with the username and password from the server). If everything is done correctly, then your new datasource should appear in the list. In the View tab, you can check the connection to the database if you click Test Connection.

Create a project in IntelliJ IDEA


Everything is standard here. I think extra comments are required. Create a maven project. In the packaging put the war. And add the necessary dependencies.

Actually here is my pom.xml

<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>yst</groupId> <artifactId>hiberProject</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.12.Final</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>hiberProject</finalName> </build> </project> 

Project structure




Note that persitence.xml lies in WEB-INF-> classes-> META-INF.

And here is the 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="myUnit" > <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <jta-data-source>java:/PostgresDS</jta-data-source> <class>UserEntity</class> <class>JavaBean</class> <properties> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.hbm2ddl.auto" value="update" /> </properties> </persistence-unit> </persistence> 

As jta-data-source we use jndi-name, which was specified when creating the datasource.
If you have forgotten, you can look at 127.0.0.1:9990 in the Configuration-> Subsystems-> Datasources-> Our datasource-> View-> Attributes-> JNDI string.

Now let's look at our classes.

1. The simplest entity class.

Details are not painted. This is another topic.

 import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class UserEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 

2. EJB class

The @PersistenceContext annotation injects our persistence unit and on its basis creates the EntityManager.

The @Stateless annotation indicates that this is an ejb.

 import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @Stateless public class JavaBean { @PersistenceContext(unitName = "myUnit") EntityManager entityManager; public void saveUser(UserEntity user){ entityManager.persist(user); } } 

3. The Simplest Servlet

Annotation @EJB injected JavaBean.

In the doGet method, a user is created with the name “Ser” and the saveUser method from ejb is called.
If there was no userentity table, then hibernate will create the table itself and insert our user there.

 import javax.ejb.EJB; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/test") public class App extends HttpServlet{ @EJB JavaBean javaBean; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { UserEntity user=new UserEntity(); user.setName("Ser"); javaBean.saveUser(user); resp.getWriter().println("Hello from servlet"); } } 

Launch of the project


To configure jboss in IDEA, go to Run-> EditConfiguration, click "+" in the upper left corner and select jboss-local.



As ApplicationServer, select the folder with our installed WildFly. As an artifact, I selected ExternalArtifact (compiled by maven hiberProject.war), removed the standard Build and added the standard maven tasks (clean package install).

Now we press start and we wait when the server is loaded. Next, go to the localhost: 8080 / project_name / test page.

When the page loads, the doGet method is triggered and our user with the name “Ser” is written to the database.

Thanks to all. I hope someone this article has helped.
Here is a link to github with this project.

Source: https://habr.com/ru/post/420827/


All Articles