技术开发 频道

Enterprise JavaBeans查询语言(二)

【IT168 技术文档】

Select查询
 
这一部分的查询是select方法所使用的。与finder方法不同,一个select方法可以返回持续化字段或其它entity bean。
 
示例 10
 
SELECT DISTINCT t.league
FROM Player p, IN (p.teams) AS t
WHERE p = ?1
获得的数据:指定队员所属的联盟。
Select方法:ejbSelectLeagues(LocalPlayer player)
 
说明:这个查询的返回类型是LeagueEJB entity bean的抽象模式类型。这个抽象模式类型映射到LocalLeagueHome接口。因为表达式t.league不是一个独立的标识变量,OBJECT关键字被省略了。
 
示例 11
 
SELECT DISTINCT t.league.sport
FROM Player p, IN (p.teams) AS t
WHERE p = ?1
获得的数据:指定队员所参与的运动。
Select方法: ejbSelectSports(LocalPlayer player)
 
说明:这个查询返回了一个名为sport的String named sport,这是LeagueEJB entity bean的一个持续化字段。
 
完整的语法
 
  在这一部分我们讨论Enterprise JavaBean规范中定义的EJB QL语法。下面的素材中的大多数直接取自Enterprise JavaBean规范或是对规范的解释。
 
EJB QL的BNF语法
 
这里是EJB QL的全部BNF语法解释:
 
EJB QL ::= select_clause from_clause [where_clause]
from_clause ::= FROM identification_variable_declaration
[, identification_variable_declaration]*
identification_variable_declaration ::=
collection_member_declaration |
range_variable_declaration
collection_member_declaration ::=
IN (collection_valued_path_expression) [AS] identifier
range_variable_declaration ::=
abstract_schema_name [AS] identifier
single_valued_path_expression ::=
{single_valued_navigation |
identification_variable}.cmp_field |
single_valued_navigation
single_valued_navigation ::=
identification_variable.[single_valued_cmr_field.]*
single_valued_cmr_field
collection_valued_path_expression ::=
identification_variable.[single_valued_cmr_field.]*
collection_valued_cmr_field
select_clause ::= SELECT [DISTINCT]
{single_valued_path_expression |
OBJECT(identification_variable)}
where_clause ::= WHERE conditional_expression
conditional_expression ::= conditional_term |
conditional_expression OR conditional_term
conditional_term ::= conditional_factor |
conditional_term AND conditional_factor
conditional_factor ::= [ NOT ] conditional_test
conditional_test :: = conditional_primary
conditional_primary ::=
simple_cond_expression | (conditional_expression)
simple_cond_expression ::=
comparison_expression |
between_expression |
like_expression |
in_expression |
null_comparison_expression |
empty_collection_comparison_expression |
collection_member_expression
between_expression ::=
arithmetic_expression [NOT] BETWEEN
arithmetic_expression AND arithmetic_expression
in_expression ::=
single_valued_path_expression
[NOT] IN (string_literal [, string_literal]* )
like_expression ::=
single_valued_path_expression
[NOT] LIKE pattern_value [ESCAPE escape-character]
null_comparison_expression ::=
single_valued_path_expression IS [NOT] NULL empty_collection_comparison_expression ::=
collection_valued_path_expression IS [NOT] EMPTY
collection_member_expression ::=
{single_valued_navigation | identification_variable |
input_parameter}
[NOT] MEMBER [OF] collection_valued_path_expression
comparison_expression ::=
string_value { =|〈〉} string_expression |
boolean_value { =|〈〉} boolean_expression} |
datetime_value { = | 〈〉 | 〉 | 〈 } datetime_expression |
entity_bean_value { = | 〈〉 } entity_bean_expression |
arithmetic_value comparison_operator
single_value_designator
arithmetic_value ::= single_valued_path_expression |
functions_returning_numerics
single_value_designator ::= scalar_expression
comparison_operator ::=
= | 〉 | 〉= | 〈 | 〈= | 〈〉
scalar_expression ::= arithmetic_expression
arithmetic_expression ::= arithmetic_term |
arithmetic_expression { + | - } arithmetic_term
arithmetic_term ::= arithmetic_factor |
arithmetic_term { * | / } arithmetic_factor
arithmetic_factor ::= { + |- } arithmetic_primary
arithmetic_primary ::= single_valued_path_expression |
literal | (arithmetic_expression) |
input_parameter | functions_returning_numerics
string_value ::= single_valued_path_expression |
functions_returning_strings
string_expression ::= string_primary | input_expression
string_primary ::= single_valued_path_expression | literal |
(string_expression) | functions_returning_strings
datetime_value ::= single_valued_path_expression
datetime_expression ::= datetime_value | input_parameter
boolean_value ::= single_valued_path_expression
boolean_expression ::= single_valued_path_expression |
literal | input_parameter
entity_bean_value ::=
single_valued_navigation | identification_variable
entity_bean_expression ::= entity_bean_value | input_parameter
functions_returning_strings ::=
CONCAT(string_expression, string_expression) |
SUBSTRING(string_expression, arithmetic_expression,
arithmetic_expression)
functions_returning_numerics::=
LENGTH(string_expression) |
LOCATE(string_expression,
string_expression[, arithmetic_expression]) |
ABS(arithmetic_expression) |
SQRT(arithmetic_expression)
 
BNF符号
 
符号:描述
::=:该符号左边的元素被该符号右边的结构所定义
*:该符号前面的结构可以重复零次或多次
{...}:在这个花括号中的结构一起组合成一组
[...]:方括号中的结构是可选的
|:一个具有排它性的OR
黑体字:一个关键字(尽管在这个BNF定义中使用了大写,但是事实上关键字并不是大小写敏感的)
空白区域:一个空白区域可以是一个空格、横表符或是换页符
 
FROM子句
 
FROM子句通过声明标识变量定义了查询的范围。这里是FROM子句的语法:
from_clause ::= FROM identification_variable_declaration [,
identification_variable_declaration]* identification_variable_declaration ::=
collection_member_declaration | range_variable_declaration
collection_member_declaration ::= IN (collection_valued_path_expression) [AS]
identifier range_variable_declaration ::= abstract_schema_name [AS] identifier
 
标识符
 
  一个标识符是由一个或多个字符组成的序列。在Java编程语言(以下简称"Java")中的标识符的首字符必须是一个有效的首字符(字母、$和_)。在一外Java标识符中的每一个子序列字符都必须是一个有效的非首字符(字母、数字、$和_)。(详细情况请参阅Character类的isJavaIdentifierStart方法和isJavaIdentifierPart方法的J2SE API文档。在EJB QL中问号(?)是一个保留字符,它不可以在一个标识符中使用。与一个Java变量不同,一个EJB QL标识符是对大小写不敏感的。
 
一个标识符不能与EJB QL关键字发生冲突:
 
AND
AS
BETWEEN
DISTINCT
EMPTY
FALSE
FROM
IN
IS
LIKE
MEMBER
NOT
NULL
OBJECT
OF
OR
SELECT
TRUE
UNKNOWN
WHERE
 
  EJB QL的关键字同时也是SQL的保留字。在将来,EJB QL关键字将扩充到包含其它SQL保留字。Enterprise JavaBeans规范推荐你不要在EJB QL标识符中使用其它SQL保留字。
0
相关文章