`

Criteria Condition: Match Mode End

 
阅读更多

/////////////////////////////////////////////////////////////////////////
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.ilike("name","1", MatchMode.END));
        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();
  }
}


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

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:data/tutorial</property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Mapping files -->
        <mapping resource="Product.hbm.xml"/>
        <mapping resource="Supplier.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


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

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



           
       
 
分享到:
评论

相关推荐

    excel Criteria1实例.docx

    在Excel中,`Criteria1`是一个非常重要的参数,特别是在与VBA(Visual Basic for Applications)结合使用时,它用于定义筛选或过滤数据的条件。在上述的VBA代码实例中,`Criteria1`被用于自动筛选功能,允许用户根据...

    Hibernate中Criteria的完整用法

    Hibernate中的Criteria API是一种用于执行动态查询的机制,它允许开发者在运行时构建SQL查询,而无需直接编写SQL语句。Criteria API提供了更加面向对象的方式来处理数据库查询,这使得代码更易于理解和维护,尤其是...

    hibernateCriteria查询

    criteria.add(Restrictions.like("title", "%C#", MatchMode.ANYWHERE)); List&lt;Books&gt; list = criteria.list(); ``` 这里使用`Restrictions.like("title", "%C%", MatchMode.ANYWHERE)` 来进行模糊查询,查找所有...

    Criteria用法

    - 使用`like()`方法进行模糊匹配,`MatchMode.ANYWHERE`表示在任何位置匹配,例如:`crt.add(Restrictions.like("allFather", SystemForUserContext.getOrganizationRootNodeId(), MatchMode.ANYWHERE));...

    HQL与Criteria的对照表.pdf

    ### HQL与Criteria API在Hibernate框架中的应用及对比 #### 概述 在Java开发领域,Hibernate是一个广泛使用的对象关系映射(ORM)框架,它提供了多种查询方式,包括HQL(Hibernate Query Language)和Criteria API...

    高级筛选教程(全VBA代码实现)

    Criteria1:="条件1", Operator:=xlAnd, Criteria2:="条件2" ``` 高级筛选则允许设置更复杂的条件,包括使用“与”(AND)、“或”(OR)逻辑,以及使用比较运算符。例如,你可以创建一个数组条件,如下所示: ```...

    Hibernate的Criteria用法总结

    Example exampleUser = Example.create(u).ignoreCase().enableLike(MatchMode.ANYWHERE); ``` Projection接口用于处理查询结果,比如计算平均值、计数、最大值、最小值和求和。SimpleProjection和ProjectionList...

    UN - Manual of Tests and Criteria:2019(Rev7) - 最新英文电子版(536页).pdf

    UN - Manual of Tests and Criteria:2019(Rev7) - 最新英文电子版(536页).pdf

    CRITERIA1D:CRITERIA 1D是一维农业水文模型。它包括土壤水通量,作物生长和作物需水,用于管理地理模拟的GIS界面以及用于管理作物和土壤参数的工具。该软件是多平台的(Windows,Linux,Mac OS)

    CRITERIA-1D / GEO CRITERIA-1D是一种农业水文学模型,用于模拟一维水通量,作物生长和作物需水量。可以在不同的详细级别定义土壤和作物参数。它需要每日农业气象数据作为输入:最低和最高气温,总降水量以及皮下...

    Hibernate criteria基本操作

    Hibernate Criteria 是一种在Java应用程序中使用Hibernate ORM框架执行SQL查询的方式。它提供了一种面向对象的方式来构建查询,使得代码更加可读和易于维护。Criteria API 提供了多种方法来构建复杂的查询,包括添加...

    Hibernate中Criteria的完整用法.docx

    然后可以设置一些过滤条件:Example exampleUser = Example.create(u) .ignoreCase() // 忽略大小写 .enableLike(MatchMode.ANYWHERE); // 对 String 类型的属性,无论在那里值在那里都匹配。相当于 %value% ...

    Criteria标准化对象查询小例子

    在Java世界中,Criteria API是JPA(Java Persistence API)的一部分,用于构建动态、类型安全的查询。这个“Criteria标准化对象查询小例子”提供了一个实际应用Criteria API的示例,帮助开发者理解如何利用它来执行...

    Hibernate中Criteria的使用

    然后可以设置一些过滤条件,例如 Example exampleUser = Example.create(u).ignoreCase().enableLike(MatchMode.ANYWHERE); // 忽略大小写, 对 String 类型的属性,无论在那里值在那里都匹配。 Projection 主要有 ...

    criteria介绍与使用

    - 可以通过`Criteria`对象的`setLockMode(String propertyName, LockMode lockMode)`方法来控制实体的锁定方式。 总之,`Criteria`API为Java开发者提供了强大而灵活的数据查询能力,使得在不编写SQL的情况下也能...

    Hibernate Criteria 排序的問題

    ### Hibernate Criteria 排序问题详解 #### 一、前言 在使用Hibernate进行数据库操作时,经常需要对查询结果进行排序处理。对于简单的查询场景,使用HQL(Hibernate Query Language)即可轻松实现排序功能;但对于...

    Criteria使用方法

    ### Criteria使用方法详解 #### 一、概述 在软件开发过程中,尤其是涉及到数据库操作的应用程序中,查询语言的灵活性和效率对于项目的成功至关重要。Hibernate作为Java领域中最流行的ORM(Object Relational ...

    Criteria的完整用法.pdf

    ### Criteria的完整用法详解 #### 一、概述 在基于Spring和Hibernate的项目开发中,Criteria API提供了灵活且强大的查询功能,使开发者能够轻松构建复杂的查询条件,从而提高数据检索的效率与精确度。本文将深入...

    Python库 | criteria-etl-1.0a3.tar.gz

    《Python库criteria-etl-1.0a3:数据提取、转换与加载的高效工具》 在信息技术领域,数据处理是至关重要的环节,而Python作为一门强大的编程语言,提供了丰富的库来支持数据处理任务,其中之一便是`criteria-etl`。...

Global site tag (gtag.js) - Google Analytics