`

Criteria: Order

 
阅读更多

/////////////////////////////////////////////////////////////////////////

import java.util.*;

import java.sql.*;
import org.hibernate.*;
import org.hibernate.criterion.*;

public class Main {
  
  public static void main(String[] args) {
    HibernateUtil.setup("create table Supplier ( id int, name VARCHAR);");
    HibernateUtil.setup("create table Product ( id int, name VARCHAR, description VARCHAR, price double,supplierId int);");
    
    prepareData();
    Session session = HibernateUtil.currentSession();
    
        Criteria crit = session.createCriteria(Product.class);
        crit.add(Restrictions.gt("price",new Double(1.0)));
        crit.addOrder(Order.desc("price"));
        List results = crit.list();
        displayProductsList(results);
    
    
        HibernateUtil.checkData("select * from Supplier");
        HibernateUtil.checkData("select * from Product");

  }
    public static void displayProductsList(List list){
        Iterator iter = list.iterator();
        if (!iter.hasNext()){
            System.out.println("No products to display.");
            return;
        }
        while (iter.hasNext()){
            Product product = (Product) iter.next();
            String msg = product.getSupplier().getName() + "\t";
            msg += product.getName() + "\t";
            msg += product.getPrice() + "\t";
            msg += product.getDescription();
            System.out.println(msg);
        }
    }

  private static void prepareData(){
        Session session = HibernateUtil.currentSession();

        Supplier supplier1 = new Supplier();
        supplier1.setName("Supplier Name 1");
        session.save(supplier1);
        
        Supplier supplier2 = new Supplier();
        supplier2.setName("Supplier Name 2");
        session.save(supplier2);        
        
        Product product1 = new Product("Product 1","Name for Product 1", 2.0);
        product1.setSupplier(supplier1);
        supplier1.getProducts().add(product1);
        session.save(product1);
        
        Product product12 = new Product("Product 2","Name for Product 2", 22.0);
        product12.setSupplier(supplier1);
        supplier1.getProducts().add(product12);        
        session.save(product12);
        
        Product product2 = new Product("Product 3", "Name for Product 3", 30.0);
        product2.setSupplier(supplier2);
        supplier2.getProducts().add(product2);
        session.save(product2);
        
        session.flush();
        HibernateUtil.closeSession();
  }
}

/////////////////////////////////////////////////////////////////////////
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;
    }
}



/////////////////////////////////////////////////////////////////////////

<?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>



/////////////////////////////////////////////////////////////////////////

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;
    }
}



/////////////////////////////////////////////////////////////////////////
 
分享到:
评论

相关推荐

    Hibernate的Criteria用法总结

    Hibernate的Criteria用法总结Hibernate的Criteria用法总结

    Hibernate教程

    24.3. Customer(客户)/Order(订单)/Product(产品) 24.4. 杂例 24.4.1. "Typed" one-to-one association 24.4.2. Composite key example 24.4.3. Content based discrimination 24.4.4. Associations on ...

    javax.persistence.jar

    javax.persistence.criteria.Order.class javax.persistence.criteria.ParameterExpression.class javax.persistence.criteria.Path.class javax.persistence.criteria.PluralJoin.class javax.persistence.criteria...

    Improved delay-dependent consensus stability criteria of high-order multi-agent systems with time delays

    Improved delay-dependent consensus stability criteria of high-order multi-agent systems with time delays

    hibernate3.6API

    org.hibernate.criterion A framework for defining restriction criteria and order criteria. org.hibernate.mapping This package defines the Hibernate configuration-time metamodel. org.hibernate....

    Hibernate_3.6.6_CHM 文档

    org.hibernate.criterion A framework for defining restriction criteria and order criteria. org.hibernate.mapping This package defines the Hibernate configuration-time metamodel. org.hibernate....

    Novel Stability Criteria for Impulsive Memristive Neural Networks with Time-Varying Delays

    In order to improve the ability of resisting disturbance for memristive neural networks (MNNs), a general impulsive controlled MNN with variable delays is constructed in this paper. Then, its ...

    PLSqlsample

    The criteria used here based on the order date and the order status. When this criteria is satisfied certain elements are removed from the collection. This results in a sparse collection which is ...

    论文研究-Coordination of Multi-agent Systems with Communication Delays.pdf

    具有通信时延的多个体系统的协调控制,刘成林,田玉平,本文考察了个体动态为二阶积分模型,具有通信时延的多个体系统的协调控制。基于频域分析方法与矩阵论,分别得到了系统渐进收敛静

    YII实现分页的方法

    $criteria-&gt;order='id DESC'; $count=User::model()-&gt;count($criteria); $pager=new CPagination($count); $pager-&gt;pageSize=10; $pager-&gt;applyLimit($criteria); $userList=User::model()-&gt;findAll

    Yii框架中 find findAll 查找出制定的字段的方法对比

    众所周知 modelName::model() -&gt; find() //找出的是一个对象 modelName::model() -&gt; findALL...$criteria-&gt;order = 'id DESC'; $users = modelName::model()-&gt;findAll( $criteria ); 后台无意中看到别人有这么写的,

    Introducing.Linux.Distros.1484213939

    Introducing Linux Distros teaches you the pros and the cons of the most frequently used distros in order to find the one that is right for you. You will explore each distro step by step, so that you ...

    The Java EE 6 Tutorial Basic Concepts 4th Edition

    The order Application 357 The roster Application 369 The address-book Application 376 Chapter 21: The Java Persistence Query Language 381 Query Language Terminology 382 Creating Queries Using ...

    jdbc基础和参考

    Criteria 1.hibernate提供的更面向对象的一种查询方式。 准备工作: 1.java中的POJO对象存在 2.数据库,表得存在 3.hibernate的配置文件(hibernate.cfg.xml)得存在 4.POJO.hbm.xml文件存在 5.hibernate的jar包以及...

    sql2005全文检索.doc

    ' ROW_NUMBER() OVER (ORDER BY RANK DESC) AS SerialNumber ,'+ ' F.[rank], '+ ' p.*' + ' FROM'+ ' FREETEXTTABLE( CapitalInfoFactTab , (ProvinceName, CityName,  CountyName, Keyword,Title ,IndustryBName ...

    Yii分页用法实例详解

    下面我总结了在Yii常用的一些yii分页方式与实例代码,这里有普通分页与ajax实现分页,希望此文章对大家... //$criteria-&gt;order = ‘news_id DESC’;  $criteria-&gt;condition = ‘user_id = 1’;    $dataProvider

    tsa时间序列分析预测.rar

    - Several criteria (AIC, BIC, FPE, MDL, SBC, CAT, PHI) for model order selection an autoregressive model are included. - Fast algorithms are used - missing values (encoded as NaN's) are considered

    Automated.Trading.with.R.Quantitative.Research.and.Platform.Development

    Automated Trading with R provides automated traders with all the tools they need to trade algorithmically with their existing brokerage, from data management, to strategy optimization, to order ...

    Recipes---Angular

    - see all recipes, filter and order them by different criteria. - search recipe by name - see recipe's detail information. - like/dislike recipes - write comments to recipe - add new recipes - update ...

    dexcoder-assistant:dexcoder 快速开发工具包

    修正Criteria方式先使用queryCount再queryList,order by丢失问题 修正RunBinder拦截器事务嵌套有错时,事务回滚出错问题 配置动态数据源请看这里: 数据水平分表请看这里: ##核心组件dexcoder-dal使用说明 dexcoder-...

Global site tag (gtag.js) - Google Analytics