Hướng dẫn sử dụng intellij idea để lập trình java Informational, Transactional năm 2024

In this blog, we’re going to look at how to create a simple JPA application in IntelliJ IDEA Ultimate. JPA allow you to store, access and manage Java objects in a relational database.

If you want to create a Jakarta Persistence application with the new jakarta namespace, checkout this version of the blog or watch this video.

Creating a new JPA Project

First, we’ll create a new project in IntelliJ IDEA Ultimate by clicking on the New Project button in the Welcome screen. We’ll select Java Enterprise from the left menu which allows us to take advantage of the enterprise framework support provided in IntelliJ IDEA Ultimate. In this tutorial, I’ll use the latest Long Term Supported (LTS) Java version which is Java 11. Then, I’ll select Library for my template. I won’t be using any application servers for my persistence application so I will not configure the application server field. Then, I’ll click Next.

Hướng dẫn sử dụng intellij idea để lập trình java	Informational, Transactional năm 2024

In the next window, we’ll select the libraries required by my application. I want to create a JPA application that uses Hibernate so under Implementations, I will select Hibernate. Then, I’ll click Next.

Hướng dẫn sử dụng intellij idea để lập trình java	Informational, Transactional năm 2024

In the next window, I will set the project name to JPA-App and change the group to my company name, com.jetbrains. Then click Finish. IntelliJ IDEA will create the project and generate some files for us.

Adding the Database Dependencies

In our new project, let’s open our generated pom.xml file. You’ll notice that IntelliJ IDEA generated some dependencies needed for our application based on the frameworks selected when we created our project. In addition to these dependencies, our application will also need the dependencies for the database where we’ll be persisting our data. In this tutorial, I’ll use a light-weight database called HyperSQL. In my pom.xml file, I’ll add my HyperSQL dependency by pressing Alt+Insert for Windows/Linux or ⌘N for macOS. Then, I’ll choose Dependency. A window will popup that I’ll use to search for my HyperSQL dependency by entering hsqldb in my search window. Under org.hsqldb, I will select the latest version to include in my pom.xml file. Then, click Add. The following dependency will be added to the pom.xml file as a result:

<dependency>

<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.5.1</version>
</dependency>

When the dependency is added, I will load my maven changes by pressing on Ctrl+Shift+O on Windows/Linux or ⇧⌘I on macOS. I can also click on the icon that appears in the top right corner of my pom.xml file. Now that our dependencies are all set, let’s start creating the files needed for our persistence application.

Creating an Employee Entity

We’ll open up our Persistence tool window by going to View -> Tool Windows -> Persistence. The Persistence tool window allows us to create a variety of resources for our persistence applications. You’ll see that IntelliJ IDEA created a

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

0 configuration file where we’ll configure our managed persistence classes as well as our database. In addition, a default persistence unit is created for us.

Let’s create an Entity which will represent an Employee. We can do so by right-clicking on our default persistence unit clicking New then clicking Entity.

Hướng dẫn sử dụng intellij idea để lập trình java	Informational, Transactional năm 2024

For Create Class, we’ll enter in

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

1. For Destination Package, we’ll create a new package called

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

2. Since the package currently doesn’t exist, it’ll be shown in red. Once we click OK, IntelliJ IDEA will create the new entity package along with our Employee class. Our Employee class will be created with a generated ID along with its setter and getter.

According to the JPA specification, an entity must have a no-arg constructor so we’ll generate it by bringing up the Generate window using Alt+Insert for Windows/Linux or ⌘N for macOS. We’ll choose Constructor from the list. Then click Select None so we can generate a constructor with no arguments. IntelliJ IDEA creates the Employee no-arg constructor.

Now let’s add a couple more variables to our Employee entity. I’ll add a String variable for the Employee’s first name called

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

3 (which isn’t the best variable name but we’ll be changing that later in the tutorial). We’ll also add a String variable for the Employee’s last name called

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

4.

You’ll notice that the Employee Entity has some gutter icons.

Hướng dẫn sử dụng intellij idea để lập trình java	Informational, Transactional năm 2024

The gutter icon on the Entity class declaration allows you to navigate to the Persistence Tool Window. There are also gutter icons for your entity’s persistent fields. IntelliJ IDEA will distinguish ID fields with a small key icon. You’ll notice that the ID field has two gutter icons, one for field access and one for property access.

