Alle 1100 Artikel in
Grundlagen:

Eigenschaften

Preis

Themen

 
Zertifikat Euro-Label Geprüfter Online-Shop - Per Klick Gültigkeit überprüfen
 

Sicher einkaufen

Was passiert bei uns?

Pro Hibernate 3
 

Zum Download (ciando)

PDF-Download

Anbieter: ciando GmbH

 

Pro Hibernate 3

 
Sie sparen 6% gegenüber der Buchausgabe!
 

Buchausgabe: 38,51€
Download-Version: 36,37€

(Preis inkl. Mwst. )

Autor(en): Linwood Jeff, Minter Dave
Verlag: Apress
Version: 1. Auflage, 2005
Umfang: 263 Seiten
Format: PDF: 1,38MB
ISBN: 1590595114
Bestell-Nr.: 43020042UP
Artikeltyp: E-Book
 

Provides current, up-to-date information about Hibernate 3 that programmers can't get anywhere else. Not only is it the first book to cover Hibernate 3 and all of the new features, it also provides an introduction to the latest version of this apparent established standard for object relational persistence in the Java world.


Leseprobe:

C H A P T E R 7

Querying Objects with Criteria (P. 131)

In this chapter, we are going to discuss retrieving objects from the database using Hibernate’s criteria query API. Hibernate provides three different ways to retrieve data: the criteria query API, the Hibernate Query Language (HQL), and native SQL queries. The criteria query API provides a set of Java objects you can use to construct your queries. Hibernate provides its own object query language, HQL, which allows you to use SQL-like syntax to retrieve objects from the database. Lastly, Hibernate provides an API for using SQL directly against the database, which allows you to use database-specific SQL that Hibernate does not support.

We discuss criteria in this chapter—in Chapter 8 we will cover HQL and SQL queries. The criteria query API lets you build nested, structured query expressions in Java, providing a compile-time syntax checking that is not possible with a query language like HQL or SQL. The criteria query API also includes Query By Example (QBE) functionality for supplying example objects that contain the properties you would like to retrieve.

In Hibernate 3, the criteria query API also includes projection and aggregation methods, including count. In Hibernate 2, you had to download a patch to the criteria query API to enable row counting.

Using the Criteria Query API

The easiest way to retrieve data with Hibernate is to use the criteria query API. Instead of writing an SQL SELECT statement or an HQL statement, you can build up a query programmatically. You can also use QBE to retrieve objects that match an object that you have partially populated (we will discuss QBE in more detail further on in the chapter). The org.hibernate. Criteria interface defines the available methods for one of these criteria query objects.

The Hibernate Session interface contains several createCriteria() methods. Pass the persistent object’s class or its entity name to the createCriteria() method, and Hibernate will create a Criteria object that returns instances of the persistence object’s class when your application executes a criteria query.

The simplest example of a criteria query is one with no optional parameters or restrictions —the criteria query will simply return every object that corresponds to the class. We will illustrate this criteria with an example, but we wanted to first introduce the sample application we are using in this chapter, and the next chapter on HQL. The sample application has three classes: Supplier, Product, and Software. The Supplier class (see Listing 7-1) has a name property and a List collection of Product objects.

Listing 7-1. The Supplier Class

package com.hibernatebook.criteria,

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)

{

Empfehlen
mail facebook twitter

 

Der besondere Tipp

Blauer Elefant

Denken Sie nicht an einen blauen Elefanten!

Anhand verblüffender Experimente und einfacher Übungen lernen Sie, wie unsere Umwelt die Gedanken und die Gedanken unsere Umwelt beeinflussen.

Früher: 12,00€
bei uns nur: 4,99€