Getting started with JPA mapping : How IntelliJ can map your data with a few clicks

3

With JPA, there finally is an ORM standard and every major IDE has support for it. Today I will show you how to map your data with IntelliJ IDEA. I also use the brand new TopLink 11g preview (a JPA implementation), but you can try this at home with every JPA-implementation.

A few weeks ago Peter showed some AMIS employees (including me) the TopLink workbench. I really was amazed how good is was. And I felt a bit bad that I didn’t knew that such a tool existed. I wanted to use the tool myself. I fiddled around with it and really love it. But I wanted to get JPA-annotations, not the TopLink XML files. Whilst searching on how to do that I discovered that every major IDE supported JPA annotation generation. Last year I mainly used iBatis and Spring-JDBC and kind of forgot about JPA, so the ‘amazing new things’ I’m about to tell might be pretty ancient by now ;-)

Creating the mapping and data sources

Create a project in IntelliJ and add libraries for JUnit, Oracle JDBC (OJDBC14.jar) and the libraries for your JPA-implementation. I’m using  TopLink 11g preview so I have to add persistence.jar, toplink.jar and xmlparserv2.jar

To validate your persistence classes and queries properly you have to add a data source. Go to Tools, Data Sources. Click on the plus icon in the upper left corner of the dialog.

Click on the plus icon and search for your JDBC-driver. It’s usually sufficient to hit the ‘Find Driver Classes’ button, but sometimes you have to look up the driver yourself.
The next step is the database url, user name, password and schema name. You can click the test connection to test the connection and with the refresh tables button you can test some more.

Right click your project, new, add persistence unit. My persistence unit is called Scott. IntelliJ generates the XML for you. You have to add some properties to make sure the application works outside IntelliJ. Overwrite the <persistence-unit> with

<code>
<span style="color: #000000;">&lt;persistence-unit name=</span><span style="color: #2a00ff;">"scott"</span><span style="color: #000000;">&gt;</span>
<span style="color: #ffffff;">    </span><span style="color: #000000;">&lt;properties&gt;</span>
<span style="color: #ffffff;">        </span><span style="color: #000000;">&lt;property name=</span><span style="color: #2a00ff;">"toplink.jdbc.driver" </span><span style="color: #000000;">value=</span><span style="color: #2a00ff;">"oracle.jdbc.OracleDriver"</span><span style="color: #000000;">/&gt;</span>
<span style="color: #ffffff;">        </span><span style="color: #000000;">&lt;property name=</span><span style="color: #2a00ff;">"toplink.jdbc.url" </span><span style="color: #000000;">value=</span><span style="color: #2a00ff;">"jdbc:oracle:thin:@127.0.0.1:1521:orcl"</span><span style="color: #000000;">/&gt;</span>
<span style="color: #ffffff;">        </span><span style="color: #000000;">&lt;property name=</span><span style="color: #2a00ff;">"toplink.jdbc.user" </span><span style="color: #000000;">value=</span><span style="color: #2a00ff;">"scott"</span><span style="color: #000000;">/&gt;</span>
<span style="color: #ffffff;">        </span><span style="color: #000000;">&lt;property name=</span><span style="color: #2a00ff;">"toplink.jdbc.password" </span><span style="color: #000000;">value=</span><span style="color: #2a00ff;">"tiger"</span><span style="color: #000000;">/&gt;</span>
<span style="color: #ffffff;">        </span><span style="color: #000000;">&lt;property name=</span><span style="color: #2a00ff;">"com.intellij.javaee.persistence.datasource" </span><span style="color: #000000;">value=</span><span style="color: #2a00ff;">"Scott"</span><span style="color: #000000;">/&gt;</span>
<span style="color: #ffffff;">    </span><span style="color: #000000;">&lt;/properties&gt;</span>
<span style="color: #000000;">&lt;/persistence-unit&gt;</span></code>

Note that the last property is needed for proper validation of the persistence classes and queries, otherwise you wil  see red underlinings everywhere. You can remove the line when you deploy your project to a web server.

