Menu

One Artifact Tutorial-01-Domain Object

Jack Liu

To get started creating a new Object and table, please complete the instructions below.
Table of Contents

  1. Create a new Git branch
  2. Create a new POJO and add JPA Annotations
  3. Create a new database table from the object using Maven
  4. Add and commit all related changes

Create a new Git branch

It's better to create a new branche for the new feature that you will develop. After it's completed, we can merge this branch into the master branch.

for example, we can create a branch "project-mgmt" by following the instructions below
1. right click eclipse project name
2. select context menu: Team - Switch To - New Branch...
3. select Source ref: refs/heads/master and input the branch name "project-mgmt" in this example.
4. make sure "checkout new branch" is selected and click "finish" button.

Create a new POJO and add JPA Annotations

The first thing you need to do is create an object to persist. Create a simple "Project" object in the src/main/java/**/model directory that has an id, a name and a description (as properties).

package com.ibm.ics.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

@Entity
public class Project extends BaseObject {

    private static final long serialVersionUID = 3155966591241028535L;
    private Long id;
    private String name;
    private String description;
    private Integer version;
    private Date createDate;
    private Date updateDate;

}

Now that you have this POJO created, you need to add JPA annotations. These annotations are used by Hibernate to map objects → tables and properties (variables) → columns.

First of all, add an @Entity annotation that signifies what table this object relates to. The "name" is optional; the class name is used if it's not specified. Make sure you import annotations from javax.persistence.* rather than from Hibernate.

@Entity
public class Person extends BaseObject {

You also have to add an @Id annotation to signify the primary key. The @GeneratedValue annotation should also be specified to indicate the primary key generation strategy.

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
    return id;
}

Create a new database table from the object using Maven
Open src/main/resources/hibernate.cfg.xml and register your Person object with the following XML:

    <mapping class="com.ibm.ics.model.Project"/>

Save the file and run mvn test-compile hibernate3:hbm2ddl from the command line. This will generate your database schema with the "project" table included.

create table Project (id bigint not null auto_increment, createDate datetime, 
description varchar(256), name varchar(256), updateDate datetime, version integer, 
primary key (id)) ENGINE=InnoDB;

Add and commit all related changes

Now, you can compose your first git commit as follows.

  1. Right click the new created domain object, Project.java in our example, then select Team -
    Add to Index
  2. Right click the modified hibernate.cfg.xml file, then select Team -
    Add to Index
  3. Right client the eclipse project, then select Team - Commit..., make sure only the above two files are selected in Commit Dialog window. click "Commit" button after inputing a message like "add Project domain object".

Next

After you've created a POJO and generated a schema from it, how do you persist that object? Please continue on the next section "Persistence"