Let’s go ahead and generate the setters and getters for my new fields. I’ll bring up the Generate menu (Alt+Insert for Windows/Linux or ⌘N for macOS) and select Getter and Setter. I’ll press Ctrl to select both variables and click OK. IntelliJ IDEA generates the getters and setters for both variables.

Here is what my Employee class looks like so far:

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

Creating the Main Class

Now that our Employee entity is complete, let’s create our

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

5 class where we’ll create an Employee object and persist it to a database. In the Project Window, we’ll select the java folder and bring up the New menu by pressing Alt+Insert for Windows/Linux or ⌘N for macOS. Choose Java Class and then type in our class name,

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

5.

In our new class, let’s add a main method. I’ll type in

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

7 and press Enter, letting IntelliJ IDEA complete the declaration for me. Now, I’ll create the Employee object that we’ll persist to our database. We’ll create an Employee object then set its first and last name.

Now, the first step to persisting my employee is to create an EntityManagerFactory (you’ll notice that if you type Emf, IntelliJ IDEA will bring up the EntityManagerFactory class that we can select). IntelliJ IDEA will also suggest a variable name that you can use. We’ll create the EntityManagerFactory by calling the

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

8 method with

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

9 as our persistence unit name.

Next, we’ll create the EntityManager by calling the

import entity.Employee; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class Main {

public static void main(String[] args) {
    Employee employee = new Employee();
    employee.setfName("Dalia");
    employee.setlName("Abo Sheasha");
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(employee);
    entityManager.getTransaction().commit();
    entityManager.close();
    entityManagerFactory.close();
}
}

0 method. Once we do, we can now begin a transaction by calling the EntityManager’s

import entity.Employee; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class Main {

public static void main(String[] args) {
    Employee employee = new Employee();
    employee.setfName("Dalia");
    employee.setlName("Abo Sheasha");
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(employee);
    entityManager.getTransaction().commit();
    entityManager.close();
    entityManagerFactory.close();
}
}

1. Then, we can persist our Employee object that we created earlier by calling the EntityManager’s

import entity.Employee; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class Main {

public static void main(String[] args) {
    Employee employee = new Employee();
    employee.setfName("Dalia");
    employee.setlName("Abo Sheasha");
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(employee);
    entityManager.getTransaction().commit();
    entityManager.close();
    entityManagerFactory.close();
}
}

2 method. Now that this is done, we can cleanup our resources. We’ll commit our transaction and close our EntityManager and EntityManagerFactory.

The final Main class should look similar to the following:

import entity.Employee; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class Main {

public static void main(String[] args) {
    Employee employee = new Employee();
    employee.setfName("Dalia");
    employee.setlName("Abo Sheasha");
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(employee);
    entityManager.getTransaction().commit();
    entityManager.close();
    entityManagerFactory.close();
}
}

At this point, we’re almost ready to persist our data. The main step missing is setting up the database where our data will be persisted. You’ll remember earlier in the tutorial we mentioned that we’ll be using HyperSQL as our database. So, let’s go ahead and setup our database.

Setting up the HyperSQL Database

Let’s navigate to our

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

0 configuration file from the Persistence tool or Project window (under

import entity.Employee; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class Main {

public static void main(String[] args) {
    Employee employee = new Employee();
    employee.setfName("Dalia");
    employee.setlName("Abo Sheasha");
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(employee);
    entityManager.getTransaction().commit();
    entityManager.close();
    entityManagerFactory.close();
}
}

4). In the

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

0 file, you’ll notice that our Employee entity has already been configured as a managed persistence class in our default persistence unit. Let’s add the JPA properties required to configure our HyperSQL database.

You’ll see that as soon as you start typing

import entity.Employee; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class Main {

public static void main(String[] args) {
    Employee employee = new Employee();
    employee.setfName("Dalia");
    employee.setlName("Abo Sheasha");
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(employee);
    entityManager.getTransaction().commit();
    entityManager.close();
    entityManagerFactory.close();
}
}

6, IntelliJ IDEA brings up suggestions for all the element that go into

import entity.Employee; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class Main {

public static void main(String[] args) {
    Employee employee = new Employee();
    employee.setfName("Dalia");
    employee.setlName("Abo Sheasha");
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(employee);
    entityManager.getTransaction().commit();
    entityManager.close();
    entityManagerFactory.close();
}
}

7. I’ll choose

