The building of a web application

The simple way, extracted from the hard methods

Posts Tagged ‘Annotating

Let’s use JPA now – Part 2: Defining Columns

After we made and understand how to make Tables from our classes, let’s continue by annotating their member variables.

Firstly, you have to keep this in mind:

JPA could only convert primitive datatypes of Java in primitive datatypes of SQL. Objects made of self-brewed classes can only be written directly to the database with the special annotation @Embeddable, or you have to create a special table for them, like we did earlier.

Generic Annotations:

Use this annotation when you don’t want that EclipseLink saves the variable to the database. For example converters such as SimpleDateFormat. EclipseLink will pass this member variable and doesn’t make a column for this one.

The annotation for defining the variable as the primary key of the table. It’s recommended to add at each class a special member-variable when the class has its own table.

The annotation which is recommended for the member-variable which is also annotated with @Id, unless you want to handle the index-value of the entries of the table by yourself.

Handy when you want to enforce that every entry has a value at that column. Please notice that you’ll also get bugged when a 0 is inserted/updated at a column with the INTEGER datatype, because it’s also counted as ‘not null’.

Contrary of @NotNull, every value is allowed. Is the default setting at each member variable when @NotNull nor @Null is annotated.

@Column (name = “email_address”)
An annotation to add more specific properties for the member value.
It could be very useful, for example since databases doesn’t like the lowerCamelCase- nor UpperCamelCase-style, like:

private String emailAddress;

Should be:
@Column (name = "email_address", nullable=false)
private String emailAddress;

For member variables which are used as primary keys, you could also use it like this:
@Column (unique = true, nullable = false)
private int id;

In this case the “unique” keyword of @Column says that each value of column should be unique and can’t be used twice in the table.

Type-specific annotations:

This one is required for variables which have collections as type.

Please remember that you can’t use direct collection implementations like HashMap, ArrayList, HashSet!
Always use their interface, like Map, List and Set, and use the implementation of the variables at their initialisation!

Use this annotation when the type of the variable is a self-defined class, which don’t have its own table.
The object assigned to this variable will be serialized and saved into the column.

This annotation is used when the type is java.util.Date and java.util.Calendar.
Note that you also have to include an attribute value, like @Temporal(TemporalType.TIMESTAMP), @Temporal(TemporalType.DATE) and @Temporal(TemporalType.TIME).

Use this annotation when the type of the variable is an ENUM. Please note that ‘ENUM-classes’ don’t have any annotation.

Done with that?
The next thing that we’ll do is talking to your database via the sourcecode.


Written by RiVaSo

21 February 2012 at 00:22

Posted in How To, JPA

Tagged with , , , ,

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 , , , ,