To create the mappings you just have to do some clicking. Right click your project, Generate persistence mapping and pick by database schema.
In the dialog that pops up you have to add some things:

  • choose Datasource: Scott
  • package: nl.amis.domain.scott
  • tick add to persistence unit and pick scott
  • deselect Bonus and Salgrade because we only want the Emp and Dept table.

Note that the deptno field of emp is deselected. This is because it’s a foreign key (hence the fk on the table icon) and JPA knows you probably don’t want to use that foreign key. And JPA is right, we want a department object in our employee object.
Select the Emp table and click on the plus icon to add a relation.

In the Employee object we want to select a Department, so add an attribute department and leave type on <entity>. In the department section we want to be able to select a list of employees. Change the Type to java.util.List and enter the attribute name employees.
When you’re using an ancient database without relations you can enter a source an target column, but that isn’t necessary becasue JPA is pretty smart.

Select src directory when you can choose between the resources and src directory.

Go to the package nl.amis.domain.scott and look what IntelliJ did for you. Of course there are a lot of annotations that aren’t really necessary, but they won’t hurt performance and now it’s more clear what is actually happening.

In the DeptEntity class you can see there is a one to many relation to employee:

<code>
<span style="color: #ffffff;">    </span><span style="color: #646464;">@OneToMany</span><span style="color: #000000;">(</span><span style="color: #000000;">mappedBy = </span><span style="color: #2a00ff;">"department"</span><span style="color: #000000;">)</span>
<span style="color: #ffffff;">    </span><span style="color: #7f0055;"><strong>public </strong></span><span style="color: #000000;">List&lt;EmpEntity&gt; getEmployees</span><span style="color: #000000;">() {</span>
<span style="color: #ffffff;">        </span><span style="color: #7f0055;"><strong>return </strong></span><span style="color: #000000;">employees;</span>
<span style="color: #ffffff;">    </span><span style="color: #000000;">}</span></code>

The final step is adding the used classes to persistence.xml (copy these tags just above <properties>)

<code>
<span style="color: #ffffff;">    </span><span style="color: #000000;">&lt;</span><span style="color: #7f0055;"><strong>class</strong></span><span style="color: #000000;">&gt;nl.amis.domain.scott.EmpEntity&lt;/</span><span style="color: #7f0055;"><strong>class</strong></span><span style="color: #000000;">&gt;</span>
<span style="color: #ffffff;">    </span><span style="color: #000000;">&lt;</span><span style="color: #7f0055;"><strong>class</strong></span><span style="color: #000000;">&gt;nl.amis.domain.scott.DeptEntity&lt;/</span><span style="color: #7f0055;"><strong>class</strong></span><span style="color: #000000;">&gt;</span></code>

Querying your data

Now it’s finally time to query the data. Let’s have a look at this simple test case

