Hibernate Java Tutorial

File: Main.java

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
public class Main {
  public static void main(String[] args) throws Exception {
    HibernateUtil hibernateUtil = new HibernateUtil();
    hibernateUtil
        .executeSQLCommand("create table Product(id int, name varchar, description varchar, price decimal(6,2), supplierid int)");
     
    hibernateUtil
    .executeSQLCommand("create table Supplier (id int , name varchar)");
    hibernateUtil
    .executeSQLCommand("create table Software(id int, name varchar, description varchar, price decimal(6,2), supplierid int, version varchar)");
    
    Session session = hibernateUtil.getSession();
    Supplier superCorp = new Supplier();
    superCorp.setName("Supplier1");
    session.save(superCorp);
    
    Supplier megaInc = new Supplier();
    megaInc.setName("Supplier2");
    session.save(megaInc);        
    Product mouse = new Product("Product1","first product", 20.0);
    mouse.setSupplier(superCorp);
    superCorp.getProducts().add(mouse);
    session.flush();
    Product mouse2 = new Product("Product2","second product", 22.0);
    mouse2.setSupplier(superCorp);
    superCorp.getProducts().add(mouse2);        
    
    Product keyboard = new Product("Product3", "third product", 30.0);
    keyboard.setSupplier(megaInc);
    megaInc.getProducts().add(keyboard);
    Software webBrowser = new Software("Web Browser","new browser", 75.0, "2.0");
    webBrowser.setSupplier(superCorp);
    superCorp.getProducts().add(webBrowser);
    Software email = new Software("Email","email client", 49.99, "4.1 Edition");
    email.setSupplier(megaInc);
    megaInc.getProducts().add(email);    
    session.flush();
    
    String hql = "from Supplier s inner join fetch s.products as p";
    Query query = session.createQuery(hql);
    List results = query.list();
    System.out.println(results);
    
    session.close();
    hibernateUtil.checkData("select * from Product");
    hibernateUtil.checkData("select * from Software");
    hibernateUtil.checkData("select * from Supplier");
  }
}
File: Product.hbm.xml


   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

   
      
         
      
      
      
      
      
   
   
   
     select product.price from Product product 
     
   
   
     select product.price from Product as product where product.price = 25.0
       

File: Product.java

public class Product
{
    private int id;
    private Supplier supplier;
    
    private String name;
    private String description;
    private double price;
    
    public Product()
    {
        super();
    }
    
    public Product(String name, String description, double price)
    {
        super();
        this.name = name;
        this.description = description;
        this.price = price;
    }
    
    public String getDescription()
    {
        return description;
    }
    public void setDescription(String description)
    {
        this.description = description;
    }
    public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id = id;
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
 
    public Supplier getSupplier()
    {
        return supplier;
    }
    public void setSupplier(Supplier supplier)
    {
        this.supplier = supplier;
    }
    
    public double getPrice()
    {
        return price;
    }
    public void setPrice(double price)
    {
        this.price = price;
    }
}
File: Software.hbm.xml


   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

   
      
      
   

File: Software.java

public class Software extends Product
{
    private String version;
    
    public Software()
    {
        super();
    }
    
    public Software(String name, String description, double price, String version)
    {
        super(name, description, price);
        this.setVersion(version);
    }
    public String getVersion()
    {
        return version;
    }
    public void setVersion(String version)
    {
        this.version = version;
    }
}
File: Supplier.hbm.xml


   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

   
      
         
      
      
      
        
        
      
   

File: Supplier.java

import java.util.ArrayList;
import java.util.List;
public class Supplier
{
    private int id;
    private String name;
    private List products = new ArrayList();
    
    public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id = id;
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public List getProducts()
    {
        return products;
    }
    public void setProducts(List products)
    {
        this.products = products;
    }
}
File: hibernate.cfg.xml


    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

    
        
        org.hsqldb.jdbcDriver
        jdbc:hsqldb:data/tutorial
        sa
        
        
        1
        
        org.hibernate.dialect.HSQLDialect
        
        org.hibernate.context.ManagedSessionContext
        false
        false
        
        
        org.hibernate.cache.NoCacheProvider        
        
        true