The building of a web application

The simple way, extracted from the hard methods

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:

@Transient
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.

@Id
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.

@GeneratedValue
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.

@NotNull
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’.

@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:

@NotNull
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:
@Id
@GeneratedValue
@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:

@ElementCollection
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!

@Embedded
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.

@Temporal
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).

@Enumerated
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.

Advertisement

Written by RiVaSo

21 February 2012 at 00:22

Posted in How To, JPA

Tagged with , , , ,

%d bloggers like this: