Sunday, 2 November 2014

Hibernat basic: annotations

Hibernate basic without annotations

@Entity [javax.persistence.Entity]: Specifies that the class is an entity
-name: (Optional) The entity name. Defaults to the unqualified name of the entity class. This name is used to refer to the entity in queries. The name must not be a reserved literal in the Java Persistence query language

@Table [javax.persistence.Table]: Specifies the primary table for the annotated entity. Additional tables may be specified using SecondaryTable or SecondaryTables annotation. If no Table annotation is specified for an entity class, the default values apply.
-name: (Optional) The name of the table
-catalog: (Optional) The catalog of the table
-schema: (Optional) The schema of the table.
-uniqueConstraints: (Optional) Unique constraints that are to be placed on the table.

@Column [javax.persistence.Column]: Specifies the mapped column for a persistent property or field.
-String name: (Optional) The name of the column. Defaults to the property or field name.
-boolean unique: (Optional) Whether the column is a unique key. This is a shortcut for the UniqueConstraint annotation at the table level and is useful for when the unique key constraint corresponds to only a single column. This constraint applies in addition to any constraint entailed by primary key mapping and to constraints specified at the table level.
-boolean nullable: (Optional) Whether the database column is nullable.
-int length: (Optional) The column length. (Applies only if a string-valued column is used.) Default 255
-int precision: (Optional) The precision for a decimal (exact numeric) column. (Applies only if a decimal column is used.) Value must be set by developer if used when generating the DDL for the column.
-int scale: (Optional) The scale for a decimal (exact numeric) column. (Applies only if a decimal column is used.)
-String table: (Optional) The name of the table that contains the column. If absent the column is assumed to be in the primary table.
-String columnDefinition: (Optional) The SQL fragment that is used when generating the DDL for the column. Defaults to the generated SQL to create a column of the inferred type.
-boolean insertable: (Optional) Whether the column is included in SQL INSERT statements generated by the persistence provider.
-boolean updatable: (Optional) Whether the column is included in SQL UPDATE statements generated by the persistence provider.

@GeneratedValue [javax.persistence.GeneratedValue]:  Provides for the specification of generation strategies for the values of primary keys.
-GenerationType strategy: (Optional) The primary key generation strategy that the persistence provider must use to generate the annotated entity primary key.
-String generator: (Optional) The name of the primary key generator to use as specified in the SequenceGenerator or TableGenerator annotation. Default:javax.persistence.GenerationType.AUTO

javax.persistence.GenerationType
-AUTO: Indicates that the persistence provider should pick an appropriate strategy for the particular database.
-IDENTITY: Indicates that the persistence provider must assign primary keys for the entity using a database identity column.
-SEQUENCE: Indicates that the persistence provider must assign primary keys for the entity using a database sequence.
-TABLE: Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.

@Id [javax.persistence.Id]: Specifies the primary key of an entity. The field or property to which the Id annotation is applied should be one of the following types: any Java primitive type; any primitive wrapper type; String; java.util.Date; java.sql.Date; java.math.BigDecimal; java.math.BigInteger. The mapped column for the primary key of the entity is assumed to be the primary key of the primary table. If no Column annotation is specified, the primary key column name is assumed to be the name of the primary key property or field.

@Temporal [javax.persistence.Temporal]: This annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar. It may only be specified for fields or properties of these types.
-TemporalType value: The type used in mapping java.util.Date or java.util.Calendar.
javax.persistence.TemporalType: Type used to indicate a specific mapping of java.util.Date or java.util.Calendar.
DATE: Map as java.sql.Date
TIME: Map as java.sql.Time
TIMESTAMP: Map as java.sql.Timestamp

Example:
package com.njoshi.hibernate.employeemanager.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "employees", uniqueConstraints = @UniqueConstraint(columnNames = {
"emp_no", "emp_name" }))
public class Employee {

@Id
@Column(name = "emp_no", nullable = false)
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private Long empNo;

@Column(name = "emp_name", length = 16, nullable = false)
private String empName;

@Column(name = "first_name", length = 14, nullable = false)
private String firstName;

@Column(name = "last_name", length = 16, nullable = false)
private String lastName;

@Temporal(TemporalType.DATE)
@Column(name = "hire_date", nullable = false)
private Date hireDate;

@Temporal(TemporalType.DATE)
@Column(name = "birth_date", nullable = false)
private Date birthDate;

public Employee() {

}

public Employee(String firstName, String empName, String lastName,
Date birthDate, Date hireDate) {
this.empName = empName;
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
this.hireDate = hireDate;
}

public Long getEmpNo() {
return empNo;
}

public void setEmpNo(Long empNo) {
this.empNo = empNo;
}

public Date getBirthDate() {
return birthDate;
}

public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Date getHireDate() {
return hireDate;
}

public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}

@Override
public String toString() {
return "Employee [empNo=" + empNo + ", firstName=" + firstName
+ ", lastName=" + lastName + ", hireDate=" + hireDate
+ ", birthDate=" + birthDate + "]";
}

}

Source: Employee Manager

No comments:

Post a Comment