To get started creating a new Object and table, please complete the instructions below.
Table of Contents
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.
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;
Now, you can compose your first git commit as follows.
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"