Now that our Employee entity is complete, let’s create our Main class where we’ll create an Employee object and persist it to a database. Here is what my Employee class looks like so far: package entity IntelliJ IDEA generates the getters and setters for both variables. I’ll press Ctrl to select both variables and click OK. I’ll bring up the Generate menu ( Alt+ Insert for Windows/Linux or ⌘N for macOS) and select Getter and Setter. Let’s go ahead and generate the setters and getters for my new fields. You’ll notice that the ID field has two gutter icons, one for field access and one for property access. IntelliJ IDEA will distinguish ID fields with a small key icon. There are also gutter icons for your entity’s persistent fields. The gutter icon on the Entity class declaration allows you to navigate to the Persistence Tool Window. You’ll notice that the Employee Entity has some gutter icons. We’ll also add a String variable for the Employee’s last name called lName. I’ll add a String variable for the Employee’s first name called fName (which isn’t the best variable name but we’ll be changing that later in the tutorial). Now let’s add a couple more variables to our Employee entity. IntelliJ IDEA creates the Employee no-arg constructor. Then click Select None so we can generate a constructor with no arguments. Our Employee class will be created with a generated ID along with its setter and getter.Īccording 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. Once we click OK, IntelliJ IDEA will create the new entity package along with our Employee class. Since the package currently doesn’t exist, it’ll be shown in red. For Destination Package, we’ll create a new package called entity. We can do so by right-clicking on our default persistence unit clicking New then clicking Entity.įor Create Class, we’ll enter in Employee. Let’s create an Entity which will represent an Employee. In addition, a default persistence unit is created for us. You’ll see that IntelliJ IDEA created a persistence.xml configuration file where we’ll configure our managed persistence classes as well as our database. The Persistence tool window allows us to create a variety of resources for our persistence applications. We’ll open up our Persistence tool window by going to View -> Tool Windows -> Persistence. Now that our dependencies are all set, let’s start creating the files needed for our persistence application. I can also click on the icon that appears in the top right corner of my pom.xml file. When the dependency is added, I will load my maven changes by pressing on Ctrl+ Shift+ O on Windows/Linux or ⇧⌘I on macOS. The following dependency will be added to the pom.xml file as a result: Under org.hsqldb, I will select the latest version to include in my pom.xml file. A window will popup that I’ll use to search for my HyperSQL dependency by entering hsqldb in my search window. In my pom.xml file, I’ll add my HyperSQL dependency by pressing Alt+ Insert for Windows/Linux or ⌘N for macOS. In this tutorial, I’ll use a light-weight database called HyperSQL. In addition to these dependencies, our application will also need the dependencies for the database where we’ll be persisting our data. You’ll notice that IntelliJ IDEA generated some dependencies needed for our application based on the frameworks selected when we created our project. In our new project, let’s open our generated pom.xml file. IntelliJ IDEA will create the project and generate some files for us. In the next window, I will set the project name to JPA-App and change the group to my company name, com.jetbrains. I want to create a JPA application that uses Hibernate so under Implementations, I will select Hibernate. In the next window, we’ll select the libraries required by my application. I won’t be using any application servers for my persistence application so I will not configure the application server field. Then, I’ll select Library for my template. In this tutorial, I’ll use the latest Long Term Supported (LTS) Java version which is Java 11. 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. Creating a new JPA Projectįirst, we’ll create a new project in IntelliJ IDEA Ultimate by clicking on the New Project button in the Welcome screen. If you want to create a Jakarta Persistence application with the new jakarta namespace, checkout this version of the blog or watch this video. JPA allow you to store, access and manage Java objects in a relational database. In this blog, we’re going to look at how to create a simple JPA application in IntelliJ IDEA Ultimate.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |