Monday 14 November 2016

HSQLDB & Hibernate Example

HSQLDB is an in-memory database, I explained about this in my previous article. In this post, I am going to explain how to configure HSQLDB with Hibernate.

Employee.java
package com.sample;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Employee {
 @Id
 private int id;
 private String firstName;
 private String lastName;
 private String designation;
 private int age;
 private double salary;
 
 public Employee(){
  
 }

 public Employee(int id, String firstName, String lastName, String designation, int age, double salary) {
  super();
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
  this.designation = designation;
  this.age = age;
  this.salary = salary;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getDesignation() {
  return designation;
 }

 public void setDesignation(String designation) {
  this.designation = designation;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public double getSalary() {
  return salary;
 }

 public void setSalary(double salary) {
  this.salary = salary;
 }

}

hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

 <session-factory>
  <property name="hibernate.hbm2ddl.auto">update</property>

  <!-- Database Connection settings -->
  <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
  <property name="connection.url">jdbc:hsqldb:file:employeeDb;shutdown=true;hsqldb.write_delay=false;</property>
  <property name="connection.username">sa</property>
  <property name="connection.password"></property>

  <!-- Enable the logging of all the generated SQL statements to the console -->
  <property name="show_sql">true</property>

  <!-- Format the generated SQL statement to make it more readable, -->
  <property name="format_sql">false</property>

  <!-- Hibernate will put comments inside all generated SQL statements to 
   hint what’s the generated SQL trying to do -->
  <property name="use_sql_comments">false</property>

  <!-- This property makes Hibernate generate the appropriate SQL for the 
   chosen database. -->
  <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

  <!-- mappings for annotated classes -->
  <mapping class="com.sample.Employee" />

 </session-factory>

</hibernate-configuration>


TestEmployee.java
package com.sample;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class TestEmployee {
 private static final SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();;

 public static void main(String args[]) {
  Employee emp1 = new Employee(5, "Hari Krishna", "Gurram", "Senior Software Developer", 26, 80000);
  Employee emp2 = new Employee(6, "Shreyas", "Desai", "Team Manager", 35, 150000);
  Employee emp3 = new Employee(7, "Piyush", "Rai", "Senior Software Developer", 26, 100000);
  Employee emp4 = new Employee(8, "Maruti", "Borker", "Software Developer", 26, 60000);

  Session session = sessionFactory.openSession();
  session.beginTransaction();
  session.save(emp1);
  session.save(emp2);
  session.save(emp3);
  session.save(emp4);
  session.getTransaction().commit();

  session.close();

  sessionFactory.close();

 }
}

Output
Hibernate: create table Employee (id integer not null, age integer not null, designation varchar(255), firstName varchar(255), lastName varchar(255), salary double not null, primary key (id))
Hibernate: insert into Employee (age, designation, firstName, lastName, salary, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into Employee (age, designation, firstName, lastName, salary, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into Employee (age, designation, firstName, lastName, salary, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into Employee (age, designation, firstName, lastName, salary, id) values (?, ?, ?, ?, ?, ?)






Previous                                                 Next                                                 Home

No comments:

Post a Comment