Hibernate Mapping (Last Update 11/01/2012)

Introduction to the Data Layer

On the datalayer you define the objects that map to your database tables and define validation rules for its members. Hibernate+JPA annotations  and Oval Validation framework, are used.

 

Definition of the Hibernate object

We assume that the tables on the db have already been created

The analyst should deliver to the developer a document like the following that contains the definition of the object that maps to a db table and the validation rules of the different properties.

Object Name Country
Object Package com.exampleServices.businesslayer.objectmodel.country
Sub Project Services
Description Represents a country
Data Name Type Validation Descr
countryId Long   Primary Key
name String Not Null
Not Empty
Size:100
Name of the country
countryCode String Not Null
Not Empty
Size:100
Country Code
intPhoneCode String Size:100 Int Access Code
population Long Not Null Country Population

 

The developer, using the above document can define the class, the hibernate mappings and the object validation rules using annotations (for more information check Oval Validation Framework ).

The creation of the class can be done manually or by reverse engineering the table on the database using Hibernate Tools Plugin.

!!!Important: Make sure that you do not any primitive types e.g. long, int!!!!!!

 

Country.java
package com.example.service.objectmodel.country;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import net.sf.oval.constraint.Length;
import net.sf.oval.constraint.NotEmpty;
import net.sf.oval.constraint.NotNull;

@Entity
@Table(name = "COUNTRY")
public class Country {
	
	@Id
	@Column(name = "COUNTRYID")
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="basis_gen")
	@SequenceGenerator(name="basis_gen", sequenceName="exampleapp_seq")
	public Long countryId;
	
	@Column
	@NotNull 
	@NotEmpty
	@Length(max = 100)
	private String name;
	
	@Column
	@NotNull 
	@NotEmpty
	@Length(max = 100)
	private String countryCode;
	
	@Column
	@NotNull 
	@NotEmpty
	@Length(max = 50)
	private String continent;
	
	
	@Column
	@NotNull 
	@NotEmpty
	@Length(max = 100)
	private String intPhoneCode;
	
	
	@Column
	@NotNull 
	@NotEmpty
	private Long population;
	
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "country")
	private Set cities = new HashSet(0);
	
	
	public Long getCountryId() {
		return countryId;
	}
	public void setCountryId(Long countryId) {
		this.countryId = countryId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getIntPhoneCode() {
		return intPhoneCode;
	}
	public void setIntPhoneCode(String intPhoneCode) {
		this.intPhoneCode = intPhoneCode;
	}
	public Long getPopulation() {
		return population;
	}
	public void setPopulation(Long population) {
		this.population = population;
	}
	

	public String getCountryCode() {
		return countryCode;
	}
	public void setCountryCode(String countryCode) {
		this.countryCode = countryCode;
	}
	
	public String getContinent() {
		return continent;
	}
	public void setContinent(String continent) {
		this.continent = continent;
	}
	
	
	public Set getCities() {
		return cities;
	}
	public void setCities(Set cities) {
		this.cities = cities;
	}

}

hibernate-context.xml decleration

You have created your hibernate class, now you have to make sure that is discovered.

Edit hibernate-context.xml and add the class package to packagesToScan

e.g: p:packagesToScan="org.jprovocateur2, com.example"

Testing

Start your server and go to yourserver:yourport/sampleweb/application/view/listPOJOs. On the list you should see your newly created object.

  • My class is not there : It is not discovered by hibernate. On sampleweb check hibernate-context.xml and find "p:packagesToScan"  it should look like p:packagesToScan="org.jprovocateur2, com.example". Add the package of your class there. Redeploy and restart!

On the listPOJOs page click on  Preview button, a CRUD page is generated based on your Hibernate Entity.

In few days a download button will be added in order to download the xml views/servlets/services.

 

 

 

 
Design downloaded from free website templates.
 
Web Templates created with FlashMint. .