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.

Leave a Reply

Your email address will not be published. Required fields are marked *