gamelan
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
New
 
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See The Winners!




Developer Jobs

Be a Commerce Partner














 


Developer News -
Why Firefox Doesn't Take Google Chrome Features    June 26, 2009
First Major PHP Update in Years Coming Soon    June 25, 2009
Red Hat CEO Calls on Oracle to Keep Java Open    June 25, 2009
Google Widens AdSense for iPhone, Android Apps    June 24, 2009
Free Tech Newsletter -

An Introduction to Object-Relational Mapping with Hibernate
By Olexiy & Alexander Prokhorenko

Go to page: Prev  1  2  3  

Finding and Loading Product Objects

Searching for and loading existing objects is a very simple task for Hibernate. With the use of its query language, we very easily can take an object (or set of objects) by its ID, a name, or other properties. We also can take either the whole object or its certain properties separately. Let's consider the FindProductByName class:

import java.util.List;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import Product;

// Usage:
//  java FindProductByName <title>
public class FindProductByName {
   public static void main(String[] args) throws Exception {
      String query = "SELECT product " +
                     "FROM product IN CLASS test.hibernate.Product " +
                     "WHERE product.name=:name";

      String name = args[0];

      Configuration cfg = new Configuration().addClass(Product.class);
      SessionFactory sf = cfg.buildSessionFactory();
      Session sess = sf.openSession();

      List list = sess.find(query, name, Hibernate.STRING);

      if (list.size() == 0) {
         System.out.println("Product [" + name + "] not found!");
      System.exit(0);
      }

      Product p = (Product) list.get(0);
      sess.close();
      System.out.println("Found product: " + p);
   }
}

Let's consider the most interesting places of the above-mentioned code:

In the code, there is a query with a WHERE expression. All is very similar to the usual SQL format.

Initialize Hibernate as we did in the first example. This time, we already have created files of a configuration and mapping (XML file). The sess.find() method carries out the query and establishes the name of a product given to it, as an argument for the search with the Hibernate.STRING type.

As a result, we will get an object of the java.util.List class, filled with Product objects from a database that satisfies the search conditions.

With the Product p = (Product) list.get (0); expression, we take the first found object.

Updating and Removing Products

By now, you already should have a decent understanding of how Hibernate works. Therefore, now we will make our examples hardly less in size, having cleaned out all repeating and unimportant items.

Following is the example of how to increase all Products' prices by 10% in one transaction. We should write the following:

...
double percentage = Double.parseDouble(args[0])/100;
sess = sf.openSession();
Transaction t = sess.beginTransaction();
Iterator iter = list.iterator();
while (iter.hasNext()) {
       Product p = (Product) iter.next();
       p.setPrice(p.getPrice() * (1 + percentage));
       sess.saveOrUpdate(p);
}
t.commit();
sess.close();
...

And, at last, for removal of a Product, we should cause a method sess.delete(product). Please, do not overlook also carrying out commit() at the end of transaction to confirm changes if only certainly the option autocommit in your database is turned on.

Afterword

This article shows what kind of tool is Hibernate, and also shows a few advantages. From it you have learned how simple is to keep any kind of Java objects, and then to manipulate them. Opportunities for Hibernate are incomparably wider. I also just need to mention that Hibernate arms the developer with very a powerful object-oriented query language, HQL, which has a set of various useful features.

Go to page: Prev  1  2  3  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Data & Java Archives






internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs