You are on page 1of 3

Hibernate

Prerequisite

…………………

Application

Database

Hibernate

……………..

ORM(object relation mapping) for persisting the data

Data can be a variable or object

Every obj have some data

Storing data is called persistence

To connect java and database we use JDBC

We use Sql language for jdbc.

Class Student{rolln, name, marks}

Hibernate,ibatis,toplink

To

1. Create session using session factory


2. Provide configuration with sessionfactory using the ways xml or java configuration
3. In configutaion, we shud provide(driver name.url,un and pwd) to load in sess factory object
4. Get() to fetch value

Implementation-

1.Create Maven Project (it will give proj structure, configuration files get loaded)

2.Internal/webapp

3.Mysql
App.java

…………..

Alien class

Public class Alien{

Private int aid;

Private String aname;

}getters setters

Alien

……….

Dependency for hibernate,mysql.\

Configuration con=new Configuration();

SessionFactory sf=con.buildSessionFactory();

Session session =sf.openSession();

Dependency in pom.xml

Plugin hibernate using clipse marketplace

Configuration file

…………………….

New->hibernate.cfg.xml->specify

Configuration con=new Configuration().configure().addAnnotatedClass(Alien.class);


serviceRegistry reg= new
ServiceRegistryBuilder().applySettings(con.getProperties()).buildServiceRegistry();

SessionFactory sf=con.buildSessionFactory(reg);

Session session=sf.openSession();

Transaction tx=session.beginTransaction)

Session.save(object of Alien with data);

Tx.commit();

Specify create table in configuration file.

Prop name=”hbm2ddl.auto”>create/update</prop>

You might also like