Common Expenses: Hibernate

Here we start coding (finally)!

As I wrote before I’m going to use Hibernate as ORM (Object Relational Mapper).

I very few words Hibernate map database tables to POJO classes to achieve persistency. To make the class map correctly the table we need to add some extra information to the code. This could be done with an XML file for each class (let’s call it Entity from now) or with annotations.

I choose annotations (JPA ones implemented by Hibernate) because I think is better to keep everithing on the same file and I do not have to switch from a Java file to an XML file.

My first idea was to generate that classes automatically and actually i did it with Hibernate Tools for Eclipse, but I was not satisfacted with the generated code, so decided to do the other way round and generate the tables from the classes.

It was not so easy to find out how to achive it but after a deep deep deep digging in google I’ve discovered a great tutorial site with many examples for Hibernate. Moreover the examples on that site are done with the last version of Hibernate.

Here is the code of User POJO class:

[java]

@Entity
@Table(name = "user")
public class User implements java.io.Serializable
{

[/java]

Here we say that User is an Entity bound to the tabe user

[java]
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private int id;
[/java]

The primary key of user table is a generated auto-increment int, the PK column name is id

[java]
@Column(name = "username", unique = true, nullable = false, length = 255)
private String username;

@Column(name = "password", nullable = false, length = 255)
private String password;

@Column(name = "email", unique = true, nullable = false, length = 255)
private String email;

@Temporal(TemporalType.DATE)
@Column(name="meta_ts", nullable=false)
private Date meta_ts = new Date();

@Column(name = "meta_user", nullable = false)
private int meta_user;

[/java]

Each other class variable is bound to the appropriate column on the table

[java]
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
private Set<Expense> expenses = new HashSet<Expense>(0);

@OneToMany(fetch = FetchType.LAZY, mappedBy = "meta_user")
private Set<Expense> created_expenses = new HashSet<Expense>(0);

@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
private Set<DefaultShare> default_shares = new HashSet<DefaultShare>(0);

@OneToMany(fetch = FetchType.LAZY, mappedBy = "meta_user")
private Set<DefaultShare> created_default_shares = new HashSet<DefaultShare>(0);

@OneToMany(fetch = FetchType.LAZY, mappedBy = "meta_user")
private Set<ExpenseCategory> categories = new HashSet<ExpenseCategory>(0);

@OneToMany(fetch = FetchType.LAZY, mappedBy = "meta_user")
private Set<Project> projects = new HashSet<Project>(0);

[/java]

One to Many relations, we need to proviede the Entity linked to User and also the Foreign Key which refers to the User entity on the other table

Follows auto-generated contructors and setters/getters.

I really like annotations because they are self-explaining and could almost replace comments.

Common Expenses: Working Place

Face a new project imply selecting the tools/technologies you’ll be using during developement.

Here is a brief excursus on what I’ll use.

IDE: Eclipse

Eclipse is a very powerfull IDE, I’m using it every day at work for PHP developing that’s why i feel so comfortable with it. Furthermore Eclipse has born for Java developing so i thing this is the right choice.

DBMS: MySQL

Because it’s free and I know it quite well since I’m using it for more than 6 years, I could choose SQL Server (the one we use at work) but since it is not free and I’m developing on a Mac that’s it. Moreover MySQL has MySQL Workbench which is a very usefull tool form designing/managing databases.

ORM: Hibernate

This is a project requirement, but i would have use it the same as it seems the most used out there and i want to learn it.