Save and update event : Session « Hibernate « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Hibernate » Session 
21. 22. 2. Save and update event

File: Main.java

import java.io.Serializable;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.event.SaveOrUpdateEvent;
import org.hibernate.event.def.DefaultSaveOrUpdateEventListener;

public class Main {
  public static void main(String[] argsthrows 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)");
    
    hibernateUtil.getConfiguration().setListener("save-update"new MySaveOrUpdateEventListener());
    
    Session session = hibernateUtil.getConfiguration().buildSessionFactory().openSession();

    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();

    session.close();
    hibernateUtil.checkData("select * from Product");
    hibernateUtil.checkData("select * from Software");
    hibernateUtil.checkData("select * from Supplier");
  }
}

class MySaveOrUpdateEventListener extends DefaultSaveOrUpdateEventListener {

  public void onSaveOrUpdate(SaveOrUpdateEvent event) {
    System.out.println(event.getEntity());
    super.onSaveOrUpdate(event);
  }
}

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: Supplier.hbm.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="Supplier" >
      <id name="id" type="int">
         <generator class="increment"/>
      </id>

      <property name="name" type="string"/>
      <bag name="products" inverse="true" cascade="all,delete-orphan">
        <key column="supplierId"/>
        <one-to-many class="Product"/>
      </bag>


   </class>
</hibernate-mapping>

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: Software.hbm.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <joined-subclass name="Software" extends="Product">
      <key column="Id"/>
      <property name="version" type="string"/>
   </joined-subclass>
</hibernate-mapping>

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: Product.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="Product">
      <id name="id" type="int">
         <generator class="increment"/>
      </id>

      <property name="name" type="string"/>
      <property name="description" type="string"/>
      <property name="price" type="double"/>
      <many-to-one name="supplier" class="Supplier" column="supplierId"/>

   </class>
   
   <query name="Product.HQLpricing">
     select product.price from Product product where product.price = 25.0
  </query>   
   <sql-query name="Product.SQLpricing">
   <return-scalar column="price" type="double"/>
     select product.price from Product as product where product.price = 25.0
  </sql-query>     
</hibernate-mapping>
  Download:  Hibernatesave-updateEvent.zip( 4,866 k)
21. 22. Session
21. 22. 1. Listen to Session Event
21. 22. 2. Save and update event
21. 22. 3. Let Hibernate to decide whether to save or update
21. 22. 4. Create Query from Session
21. 22. 5. Create Criteria from Session
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.