Browsed by
分类:Spring

4.6. Spring Data库的自定义实现

4.6. Spring Data库的自定义实现

4.6. Custom Implementations for Spring Data Repositories 此章节覆盖了库的自定义与如何组合一个复合库。 This section covers repository customization and how fragments form a composite repository. 当一个查询方法需要不同的行为,或者无法由查询导出实现,就有必要提供一个自定义的实现。Spring Data库允许你提供一个自定义库并于泛型CRUD抽象和查询方法功能集成。 When a query method requires a different behavior or cannot be implemented by query derivation, then it is necessary to provide a custom implementation. Spring Data repositories let you provide custom repository code and integrate it with generic CRUD abstraction and query method functionality. 4.6.1. 自定义个体库 4.6.1. Customizing Individual Repositories 为了让自定义功能来丰富一个库,你必须先定义一个片段接口和与一个自定义功能的实现,如下例所示: To…

阅读全文 Read More

5.3.9. 应用查询提示

5.3.9. 应用查询提示

5.3.9. Applying Query Hints 为了在库接口声明的查询上应用JPA查询提示,可以使用@QueryHints注解。接收参数为JPA @QueryHint注解数组,和一个为了在应用分页时,用于失效提示应用被计数查询触发的布尔值,如下例所示: To apply JPA query hints to the queries declared in your repository interface, you can use the @QueryHints annotation. It takes an array of JPA @QueryHint annotations plus a boolean flag to potentially disable the hints applied to the additional count query triggered when applying pagination, as shown in the following example: 例66. 在库方法上使用QueryHints Example 66. Using QueryHints with a repository method public interface UserRepository extends Repository<User, Long> { @QueryHints(value = { @QueryHint(name = “name”, valu…

阅读全文 Read More

5.3.8. 修改查询

5.3.8. 修改查询

5.3.8. Modifying Queries 前面的章节都描述了如何声明查询,来得到一个指定的实体或者实体集合。你可以使用“Spring Data库的自定义实现”中描述的工具,来添加一个自定义的修改行为。此方法对于大多数自定义功能都可用,你只需要通过@Modifying注解查询方法并绑定参数,就可以修改查询,如下例所示: All the previous sections describe how to declare queries to access a given entity or collection of entities. You can add custom modifying behavior by using the facilities described in “Custom Implementations for Spring Data Repositories”. As this approach is feasible for comprehensive custom functionality, you can modify queries that only need parameter binding by annotating the query method with @Modifying, as shown in the following example: 例64. 声明操作查询 Example 64. Declaring manipulating q…

阅读全文 Read More

5.3.7. 使用SpEL表达式

5.3.7. 使用SpEL表达式

5.3.7. Using SpEL Expressions 从Spring Data JPA发布1.4版本时,支持使用@Query手动定义查询来支持严格的SpEL模板表达式。在查询执行时,这些表达式通过一系列预设的变量进行评估。Spring Data JPA支持entityName变量。用法为select x from #{#entityName} x。会把entityName插入关联指定库的领域类型。entityName会按如下解析:如果领域类型有@Entity注解的名称属性,则会使用此名称。否则将使用领域类型的简单类名。 As of Spring Data JPA release 1.4, we support the usage of restricted SpEL template expressions in manually defined queries that are defined with @Query. Upon query execution, these expressions are evaluated against a predefined set of variables. Spring Data JPA supports a variable called entityName. Its usage is select x from #{#entityName} x. It inserts the entityName of the domain type as…

阅读全文 Read More

5.3.6. 使用命名参数

5.3.6. 使用命名参数

5.3.6. Using Named Parameters 默认Spring Data JPA使用基于位置的参数绑定,前面的例子都是如此。当有关于参数位置的重构时,会使查询方法容易出现错误。为了解决这个问题,可以在一个方法参数上使用@Param注解,给定一个名称并在查询中绑定这个名称,如下例: By default, Spring Data JPA uses position-based parameter binding, as described in all the preceding examples. This makes query methods a little error-prone when refactoring regarding the parameter position. To solve this issue, you can use @Param annotation to give a method parameter a concrete name and bind the name in the query, as shown in the following example: 例61. 使用命名参数 Example 61. Using named parameters public interface UserRepository extends JpaRepository<User, Long> { @Query(“select u from …

阅读全文 Read More

5.3.5. 使用Sort

5.3.5. 使用Sort

5.3.5. Using Sort 提供PageRequest或者直接使用Sort可以启用排序。在Sort的实例Order中使用的属性需要匹配你的域模型,意味着需要匹配一个属性或者在查询中使用别名。JPQL将此定义为一个状态字段路径表达式。 Sorting can be done be either providing a PageRequest or by using Sort directly. The properties actually used within the Order instances of Sort need to match your domain model, which means they need to resolve to either a property or an alias used within the query. The JPQL defines this as a state field path expression. 但同时使用@Query和Sort会使Order实例的ORDER BY语句中包含函数。原因是Order添加了指定的查询字符串。默认Spring Data JPA拒绝所有包含函数调用的Order实例,但你可以使用JpaSort.unsafe来添加潜在的非安全排序。 However, using Sort together with @Query lets you sneak in non-path-checked Order ins…

阅读全文 Read More

5.3.4. 使用@Query

5.3.4. 使用@Query

5.3.4. Using @Query 使用命名查询来为实体声明查询,对于小数据量的查询来说个不错的选择。查询本身会绑定被执行的Java方法,你可以使用Spring Data JPA @Query注解,而不是注解在域类上,来绑定对应方法。这种做法将域类从特定持久化信息中释放出来,并协同定位查询所属的库接口。 Using named queries to declare queries for entities is a valid approach and works fine for a small number of queries. As the queries themselves are tied to the Java method that executes them, you can actually bind them directly by using the Spring Data JPA @Query annotation rather than annotating them to the domain class. This frees the domain class from persistence specific information and co-locates the query to the repository interface. 注解在方法上的查询优先级高于使用@NamedQuery定义的查询和在orm.xml声明的命名查询。 Queries ann…

阅读全文 Read More

5.3.3. 使用JPA命名查询

5.3.3. 使用JPA命名查询

5.3.3. Using JPA Named Queries XML命名查询定义 XML Named Query Definition 可以在classpath下的META-INF文件夹中的orm.xmlJPA配置文件中,添加<named-query />元素来使用XML配置。通过使用名义的命名转换,来启用命名查询的自动调用。下面是详细例子。 To use XML configuration, add the necessary <named-query /> element to the orm.xml JPA configuration file located in the META-INF folder of your classpath. Automatic invocation of named queries is enabled by using some defined naming convention. For more details, see below. 例53. XML命名查询配置 Example 53. XML named query configuration <named-query name=”User.findByLastname”> <query>select u from User u where u.lastname = ?1</query> </named-query> 此查询有一…

阅读全文 Read More

5.3.2. 查询创建

5.3.2. 查询创建

5.3.2. Query Creation JPA的查询创建的机制通常来说都像“查询方法”中描述的一样。下面的例子展示了一个JPA查询方法的转换: Generally, the query creation mechanism for JPA works as described in “Query methods”. The following example shows what a JPA query method translates into: 例52. 从方法名创建查询 Example 52. Query creation from method names public interface UserRepository extends Repository<User, Long> { List<User> findByEmailAddressAndLastname(String emailAddress, String lastname); } 我们使用JPA标准API来创建一个查询,但本质上是被翻译为了:select u from User u where u.emailAddress = ?1 and u.lastname = ?2。Spring Data JPA做了属性检查与属性转换,正如“属性表达式”中描述的一样。 We create a query using the JPA criteria API from this, but, essential…

阅读全文 Read More

5.3.1. 查询策略

5.3.1. 查询策略

5.3.1. Query Lookup Strategies JPA模块支持手动定义一个查询为字符串,或者通过方法命名推导。 The JPA module supports defining a query manually as a String or having it being derived from the method name. 声明查询 Declared Queries 虽然从方法名中推导得到查询的方式比较便捷,但也会面临一个问题,方法名解析器不支持要用到的关键词,或者通过方法名得到的不是预期的结果。所以你可以通过命名规范(请查阅使用JPA命名查询来获取更多信息)来使用JPA命名查询,或者使用@Query(请查询使用@Query获取更新信息)来注解你的查询方法。 Although getting a query derived from the method name is quite convenient, one might face the situation in which either the method name parser does not support the keyword one wants to use or the method name would get unnecessarily ugly. So you can either use JPA named queries through a naming convention (see Usin…

阅读全文 Read More