The building of a web application

The simple way, extracted from the hard methods

Let’s use JPA now Part 1: Creating Tables

We make a travel in time. And now we’re at the point that we made a domain model for the logic of our application. Of course, you could indeed build a GUI with Wicket first, it’s a bit what you like the most. In this blogpost I assume you’ve build a working domain model. But now, we have to save our precious data in a database. Instead of building a database with a database-generation program, -or even more dramatically- writing it in PLAIN SQL. We don’t want that because it’s too much work, and time. Instead of that, we’re using JPA (previously known as Hibernate).

What you’ll need:
– A few basic classes
– Configured your IDE to work with an Persistence Service, Like OpenJPA, TopLink or EclipseLink. The latter one is packed and preconfigured in NetBeans.

You could use JPA via an apart XML file or do it with Annotations in your code. It’s up to you which method you like the most, I’m covering the method for using Annotations because I find apart XML files harder to maintain.

There are various things you could define with Annotations.

Let’s start now.

If you want to give your domain class a own table use the annotation @Entity above the define statement of your class, like this:

package com.rivaso.exp.domain;

import javax.persistence.Entity;
import javax.persistence.Table;

 * @author RiVaSo
 * @version 1.0
@Table( name = "person" )
public class Person
        implements Serializable
	// More code goes here

As you could see, there is also an annotation included called @Table, where you define that it has a primairy table, in this case the table “Person”.

Please notice that the parameter ‘name = “person”‘ wasn’t needed, because it’s more used to give the Classes who are written in camelcase, a nickname for the database to distinguish them from other classes and SQL-keywords.

Ok, done that. What’s next?
Annotating the member values of the class…so we could define the columns.


Written by RiVaSo

20 February 2012 at 23:54

Posted in How To, JPA

Tagged with , , , ,

%d bloggers like this: