Basic getting started example using hibernate annotation
By candid | Posted :
Aug 21, 2015
| Updated :
Aug 21, 2015
Hibernate configuration file
Hibernate is designed to operate in many different environments and, as such, there is a broad range of configuration parameters.
SessionFactory is a global factory responsible for a particular database. If you have several databases, for easier startup you should use several <session-factory> configurations in several configuration files.
package com.candidjava.hibernate.sample.dao;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "std")
public class User {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Dao class
What is SessionFactory?
You have to startup Hibernate by building a global org.hibernate.SessionFactory object and storing it somewhere for easy access in application code. A org.hibernate.SessionFactory is used to obtain org.hibernate.Session instances. A org.hibernate.Session represents a single-threaded unit of work. The org.hibernate.SessionFactory is a thread-safe global object that is instantiated once.
1.Usually an application has a single SessionFactory
2.SessionFactorys are immutable
3.The behaviour of a SessionFactory is controlled by properties supplied at configuration time