如何在标准API中编写DDL?

发布于 2024-09-11 05:53:59 字数 330 浏览 2 评论 0原文

考虑以下代码 如何删除所有 JPA 实体? 此处的文档 http://download.oracle .com/docs/cd/E17410_01/javaee/6/tutorial/doc/gjitv.html 仅描述查询。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

似梦非梦 2024-09-18 05:53:59

Criteria API 的文档仅描述查询,因为 Criteria API 不是为 DDL 操作而设计的。事实上,我什至会说整个 JPA API 并不是真正为此而设计的。

顺便说一句,另一个问题的代码不显示 DDL 操作,它显示 JPA 2.0 规范中描述的批量 DML 操作:

4.10 批量更新和删除操作

批量更新和删除操作
适用于单个实体的实体
类(及其子类,
如果有的话)。只有一个实体抽象
模式类型可以在
FROM 或 UPDATE 子句。

这些操作的语法如下
如下:

更新语句 ::=
update_clause [where_clause]
update_clause ::= UPDATE 实体名称 [[AS] 标识变量]
                     设置更新项目{,更新项目}*
update_item ::= [identification_variable。]{state_field |单值对象字段} =
                     新值
新值 ::=
       标量表达式 |
       简单实体表达式 |
       无效的

删除语句 ::= 删除子句 [where_clause] 
删除子句 ::= 从实体名称 [[AS] 标识变量] 中删除

  
  

WHERE 子句的语法为 第 4.5 节中描述。​​

删除操作仅适用于 指定类别的实体和 它的子类。它不会级联到 相关实体。

为某个指定的new_value 更新操作必须兼容 键入它所在的字段 已分配。

批量更新直接映射到 数据库更新操作,绕过 乐观锁定检查。便携的 应用程序必须手动更新 版本列的值,如果 所需的,和/或手动验证 版本列的值。

持久化上下文不是 与结果同步 批量更新或删除。

执行批量更新或删除操作时应谨慎 因为它们可能会导致 数据库之间的不一致 以及活动中的实体 持久化上下文。一般来说,散装 更新和删除操作应该 只能在事务中执行 在新的持久性上下文中或之前 获取或访问其实体 国家可能会受到这种影响 操作。

示例:

<前><代码>删除 来自客户 c WHERE c.status = '不活动' 删除 来自客户 c WHERE c.status = '不活动' 并且 c.orders 为空 更新客户c SET c.status = '优秀' 其中 c.balance < 10000

The documentation of the Criteria API describes only queries because the Criteria API is not made for DDL operations. Actually, I'd even say that the whole JPA API is not really made for that.

And by the way, the code of the other question doesn't show DDL operations, it's shows bulk DML operations which are described in the JPA 2.0 specification:

4.10 Bulk Update and Delete Operations

Bulk update and delete operations
apply to entities of a single entity
class (together with its subclasses,
if any). Only one entity abstract
schema type may be specified in the
FROM or UPDATE clause.

The syntax of these operations is as
follows:

update_statement ::=
update_clause [where_clause]
update_clause ::= UPDATE entity_name [[AS] identification_variable]
                     SET update_item {, update_item}*
update_item ::= [identification_variable.]{state_field | single_valued_object_field} =
                     new_value
new_value ::=
       scalar_expression |
       simple_entity_expression |
       NULL

delete_statement ::= delete_clause [where_clause] 
delete_clause ::= DELETE FROM entity_name [[AS] identification_variable]

The syntax of the WHERE clause is
described in Section 4.5.

A delete operation only applies to
entities of the specified class and
its subclasses. It does not cascade to
related entities.

The new_value specified for an
update operation must be compatible in
type with the field to which it is
assigned.

Bulk update maps directly to a
database update operation, bypassing
optimistic locking checks. Portable
applications must manually update the
value of the version column, if
desired, and/or manually validate the
value of the version column.

The persistence context is not
synchronized with the result of the
bulk update or delete.

Caution should be used when executing bulk update or delete operations
because they may result in
inconsistencies between the database
and the entities in the active
persistence context. In general, bulk
update and delete operations should
only be performed within a transaction
in a new persistence context or before
fetching or accessing entities whose
state might be affected by such
operations.

Examples:

DELETE
FROM Customer c
WHERE c.status = ‘inactive’

DELETE
FROM Customer c
WHERE c.status = ‘inactive’
AND c.orders IS EMPTY

UPDATE customer c
SET c.status = ‘outstanding’
WHERE c.balance < 10000
诺曦 2024-09-18 05:53:59

我认为标准 API 中没有 DDL。 JPQL 中也没有。

I think there is no DDL in criteria API. Nor in JPQL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文