hibernate查询语句怎么写( 三 )


) 其用法和SQL基本相同 select distinct employee.name from Employee as employee select count(distinct employee.name),count(employee) from Employee as employee 6 。polymorphism (暂时不知道如何解释?) from com.test.Animal as animal 不光得到所有Animal得实例,而且可以得到所有Animal的子类(如果我们定义了一个子类Cat) 一个比较极端的例子 from java.lang.Object as o 可以得到所有持久类的实例 7 。
【hibernate查询语句怎么写】where语句 定义查询语句的条件,举几个例子吧: from Employee as employee where employee.Name='Jplateau' from Employee as employee where employee.Name like 'J%' from Employee as employee where employee.Name like '%u' 在where语句中“=”不光可以比较对象的属性,也可以比较对象,如: select animal from com.test.Animal as animal where animal.name=dog 8 。表达式 在SQL语句中大部分的表达式在HQL中都可以使用: mathematical operators +, -, *, / binary comparison operators =, >=, <=, <>, !=, like logical operations and, or, not string concatenation || SQL scalar functions like upper() and lower() Parentheses ( ) indicate grouping in, between, is null JDBC IN parameters ? named parameters :name, :start_date, :x1 (这种应该是另一种"?"的变通解决方法) SQL literals 'foo', 69, '1970-01-01 10:00:01.0' Java public static final constants eg.Color.TABBY 其他不必解释了,在这里我只想对查询中的参数问题说明一下: 大家知道在SQL中进行传递参数进行查询的时候,我们通常用PreparedStatement,在语句中写一大堆的“?”, 在hql中也可以用这种方法,如: List mates = sess.find( "select employee.name from Employee as employee " + "where employee.Name=? ", name, Hibernate.STRING ); (说明:上面利用Session里的find方法,在hibernate的api Session中重载了很多find方法,它可以满 。