
Create a simple maven project using Java EE + WildFly10 + JPA (Hibernate) + Postgresql + EJB + IntelliJ IDEA
In this article I will tell you how to configure a project on JBoss using JPA. I won’t go into the details of JPA, Hibernate, EJB, this is a separate topic. I’ll just show you the structure of the project, how to configure datasource on WildFly and run it all in IntelliJ IDEA. This framework, I think, will be useful for beginners working with JavaEE and JPA.
We 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 into 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.
The next step is to create a datasource on the server. The easiest way would be to use the admin console provided by WildFly.
In order to enter it, first you need to start the server /bin/standalone.bat and go to the address 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 website. I downloaded postgresql-42.2.4.jar. We add it to deployments. Any name can be given.
Further Configuration-> Subsystems-> Datasources-> Non-XA-> Add.
Choose Postgresql Datasource and our downloaded driver. We set the url of our database, username and password. (not to be confused with the username and password from the server). If everything was 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.
Everything is standard here. I think no extra comments are required. Create a maven project. We put war in packaging. And add the necessary dependencies.
Actually here is my pom.xml

Note that persitence.xml is in WEB-INF-> classes-> META-INF.
And here is persistence.xml itself
As jta-data-source, use jndi-name, which was specified when creating the datasource.
If you forget, you can look at 127.0.0.1:9990 in the section Configuration-> Subsystems-> Datasources-> Our datasource-> View-> Attributes-> JNDI line.
Now let's look at our classes.
1. The simplest entity class.
Details do not paint. This is another topic.
2. EJB class
Annotation @PersistenceContext injects our persistence-unit and creates an EntityManager based on it.
The @Stateless annotation indicates that it is ejb.
3. The simplest Servlet
Annotation @EJB injectit JavaBean.
In the doGet method, a user named “Ser” is created 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.
To configure jboss in IDEA, go to Run-> EditConfiguration, click "+" in the upper left corner and select jboss-local.

As the 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 page: 8080 / project name / test.
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 this article helped someone.
Here is a link to GitHub with this project.
Install WildFly10
We 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 into 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 would be to use the admin console provided by WildFly.
In order to enter it, first you need to start the server /bin/standalone.bat and go to the address 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 website. I downloaded postgresql-42.2.4.jar. We add it to deployments. Any name can be given.
Further Configuration-> Subsystems-> Datasources-> Non-XA-> Add.
Choose Postgresql Datasource and our downloaded driver. We set the url of our database, username and password. (not to be confused with the username and password from the server). If everything was 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.
Creating a project in IntelliJ IDEA
Everything is standard here. I think no extra comments are required. Create a maven project. We put war in packaging. And add the necessary dependencies.
Actually here is my pom.xml
4.0.0 yst hiberProject 1.0-SNAPSHOT war javax javaee-api 7.0 provided org.hibernate hibernate-core 5.2.12.Final provided hiberProject
Project structure

Note that persitence.xml is in WEB-INF-> classes-> META-INF.
And here is persistence.xml itself
org.hibernate.jpa.HibernatePersistenceProvider java:/PostgresDS UserEntity JavaBean
As jta-data-source, use jndi-name, which was specified when creating the datasource.
If you forget, you can look at 127.0.0.1:9990 in the section Configuration-> Subsystems-> Datasources-> Our datasource-> View-> Attributes-> JNDI line.
Now let's look at our classes.
1. The simplest entity class.
Details do not paint. 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
Annotation @PersistenceContext injects our persistence-unit and creates an EntityManager based on it.
The @Stateless annotation indicates that it is 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 injectit JavaBean.
In the doGet method, a user named “Ser” is created 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 the 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 page: 8080 / project name / test.
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 this article helped someone.
Here is a link to GitHub with this project.