POJO Annotation (hibernate) Example

August 26, 2009

The following is an example of what a hibernate annotated java file may look like…

Source :

/*
 * filename: Tax.java
 * descrip.: represents the tax object ( tax domain )
 */

package com.foo.tax.model;

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

/**
 * Represents the Tax entity.
 *
 * please note if the table name is the same as the class name
 * then the @table annotation doesn't need to be specified
 */
@Entity
@SequenceGenerator( name="fooTaxIdSeq", sequenceName="FOO_TAX_ID_SEQ", allocationSize=1)
@Table(name="FOO_TAX")
public class Tax
{
    private Long id = null;
    private String name = null;
    private double percentage = 0;

    @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="fooTaxIdSeq")
    @Column(name="ID", nullable=false, unique=true)
    public Long getId()
    {
	return id;
    }

    // no need for the name as we want the defualt to be used
    @Column( nullable=false, unique=true )
    public String getName()
    {
	return name;
    }

    public void setName(String name)
    {
	this.name = name;
    }

    // equivalent to NUMBERIC( 256, 2 )
    @Column( nullable=false, precision=256, scale=2 )
    public double getPercentage()
    {
	return percentage;
    }

    public void setPercentage(double type)
    {
	this.percentage = type;
    }
}

Notes :

- assuming that database id generation is in place, as opposed to application generated.

- the ‘@SequenceGenerator’ has been declared at the class level, and it is used at the method level with, ‘@Id @GeneratedValue’. Please note the ‘generator=”fooTaxIdSeq”‘.

- also please note that the ‘@SequenceGenerator’ specification is somewhat database dependant, and that in this case the above combination works for PostGres. A different database may require a different specification.

And the following is a corresponding table creation sql file for the above pojo…

Source:

-- filename: 1_tax_init.sql
-- description:
--   creates a 'foo_tax' table, along with a 'foo_tax_id_seq' sequence
--   that will be used to increment the id with every new tax entity

CREATE TABLE FOO_TAX (
	ID VARCHAR(36) PRIMARY KEY,
	VERSION INTEGER NOT NULL,
	ADDED_BY VARCHAR(36) NOT NULL,
	DATE_CREATED TIMESTAMP NOT NULL DEFAULT NOW(),
	MODIFIED_BY VARCHAR(36),
	DATE_MODIFIED TIMESTAMP,
	REMARKS TEXT,

	NAME VARCHAR(256) NOT NULL,
	PERCENTAGE NUMERIC(256,2) NOT NULL
);
ALTER TABLE FOO_TAX OWNER TO FOO_OWNER;

CREATE SEQUENCE FOO_TAX_ID_SEQ;
ALTER TABLE FOO_TAX_ID_SEQ OWNER TO FOO_OWNER;

Notes :

- the ‘create sequence foo_tax_id_seq;’ statement could have been omitted if the keyword ’serial’ was specified for the id column above.

CREATE TABLE FOO_TAX (
	ID SERIAL VARCHAR(36) PRIMARY KEY,
        ...

- this instructs the database that the id will be database generated, and the default name for the sequence table would have been the table name plus ‘id_seq’, so in this case ‘foo_tax_id_seq’.

… wa Allahu A’lam !

/*
* filename: Tax.java
* descrip.: represents the tax object ( tax domain )
*
* Copyright 2009 X-IT Management Ltd.
*/

package com.astra.tax.model;

import com.astra.common.model.BaseEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
* Represents the Tax entity.
*/
/* please note if the table name is the same as the class name
* then the @table annotation doesn’t need to be specified
*/
@Entity
@SequenceGenerator( name=”astraTaxIdSeq”, sequenceName=”ASTRA_TAX_ID_SEQ”, allocationSize=1)
@Table(name=”ASTRA_TAX”)
public class Tax extends BaseEntity
{
private String name = null;
private double percentage = 0;

@Override
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator=”astraTaxIdSeq”)
@Column(name=”ID”, nullable=false, unique=true)
public Long getId()
{
return id;
}

// no need for the name as we want the defualt to be used
@Column( nullable=false, unique=true )
public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

// equivalent to NUMBERIC( 256, 2 )
@Column( nullable=false, precision=256, scale=2 )
public double getPercentage()
{
return percentage;
}

public void setPercentage(double type)
{
this.percentage = type;
}
}

Entry Filed under: Annotations, Hibernate, Java, Netbeans. Tags: , , .

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Posts

Recent Comments

Categories

Top Posts

Links

Blog Stats

Meta