ORM Support

ORM (Object/Relational Mapping) is a technique used to provide an object approach in a relational structure; this way, the developer's work is facilitated when he/she is working with the database in his project. In Java, a specification called JPA (Java Persistence API) defines the way that ORM frameworks should work. To facilitate even more the way the developer uses ORM frameworks, IntelliJ provides a lot of support to ORM frameworks like Hibernate and EclipseLink.

To use Hibernate or JPA, we need to activate the persistence tool tab. You may imagine that you just need to go to View | Tool windows | Persistence to activate the persistence tool tab, but you won't find the persistence tool available there. Some tool tabs are only available when you add a corresponding facet to the project; you need to add a facet to enable this tool. As this project was created only to create the tables in the contacts database, it isn't a good approach to add the Java code directly into it. So, instead of creating another project, let's create a new module that will be a child in this project; this way, we continue in the same project without being concerned about the organization of the project become confusing.

Go to the File menu and select New module; the new module window will appear to permit us to select the kind of module we want. Select the option Java module, insert entities as the name of the module, and press the Next button. The next frame of the wizard will ask which facets you want include in your project and, if necessary, it will download the libraries for you. We will use Hibernate instead of JPA in this project, so check the Hibernate facet option (in case you need to use JPA, select the JavaEE Persistence facet); when you do that, the panel at the right will show you some options you can use to configure the facet in the project.

We won't configure anything here, so just click on the Finish button. After the download of the libraries and some processing, you will see the module entities in the Project window. As you can see, the module just has the lib folder with the dependencies and an empty src folder. However, now you can see the persistence tool tab just because this module has the Hibernate facet option. The following screenshot shows the configuring of Hibernate facet:

ORM Support

Creating database entities

Click and open the persistence tool; there you will see Hibernate's logo followed by the entities text, and you should right-click on it. In the context menu, select the last option Generate persistence mapping, and then click on By database schema. A new window will appear to us to generate the database entities. In the Choose data source combobox, select contacts. As we haven't created any package yet, we can't select anything in the Package combobox; however, we can enter the package name we want here and the tool will create it for us, so type org.example.entities in this field. There are two other fields available in the window: Entity prefix and Entity suffix. They are used in case you want to insert something at the start or end of the entity name. In our case, we will insert in the Entity prefix field the value Entity.

In the Database Schema Mapping section, you will see a table with the entities, the tool retrieved from the data source, and some buttons that you can use to edit the relationship between these entities. At the leftmost side of the table exist some colored marks. They are used to inform you that the respective entity isn't already referenced, as you can see in the legend below the table. You can expand the nodes to see if everything is okay and to change type of the properties. In the following screenshot, you can see the Database Schema Mapping section:

Creating database entities

Finally, in the Generation settings section, you will see options that directly affect how the entities will be generated; as you can see, you can define if the entities will use JPA specifications, be generated in XML files, and more. As we won't change anything right now, just click on the OK button to generate the entities and we get an error. We didn't select any persistence unit and still can't see where we can do this. This is common in this window; it shows an error message and is sometimes confusing. To solve this problem, make sure that the Add to session factory option is checked and click on the button with a + icon at the right of the related combobox. A new window will appear asking you to choose or create a Hibernate configuration file—let's create it. Click on the button New file (the first active button at the top), enter the name of the configuration file hibernate.cfg, and click on OK. Now select the created file and click on OK again. Back at the Database Schema Mapping window, the error message immediately is removed and we can click on the OK button to generate the entities. Click on Yes in the confirm dialog and, after some processing, the entities will be ready for use.

Open the Project window and the entity EntityPerson.java. You will see that, in this file, some names retrieved from the tables are underlined in red; it means IntelliJ can't identify the fields. To solve this, position the cursor in one of these problematic fields and use the Alt + Enter shortcut; the first option available is Assign data source—select this and press the Enter key. In the Assign data sources window, you will see the configuration file we created in the Session Factory column and <none> in the Data Source column; click on the <none> entry, select contacts and then click on the OK button. As you can see, all the fields underlined in red vanish.

Problems that can occur

During the time I have been using IntelliJ, in some rare situations, a simple feature didn't work as I had expected and only one feature forced me to do the job using another IDE: generating the entities from a database. You are probably asking yourself now: "why can't you create the entities if you have already done so in the example and it is so simple?" You're correct, it is really simple; however, IntelliJ isn't free from bugs and one of the bugs it has drastically affects the mapping of entities when we are working with Microsoft SQL Server or Oracle database.

When you create a column in Microsoft SQL Server or in Oracle database with the type NVARCHAR, IntelliJ can't identify these types of data as java.lang.String, and instead it will identify as java.lang.Object. If you click on the OK button to generate the entities, it will not work because the properties shouldn't be of type java.lang.Object. You can solve this problem in three ways: manually modify the type of the property, change all of your columns to VARCHAR, or change your database. These solutions are really simple for small databases with less than 20 tables; however, the database of the project I was developing had more than 100 tables, so it was easier for me to use Netbeans just to generate the entities. You can see more about this issue at http://youtrack.jetbrains.com/issue/IDEA-79437. The following image shows IntelliJ can't identify NVARCHAR as java.lang.String:

Problems that can occur
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.248.162