Object Oriented Java

champak
₹2,000.00 ₹300.00
  • 27 students
  • 25 lessons
  • 0 quizzes
  • 10 week duration

Object Oriented Java

Setting up Hibernate?

Hibernate

Hibernate ORM(Object Relational Mapping) or simply Hibernate is an object-relational mapping tool for the Java programming language. It maps an object-oriented model to a relational database. Hibernate provides a framework using which the programmer can map his classes and objects to tables in a database. The programmer doesn’t have to go into low level JDBC based database access.

 

Hibernate is free.

Hibernate maps from Java classes to database tables, and auto converts between Java data types and SQL data types. Hibernate works with its own query language called HQL(Hibernate Query Language) . It generates SQL statements of the dialect of the connected database  and relieves the developer from the manual handling and object conversion of the result set.

To start with we shall create a database in MySql and create a database called hibernate.

Database in MySql for Hibernate

 

Create a table called Book.

 

Next step.

Download Eclipse

and install.

Open Eclipse

File System

Make a class to map with the database table


package com.champak.firsthibernate;

public class Book {
private int bookid;
private String bookname,booksubject;
public int getBookid() {
return bookid;
}
public void setBookid(int bookid) {
this.bookid = bookid;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getBooksubject() {
return booksubject;
}
public void setBooksubject(String booksubject) {
this.booksubject = booksubject;
}

}

Declare the mapping book.hbm.xml


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 5.3//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-5.3.dtd">

<hibernate-mapping>
<class name="com.champak.firsthibernate.Book" table="book">
<id name="bookid">
<generator class="assigned"></generator>
</id>

<property name="bookname"></property>
<property name="booksubject"></property>

</class>

</hibernate-mapping>

The hibernate mapping with the database is in hibernate.cfg.xml


<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>

<property name = "hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>

<property name = "hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>

<!--hibernate is the database name -->

<property name = "hibernate.connection.url">
jdbc:mysql://localhost/hibernate
</property>

<property name = "hibernate.connection.username">
root
</property>

<property name = "hibernate.connection.password"></property>

<!-- List of XML mapping files -->
<mapping resource = "book.hbm.xml"/>

</session-factory>
</hibernate-configuration>

Download Jar Files

 

Add Jar Files to class buildpath

 

 

Create a class HibernateManager

 

</pre>
package com.champak.firsthibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class HibernateManager {

public static SessionFactory GetFactory() {
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();

SessionFactory factory = meta.getSessionFactoryBuilder().build();
return factory;
}

public static void delete(int id )
{
SessionFactory factory = GetFactory();
Session session = factory.openSession();
Transaction t = session.beginTransaction();

Book b1=new Book();
b1.setBookid(id);


session.delete(b1);
t.commit();
System.out.println("successfully deleted");
factory.close();
session.close();
}

public static void update(int id, String bookname,String subject )
{
SessionFactory factory = GetFactory();
Session session = factory.openSession();
Transaction t = session.beginTransaction();

Book b1=new Book();
b1.setBookid(id);
b1.setBookname(bookname);
b1.setBooksubject(subject);

session.update(b1);
t.commit();
System.out.println("successfully updated");
factory.close();
session.close();
}
public static void insert( String bookname,String subject )
{
SessionFactory factory = GetFactory();
Session session = factory.openSession();
Transaction t = session.beginTransaction();

Book b1=new Book();

b1.setBookname(bookname);
b1.setBooksubject(subject);

// session.save(b1);
session.saveOrUpdate(b1);
t.commit();
System.out.println("successfully saved");
factory.close();
session.close();
}
}
<pre>

Run as Java Application and view the results.

end

Leave a Reply

Your email address will not be published. Required fields are marked *