You are on page 1of 5

Hibernate

ORM tool
Used in data layer of application
Implements JPA

The Problem

Mapping member variables to columns


Mapping relationships (user table and address table using relationship)
Handling data type (boolean manual handling)
Managing changes to object state

Writing a Hibernate Application

Hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property
name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property
name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">system</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property
name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- Enable Hibernate's current session context -->
<property
name="current_session_context_class">org.hibernate.context.ManagedSessionConte
xt</property>
<!-- Disable the second-level cache -->
<property
name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping
<mapping
<mapping
<mapping
<!--<mapping

class="com.vishal.dto.UserDetails" />
class="com.vishal.dto.Vechicle" />
class="com.vishal.dto.TwoWheeler"/>
class="com.vishal.dto.FourWheeler"/>
resource="com/vishal/dto/UserDetails.hbm.xml"/>

<mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/>
--></session-factory>
</hibernate-configuration>

UserDetails Entity Class

@Entity(name = "USER_DETAILS")
public class UserDetails {
@Id
@Column(name = "USER_ID")
@GeneratedValue(strategy=GenerationType.AUTO)
private int userId;
@Column(name = "USER_NAME")
private String userName;
@Temporal(TemporalType.DATE)
@Column(name = "JOIN_DATE")
private Date joinDate;

Saving Objects using Hibernate APIs:

Create a session factory


Create a session from Session Factory
Use the session to save object

SessionFactory sf = new Configuration().configure().buildSessionFactory();


Session session = sf.openSession();
session.beginTransaction();
session.save(ud);
session.getTransaction().commit();
session.close();

Hibernate picks up the value from the getter method to save into the
Database. In the below snippet of code it will insert <userName>from get as
UserName. We can also put @Column annotation to the getter method.
@Column(name = "USER_NAME")
public String getUserName() {
return userName + "from get";
}

Few more annotations:


@Entity
@Table(name = "USER_DETAILS")
public class UserDetails {

@Transient
@Temporal(TemporalType.TIMESTAMP)
@Lob

Retrieving data using get:


session = sf.openSession();
session.beginTransaction();
UserDetails udFromDb = (UserDetails)session.get(UserDetails.class,
1);
session.close();

Natural Key and Surrogate key:


@Id
@Column(name = "USER_ID")
@GeneratedValue(strategy=GenerationType.AUTO)
private int userId;

If do not specify strategy, then it will add AUTO as default

AUTO
IDENTITY
SEQUENCE
TABLE

Values Types and Embedding Objects:


Entity Objects v/s Value Objects
It independent it contains data that provide meaning about itself
Value object is an object that has data even that has to save to database
but it doesnt have meaning as of itself. It provides meaning to some other
object
Without having a user object Address object doesnt make sense.

@Entity(name = "USER_DETAILS")
public class UserDetails {
@Id
@Column(name = "USER_ID")
@GeneratedValue(strategy=GenerationType.AUTO)
private int userId;
@Column(name = "USER_NAME")
private String userName;
@Embedded
private Address address;

@Embeddable
public class Address {
@Column(name="STREEET")
private String street;

If I have two Address Objects inside the User class (HomeAddress and
OfficeAddress)
In this case there will be two STREET columns. Will override the attribute.
@Embedded
@AttributeOverrides({
@AttributeOverride(name="street",column=@Column(name="HOME_STREET")),
@AttributeOverride(name="state",column=@Column(name="HOME_STATE")),
@AttributeOverride(name="pinCode",column=@Column(name="HOME_PIN")),
@AttributeOverride(name="city",column=@Column(name="HOME_CITY"))
})
private Address homeAdress;
@Embedded
@AttributeOverrides({

@AttributeOverride(name="street",column=@Column(name="OFFICE_STREET")),
@AttributeOverride(name="state",column=@Column(name="OFFICE_STATE")),
@AttributeOverride(name="pinCode",column=@Column(name="OFFICE_PIN")),
@AttributeOverride(name="city",column=@Column(name="OFFICE_CITY"))
})
private Address officeAdress;

You might also like