import entity.Employee; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class Main {

public static void main(String[] args) {
    Employee employee = new Employee();
    employee.setfName("Dalia");
    employee.setlName("Abo Sheasha");
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(employee);
    entityManager.getTransaction().commit();
    entityManager.close();
    entityManagerFactory.close();
}
}

8 and press Enter. Then I’ll start typing

import entity.Employee; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class Main {

public static void main(String[] args) {
    Employee employee = new Employee();
    employee.setfName("Dalia");
    employee.setlName("Abo Sheasha");
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(employee);
    entityManager.getTransaction().commit();
    entityManager.close();
    entityManagerFactory.close();
}
}

6 and select

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

0 which will insert

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

0 with the

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

2 and

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

3 attributes.

For my first property, I want to specify the HyperSQL JDBC driver. I’ll set the first property

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

2 attribute to

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

5 and

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

3 attribute to

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

7.

Then, I’ll add another property element to configure the database URL. I’ll set the property

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

2 attribute to

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

9. For the value, I want my program to create an embedded HyperSQL database for me when it runs. So I will specify my URL to create an embedded database in my project’s target directory and call it

employee.setFirstName("Hermione"); employee.setLastName("Granger");

0. I’ll also set the shutdown property to true so that the database will close with the last connection. I can do this by specifying a

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

3 of

employee.setFirstName("Hermione"); employee.setLastName("Granger");

2.

Next, I’ll add two property elements to configure the database user and password. For the user, I’ll set the property

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

2 attribute to

employee.setFirstName("Hermione"); employee.setLastName("Granger");

4 and

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

3 attribute to

employee.setFirstName("Hermione"); employee.setLastName("Granger");

6. For the password, I’ll set the property

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

2 attribute to

employee.setFirstName("Hermione"); employee.setLastName("Granger");

8 and

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

3 attribute to

@Column(name = "FNAME") private String firstName; @Column(name = "LNAME") private String lastName;

0.

Finally, I’ll add another property that will result in my entity’s table being generated for me in the database. I’ll set the property

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

2 attribute to

@Column(name = "FNAME") private String firstName; @Column(name = "LNAME") private String lastName;

2 and

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

3 attribute to

@Column(name = "FNAME") private String firstName; @Column(name = "LNAME") private String lastName;

4. This property results in the Employee table getting created for me in the database.

The final

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

0 file should look like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

Running the JPA Application

Now that we’ve finished configuring our database, let’s go back to our

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

5 class and run our application. We can run our application by pressing Ctrl+Shift+F10 for Windows/Linux or ^⇧R for macOS.

Once our application runs, a HyperSQL database will be created as well as an Employee table. Then our Employee object will be persisted to our database table. At this point, you might want to view the tables that were created for you in the database. To do that, let’s configure IntelliJ IDEA to connect to the HyperSQL database that was created by our application.

Viewing the Persisted Employee in the Database

We’ll need to copy the database URL we specified in the

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

0 file (

employee.setFirstName("Hermione"); employee.setLastName("Granger");

2). Remember that specifying this URL in your

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

0 file resulted in our application creating an embedded database called myDB. We will now connect to this database and see the data that was persisted into our database.

Open the by going to View -> Tool Windows -> Database. Click on the + button and choose Data source from URL.

Hướng dẫn sử dụng intellij idea để lập trình java	Informational, Transactional năm 2024

Then, paste in the database URL (

employee.setFirstName("Hermione"); employee.setLastName("Granger");

2). IntelliJ IDEA will detect that it’s a HyperSQL URL. Click OK.

Let’s finish configuring the database. I’ll set the database configuration name to

employee.setFirstName("Hermione"); employee.setLastName("Granger");

0. For my User and Password fields, I’ll enter in the user and password I set in my

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

0 file (

employee.setFirstName("Hermione"); employee.setLastName("Granger");

6,

@Column(name = "FNAME") private String firstName; @Column(name = "LNAME") private String lastName;

0). If you have a warning about missing HyperSQL drivers, click on the `JPA-App`5.

Hướng dẫn sử dụng intellij idea để lập trình java	Informational, Transactional năm 2024

Under the Options tab, I will enable the Auto-disconnect after setting and set it to disconnect after 3 seconds. This setting will disconnect the database in IntelliJ IDEA and release all locks allowing my application’s process to continually connect and write to the database. Then I will test my connection and make sure my configuration is valid then I’ll click OK.

