开发者

How to write DDL in the criteria API?

Consider the code in How do I delete all JPA entities? The documentation here http://download.开发者_运维知识库oracle.com/docs/cd/E17410_01/javaee/6/tutorial/doc/gjitv.html describes only queries.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