<code>
<span style="color: #7f0055;"><strong>public
 class
 </strong></span><span style="color: #000000;">TestTopLinkJPA </span><span style="color: #7f0055;"><strong>extends </strong></span><span style="color: #000000;">TestCase </span><span style="color: #000000;">{</span>
<span style="color: #ffffff;">    </span><span style="color: #7f0055;"><strong>private static final </strong></span><span style="color: #000000;">Logger log = Logger.getLogger</span><span style="color: #000000;">(</span><span style="color: #000000;">TestTopLinkJPA.</span><span style="color: #7f0055;"><strong>class</strong></span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">    </span><span style="color: #7f0055;"><strong>private </strong></span><span style="color: #000000;">EntityManagerFactory emf = </span><span style="color: #7f0055;"><strong>null</strong></span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">    </span><span style="color: #7f0055;"><strong>private </strong></span><span style="color: #000000;">String s;</span>

<span style="color: #ffffff;">    </span><span style="color: #7f0055;"><strong>protected </strong></span><span style="color: #7f0055;"><strong>void </strong></span><span style="color: #000000;">setUp</span><span style="color: #000000;">() {</span>
<span style="color: #ffffff;">        </span><span style="color: #000000;">emf = Persistence.createEntityManagerFactory</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"scott"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">        </span><span style="color: #000000;">log.debug</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"EntityManagerFactory created"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">    </span><span style="color: #000000;">}</span>

<span style="color: #ffffff;">    </span><span style="color: #7f0055;"><strong>protected </strong></span><span style="color: #7f0055;"><strong>void </strong></span><span style="color: #000000;">tearDown</span><span style="color: #000000;">() </span><span style="color: #7f0055;"><strong>throws </strong></span><span style="color: #000000;">Exception </span><span style="color: #000000;">{</span>
<span style="color: #ffffff;">        </span><span style="color: #000000;">emf.close</span><span style="color: #000000;">()</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">        </span><span style="color: #000000;">log.debug</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"EntityManagerFactory closed"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">    </span><span style="color: #000000;">}</span>

<span style="color: #ffffff;">    </span><span style="color: #7f0055;"><strong>public </strong></span><span style="color: #7f0055;"><strong>void </strong></span><span style="color: #000000;">testScott</span><span style="color: #000000;">() </span><span style="color: #7f0055;"><strong>throws </strong></span><span style="color: #000000;">Exception </span><span style="color: #000000;">{</span>
<span style="color: #ffffff;">        </span><span style="color: #000000;">EntityManager em =  emf.createEntityManager</span><span style="color: #000000;">()</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">        </span><span style="color: #000000;">Query query = em.createQuery</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"select e from EmpEntity e"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">        </span><span style="color: #000000;">List&lt;EmpEntity&gt; list = query.getResultList</span><span style="color: #000000;">()</span><span style="color: #000000;">;</span>

<span style="color: #ffffff;">        </span><span style="color: #7f0055;"><strong>for </strong></span><span style="color: #000000;">(</span><span style="color: #7f0055;"><strong>int </strong></span><span style="color: #000000;">i = </span><span style="color: #990000;">0</span><span style="color: #000000;">; i &lt; list.size</span><span style="color: #000000;">()</span><span style="color: #000000;">; i++</span><span style="color: #000000;">) {</span>
<span style="color: #ffffff;">            </span><span style="color: #000000;">EmpEntity e = list.get</span><span style="color: #000000;">(</span><span style="color: #000000;">i</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">            </span><span style="color: #000000;">System.out.println</span><span style="color: #000000;">(</span><span style="color: #000000;">e.getEname</span><span style="color: #000000;">() </span><span style="color: #000000;">+ </span><span style="color: #2a00ff;">", dept location: " </span><span style="color: #000000;">+ e.getDepartment</span><span style="color: #000000;">()</span><span style="color: #000000;">.getLoc</span><span style="color: #000000;">())</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">        </span><span style="color: #000000;">}</span>
<span style="color: #ffffff;">    </span><span style="color: #000000;">}</span>
<span style="color: #000000;">}</span></code>

The coolest thing about IntelliJ is that ‘they’ validate your queries! Lets add ‘where e.’ and hit ctrl-space

How cool is that!? Let’s change EmpEntity to EmpEntaty:

This will make your work so much easier, no more stupid typo’s or hours of searching because you always write Entaty and don’t know what’s wrong with that.

Conclusion

After a few hours with JPA I really love it. I’m always a bit conservative and stuck to iBatis and Spring-JDBC for too long. Not that there’s anything wrong with those two, but JPA can make you job easier (and of course it always good to check out other tools). The four major IDE’s (Eclipse, IntelliJ, NetBeans and JDeveloper) are really competing with each other and we as users get betters tools because of that competition. A few years ago it would’ve taken ages to get some proper tooling for new frameworks and now I’m suprised good tools are already out there.

Share.

About Author

3 Comments

  1. Jeroen van Wilgenburg on

    I have 2GB of RAM. With the Scott schema it’s pretty fast. With a large database it gets a little bit slower, but 10 minutes is too long. Did you notice the unselect all button in the toolbar above the check-boxes?

  2. How much RAM do you have? I have 2GB, but every time I tried to check one of the deselected check-boxes in the Import Database Schema dialog, I had to wait 10-15 minutes to get my processor back. With dozens to go, this just isn’t going to work.