Hướng dẫn sử dụng intellij idea để lập trình java	Informational, Transactional năm 2024

In the Database Window, you will now see the

employee.setFirstName("Hermione"); employee.setLastName("Granger");

0 database. The database contains an `JPA-App`7 table under the default `JPA-App`8 schema. When we double-click on the `JPA-App`7 table, we see that our Employee entity is persisted into an Employee table with an `com.jetbrains`0, `com.jetbrains`1 and `com.jetbrains`2 column. We also see the data from the Employee object we created in our

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

5 class.

Hướng dẫn sử dụng intellij idea để lập trình java	Informational, Transactional năm 2024

Now that we have our database configured, let’s connect this datasource to our persistence unit by right-clicking on the default persistence unit in the Persistence tool window and clicking Assign Data Sources… then selecting our

employee.setFirstName("Hermione"); employee.setLastName("Granger");

0 database from the drop-down menu. This step is required for the IntelliJ IDEA code completion that we’ll see in the next section.

Hướng dẫn sử dụng intellij idea để lập trình java	Informational, Transactional năm 2024

Hướng dẫn sử dụng intellij idea để lập trình java	Informational, Transactional năm 2024

Modifying the Application

I want to persist another Employee object so I’ll go back to my

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

5 class. Since my first `com.jetbrains`6 Employee is already persisted, I can take advantage of the code I already wrote and simply replace the first and last name with other names:

employee.setFirstName("Hermione"); employee.setLastName("Granger");

I also want to make some adjustments to my Employee object so I’ll navigate to my

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

1 class. I want to rename my variables and give them better names. I’ll select the

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

3 variable and press Shift+F6. I’ll choose Include Accessors and rename my field from

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

3 to `hsqldb`0. I’ll do the same for the

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

4 variable and replace

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

4 with `hsqldb`3.

Now, let’s try to rerun our application so we can persist our second Employee.

You’ll notice that after the application runs, we get an error saying that the firstName object was not found.

Hướng dẫn sử dụng intellij idea để lập trình java	Informational, Transactional năm 2024

This error happens because the first time the application ran, it created an Employee table using the

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

3 variable name as its column name. After refactoring the variable to `hsqldb`0, the application tries to persist the second Employee into the existing table using the new `hsqldb`0 variable which doesn’t match the database table.

Hướng dẫn sử dụng intellij idea để lập trình java	Informational, Transactional năm 2024

We can either drop the `JPA-App`7 table and create a new table with the new variable name or, if we have existing data in our table and we can’t drop the table, we can add an `hsqldb`8 annotation that maps the Entity’s variable to the database column. Since I don’t want to lose my existing data, I’ll add the `hsqldb`8 annotation to my Entity’s variables. On the `hsqldb`0 variable declaration, I’ll start typing in the `hsqldb`8 annotation then select the

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd"
         version="2.2">
<persistence-unit name="default">
    <class>entity.Employee</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/myDB;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

2 attribute from the list of suggestions. Then, I will press Ctrl+Space for code completion. IntelliJ IDEA will bring up the current `JPA-App`7 table column names. I will choose the FNAME column name.

Hướng dẫn sử dụng intellij idea để lập trình java	Informational, Transactional năm 2024

Note: the database code completion is only available after you assign your persistence unit to your data source we did in the last section.

We’ll similarly add the `hsqldb`8 annotation to the `hsqldb`3 variable.

@Column(name = "FNAME") private String firstName; @Column(name = "LNAME") private String lastName;

If your Employee entity is generated with property access (the `org.hsqldb`6 is on the `org.hsqldb`7 method instead of the `org.hsqldb`8 field), place your `hsqldb`8 annotations on your

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

00 and

package entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee {

@Id
@GeneratedValue
private Long id;
private String fName;
private String lName;
public Employee() {
}
public void setId(Long id) {
    this.id = id;
}
public Long getId() {
    return id;
}
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
}

01 methods instead of your fields.

Now, let’s try to re-run our application.

The application runs successfully. We can verify that the second employee was persisted to the database by going to the Database view and viewing our `JPA-App`7 table.

Hướng dẫn sử dụng intellij idea để lập trình java	Informational, Transactional năm 2024

In this tutorial we created a JPA application that persisted an Employee entity into a database. The application used for this tutorial is available on GitHub.