All generators implement the interface org.hibernate.id.IdentifierGenerator.. Some applications can choose to provide their own specialized implementations, however, Hibernate provides a range of built-in implementations. The shortcut names for the built-in generators are as follows:
increment
generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. Do not use in a cluster.
identity
supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.
sequence
uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned identifier is of type long, short or int
hilo
uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column (by default hibernate_unique_key and next_hi respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database.
seqhilo
uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a named database sequence.
uuid
uses a 128-bit UUID algorithm to generate identifiers of type string that are unique within a network (the IP address is used). The UUID is encoded as a string of 32 hexadecimal digits in length.
guid
uses a database-generated GUID string on MS SQL Server and MySQL.
native
selects identity, sequence or hilo depending upon the capabilities of the underlying database.
assigned
lets the application assign an identifier to the object before save() is called. This is the default strategy if no element is specified.
select
retrieves a primary key, assigned by a database trigger, by selecting the row by some unique key and retrieving the primary key value.
foreign
uses the identifier of another associated object. It is usually used in conjunction with a primary key association.
sequence-identity
a specialized sequence generation strategy that utilizes a database sequence for the actual value generation, but combines this with JDBC3 getGeneratedKeys to return the generated identifier value as part of the insert statement execution. This strategy is only supported on Oracle 10g drivers targeted for JDK 1.4. Comments on these insert statements are disabled due to a bug in the Oracle drivers.
Sunday, December 16, 2012
Hibernate ID generator
Hibernate Basic example For Simple persistance entity
Here I describe How to write basic Hibernate persistence class and How to configure it.
1st it is need to configure hibernate properties.This can be done in 3 ways.
1.hibernate property file
2.hibernate configuration file
3.Set configurations programmatically.
2. Employee object relationally Mapped in to EmployeeInfo Table.
Mapping can be done using annotation or creating Hibernate managing files(hbm)
@Entity-declare class is an Entity
@Id-Id
@Table(name="EmployeeInfo") -table and table name declaration
@Column(name="EmployeeID")-column and column name declaration
@Column(nullable=false)-column contain values declaration
@Transient-If you dont want to create a column for property you need to mark property with transient anotation
@Temporal(TemporalType.DATE) Temporal data can have DATE, TIME, or TIMESTAMP precision (ie the actual date, only the time, or both). Use the @Temporal annotation to fine tune that.
@Basic -By default, the @Basic annotation is used on every field
Employee Bean class
import java.util.Calendar;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
@Entity
@Table(name="EmployeeInfo")
public class Employee {
private int empId;
private String empName;
private String empEmailAddress;
private String empPassword;
private boolean isPermanent;
private Calendar empJoinDate;
private Date empLoginTime;
@Id
@Column(name="EmployeeID")
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
@Column(name="EmployeeName")
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
@Column(nullable=false)
public String getEmpEmailAddress() {
return empEmailAddress;
}
public void setEmpEmailAddress(String empEmailAddress) {
this.empEmailAddress = empEmailAddress;
}
@Transient
public String getEmpPassword() {
return empPassword;
}
public void setEmpPassword(String empPassword) {
this.empPassword = empPassword;
}
@Basic
public boolean isPermanent() {
return isPermanent;
}
public void setPermanent(boolean isPermanent) {
this.isPermanent = isPermanent;
}
@Temporal(TemporalType.DATE)
public Calendar getEmpJoinDate() {
return empJoinDate;
}
public void setEmpJoinDate(Calendar empJoinDate) {
this.empJoinDate = empJoinDate;
}
@Temporal(TemporalType.TIMESTAMP)
public Date getEmpLoginTime() {
return empLoginTime;
}
public void setEmpLoginTime(Date empLoginTime) {
this.empLoginTime = empLoginTime;
}
}
Following Main method contains how to load Hibernate configuration and create hibernate session and How to Persist entity in to Data source using that session.
Main method
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.addAnnotatedClass(Employee.class);
cfg.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sessionFactory = cfg
.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Employee test = new Employee();
test.setEmpId(1);
test.setEmpName("Kalana Sandhana");
test.setEmpEmailAddress("adxsaxax@a.com");
test.setEmpJoinDate(new GregorianCalendar(2012, 11, 05));
test.setEmpLoginTime(Date.valueOf("2012-06-10"));
session.save(test);
session.getTransaction().commit();
}
Hibernate Log-
Hibernate: select sequence_next_hi_value from HIBERNATETESTSCHEMA.empPKtb where empkey = 'empvalue' for update with rs
Hibernate: update HIBERNATETESTSCHEMA.empPKtb set sequence_next_hi_value = ? where sequence_next_hi_value = ? and empkey = 'empvalue'
Hibernate: insert into HIBERNATETESTSCHEMA.EmployeeInfo (empEmailAddress, empJoinDate, empLoginTime, EmployeeName, permanent, EmployeeID) values (?, ?, ?, ?, ?, ?)
1st it is need to configure hibernate properties.This can be done in 3 ways.
1.hibernate property file
2.hibernate configuration file
3.Set configurations programmatically.
2. Employee object relationally Mapped in to EmployeeInfo Table.
Mapping can be done using annotation or creating Hibernate managing files(hbm)
@Entity-declare class is an Entity
@Id-Id
@Table(name="EmployeeInfo") -table and table name declaration
@Column(name="EmployeeID")-column and column name declaration
@Column(nullable=false)-column contain values declaration
@Transient-If you dont want to create a column for property you need to mark property with transient anotation
@Temporal(TemporalType.DATE) Temporal data can have DATE, TIME, or TIMESTAMP precision (ie the actual date, only the time, or both). Use the @Temporal annotation to fine tune that.
@Basic -By default, the @Basic annotation is used on every field
Employee Bean class
import java.util.Calendar;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
@Entity
@Table(name="EmployeeInfo")
public class Employee {
private int empId;
private String empName;
private String empEmailAddress;
private String empPassword;
private boolean isPermanent;
private Calendar empJoinDate;
private Date empLoginTime;
@Id
@Column(name="EmployeeID")
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
@Column(name="EmployeeName")
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
@Column(nullable=false)
public String getEmpEmailAddress() {
return empEmailAddress;
}
public void setEmpEmailAddress(String empEmailAddress) {
this.empEmailAddress = empEmailAddress;
}
@Transient
public String getEmpPassword() {
return empPassword;
}
public void setEmpPassword(String empPassword) {
this.empPassword = empPassword;
}
@Basic
public boolean isPermanent() {
return isPermanent;
}
public void setPermanent(boolean isPermanent) {
this.isPermanent = isPermanent;
}
@Temporal(TemporalType.DATE)
public Calendar getEmpJoinDate() {
return empJoinDate;
}
public void setEmpJoinDate(Calendar empJoinDate) {
this.empJoinDate = empJoinDate;
}
@Temporal(TemporalType.TIMESTAMP)
public Date getEmpLoginTime() {
return empLoginTime;
}
public void setEmpLoginTime(Date empLoginTime) {
this.empLoginTime = empLoginTime;
}
}
Following Main method contains how to load Hibernate configuration and create hibernate session and How to Persist entity in to Data source using that session.
Main method
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.addAnnotatedClass(Employee.class);
cfg.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sessionFactory = cfg
.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Employee test = new Employee();
test.setEmpId(1);
test.setEmpName("Kalana Sandhana");
test.setEmpEmailAddress("adxsaxax@a.com");
test.setEmpJoinDate(new GregorianCalendar(2012, 11, 05));
test.setEmpLoginTime(Date.valueOf("2012-06-10"));
session.save(test);
session.getTransaction().commit();
}
Hibernate Log-
Hibernate: select sequence_next_hi_value from HIBERNATETESTSCHEMA.empPKtb where empkey = 'empvalue' for update with rs
Hibernate: update HIBERNATETESTSCHEMA.empPKtb set sequence_next_hi_value = ? where sequence_next_hi_value = ? and empkey = 'empvalue'
Hibernate: insert into HIBERNATETESTSCHEMA.EmployeeInfo (empEmailAddress, empJoinDate, empLoginTime, EmployeeName, permanent, EmployeeID) values (?, ?, ?, ?, ?, ?)
Saturday, December 15, 2012
EJB 2.1 vs EJB 3
Problems with EJB 2.x specification
From EJB 3.0
- . Tight coupling between the developer-written code and the interface classes from the EJB framework package. It also requires the implementation of several unnecessary callback methods (ejbCreate, ejbPassivate, ejbActivate component interface must extend an interface from the EJB framework package and the business logic implementation class must implement an interface from the EJB framework package ) not directly related to the main design goal of the EJB, and the handling of unnecessary exceptions.
- The reliance on JNDI every time you have to access a resource (such as a data source, or an EJB home reference) is a repetitive and tedious operation of J2EE development
- EJB deployment descriptors are overly verbose, complex, and error prone.
- EJBs are difficult to test, since the application needs a J2EE container to provide all the services required to correctly run the EJB component.
- The container-managed persistence model is complex to develop and manage.
From EJB 3.0
- EJB 3.0 specification centers on a plain old Java object (POJO) programming model that uses Java annotations to capture information that deployment descriptors used to contain. Deployment descriptors are now optional in most cases.so there is no requirement for home interfaces.
- Interceptor facility to invoke user methods at the invocation of business methods or at life cycle events.
- Reduction in the requirements for usage of checked exception.
- A complete new persistence model (based on the JPA standard), that supersedes EJB 2.x entity beans
Thursday, December 13, 2012
Hibernate Overview
Introduction
to ORM
- Object-oriented programming technologies are typically used to implement business logic.
- Object-oriented technology supports the building of applications out of networks of objects with both data and behavior.
- Relational databases are used for persistent data storage
- Relational technology supports the storage of data in tables and manipulation of that data using data manipulation language (DML)
- Impedance mismatch between the two paradigms: objects vs. relations
- ORM toolkits are designed to address this impedance mismatch
Important
aspects of ORM toolkits
Mapping
specification
Class
inheritance
Query
language
Persistence
Fetch
strategies
Caching
ORM Solutions
Hibernate
– Open Source
iBatis
SQL Maps – Open Source
TopLink
– Commercial
JPA – Java EE 5 Solution
Nhibernate
(.NET environment (C#))
Overview
of Hibernate
Started
in 2001 by Gavin King as an alternative
to using EJB2-style entity beans
Open Source
light-weight ORM solution
Doesn’t
require container (light-weight)
Object
based model
It is
around from quite some time
Very well
matured and adopted by a large
developer
community
Maintained
by a team at JBoss (Redhat)
led by Steve Ebersole
Latest
Version 4.x
Why
Hibernate?
Hibernate
was introduced to address the issues of
Entity Beans
Hibernate
is built on top of JNDI, JDBC, JTA
It uses
XML based configuration files for mapping
Supports
many databases like Sybase, Oracle,
MySQL,other Object
Oriented Databases etc.
Easy migration
from one vendor database to another
Hibernate
generates the JDBC Code based on the underlying vendor database
Hibernate
APIs are very simple to learn and use
Provides
quite powerful object query language
known as Hibernate Query
Language (HQL)
Hibernate Architecture
Subscribe to:
Posts (Atom)