我正在努力实现我想实施的验证。我想验证以update开头的服务中的每个方法都必须具有@Transactional注释。到目前为止,我已经创建了一个概念,它提供了我的服务类中以update(例如updateInvoice)开头的方法。但我不知道如何构建一个约束来选择没有@Transaction注释的方法。
答案 0 :(得分:3)
我建议定义一些代表你的关键元素的概念来定义它们的约束,即
您的服务:
<concept id="service:ServiceClass">
<description>Adds a label "Service" to every class annotated by "@com.mycompany.services.Service"</description>
<cypher><![CDATA[
MATCH
(service:Type:Class)-[:ANNOTATED_BY]->()-[:OF_TYPE]->(serviceAnnotationType)
SET
service:Service
WHERE
serviceAnnotationType.fqn = "com.mycompany.services.Service"
RETURN
service
]]>
</cypher>
</concept>
您的交易方法:
<concept id="service:TransactMethod">
<description>Adds a label "Transact" to every method annotated by "@com.mycompany.services.Transact"</description>
<cypher><![CDATA[
MATCH
(method:Method)-[:ANNOTATED_BY]->()-[:OF_TYPE]->(transactAnnotationType)
SET
method:Transact
WHERE
transactAnnotationType.fqn = "com.mycompany.services.Transact"
RETURN
method
]]>
</cypher>
</concept>
你的约束:
<constraint id="service:AllUpdateMethodsMustBeTransacted">
<requiresConcept refId="service:ServiceClass" />
<requiresConcept refId="service:TransactMethod" />
<description>All update methods must be transacted</description>
<cypher><![CDATA[
MATCH
(service:Service:Class)-[:DECLARES]->(updateMethod:Method)
WHERE
updateMethod.name =~ "update.*" // even this could be extracted to a concept
and not updateMethod:Transact
RETURN
updateMethod
]]>
</cypher>
</constraint >
这种方法有几个优点:
答案 1 :(得分:1)
以下似乎有效:
match
(aType:Type:Class)-[:ANNOTATED_BY]->()-[:OF_TYPE]->(anAnnotationType:Type),
(aType:Type)-[:DECLARES]->(aMethod:Method)
optional match
(aMethod)-[:ANNOTATED_BY]->()-[:OF_TYPE]->(tType:Type)
with anAnnotationType, aMethod, tType
where
anAnnotationType.fqn = "com.mycompany.services.Service"
and aMethod.name =~ "update.*"
and ((tType is null) or not (tType.fqn = "com.mycompany.services.Transact"))
return
aMethod.name, tType