/** * Interface to allow execution of {@link Specification}s based on the JPA criteria API. * */ publicinterfaceJpaSpecificationExecutor<T> {
/** * Returns a single entity matching the given {@link Specification} or {@link Optional#empty()} if none found. * * @param spec can be {@literal null}. * @return never {@literal null}. * @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one entity found. */ Optional<T> findOne(@Nullable Specification<T> spec);
/** * Returns all entities matching the given {@link Specification}. * * @param spec can be {@literal null}. * @return never {@literal null}. */ List<T> findAll(@Nullable Specification<T> spec);
/** * Returns a {@link Page} of entities matching the given {@link Specification}. * * @param spec can be {@literal null}. * @param pageable must not be {@literal null}. * @return never {@literal null}. */ Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable);
/** * Returns all entities matching the given {@link Specification} and {@link Sort}. * * @param spec can be {@literal null}. * @param sort must not be {@literal null}. * @return never {@literal null}. */ List<T> findAll(@Nullable Specification<T> spec, Sort sort);
/** * Returns the number of instances that the given {@link Specification} will return. * * @param spec the {@link Specification} to count instances for. Can be {@literal null}. * @return the number of instances. */ longcount(@Nullable Specification<T> spec); }
/** * Returns all entities sorted by the given options. * * @param sort * @return all entities sorted by the given options */ Iterable<T> findAll(Sort sort);
/** * Returns a {@link Page} of entities meeting the paging restriction provided in the {@code Pageable} object. * * @param pageable * @return a page of entities */ Page<T> findAll(Pageable pageable); }
默认情况下,Spring Data JPA提供的CRUD方法都添加了事务,这里的事务使用的是Spring的事务管理机制。对于读操作来说,事务的readOnly属性是设置的true(默认值是false),而其他操作都是设置的一个空的@Transactional注解,所以使用的都是Spring事务的默认配置。
/** * Alias for {@link #transactionManager}. * @see #transactionManager */ @AliasFor("transactionManager") String value()default"";
/** * A <em>qualifier</em> value for the specified transaction. * <p>May be used to determine the target transaction manager, matching the * qualifier value (or the bean name) of a specific * {@link org.springframework.transaction.TransactionManager TransactionManager} * bean definition. * @since 4.2 * @see #value * @see org.springframework.transaction.PlatformTransactionManager * @see org.springframework.transaction.ReactiveTransactionManager */ @AliasFor("value") String transactionManager()default"";
/** * Defines zero (0) or more transaction labels. * <p>Labels may be used to describe a transaction, and they can be evaluated * by individual transaction managers. Labels may serve a solely descriptive * purpose or map to pre-defined transaction manager-specific options. * <p>See the documentation of the actual transaction manager implementation * for details on how it evaluates transaction labels. * @since 5.3 * @see org.springframework.transaction.interceptor.DefaultTransactionAttribute#getLabels() */ String[] label() default {};
/** * The transaction propagation type. * <p>Defaults to {@link Propagation#REQUIRED}. * @see org.springframework.transaction.interceptor.TransactionAttribute#getPropagationBehavior() */ Propagation propagation()default Propagation.REQUIRED;
/** * The transaction isolation level. * <p>Defaults to {@link Isolation#DEFAULT}. * <p>Exclusively designed for use with {@link Propagation#REQUIRED} or * {@link Propagation#REQUIRES_NEW} since it only applies to newly started * transactions. Consider switching the "validateExistingTransactions" flag to * "true" on your transaction manager if you'd like isolation level declarations * to get rejected when participating in an existing transaction with a different * isolation level. * @see org.springframework.transaction.interceptor.TransactionAttribute#getIsolationLevel() * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#setValidateExistingTransaction */ Isolation isolation()default Isolation.DEFAULT;
/** * The timeout for this transaction (in seconds). * <p>Defaults to the default timeout of the underlying transaction system. * <p>Exclusively designed for use with {@link Propagation#REQUIRED} or * {@link Propagation#REQUIRES_NEW} since it only applies to newly started * transactions. * @return the timeout in seconds * @see org.springframework.transaction.interceptor.TransactionAttribute#getTimeout() */ inttimeout()default TransactionDefinition.TIMEOUT_DEFAULT;
/** * The timeout for this transaction (in seconds). * <p>Defaults to the default timeout of the underlying transaction system. * <p>Exclusively designed for use with {@link Propagation#REQUIRED} or * {@link Propagation#REQUIRES_NEW} since it only applies to newly started * transactions. * @return the timeout in seconds as a String value, e.g. a placeholder * @since 5.3 * @see org.springframework.transaction.interceptor.TransactionAttribute#getTimeout() */ String timeoutString()default"";
/** * A boolean flag that can be set to {@code true} if the transaction is * effectively read-only, allowing for corresponding optimizations at runtime. * <p>Defaults to {@code false}. * <p>This just serves as a hint for the actual transaction subsystem; * it will <i>not necessarily</i> cause failure of write access attempts. * A transaction manager which cannot interpret the read-only hint will * <i>not</i> throw an exception when asked for a read-only transaction * but rather silently ignore the hint. * @see org.springframework.transaction.interceptor.TransactionAttribute#isReadOnly() * @see org.springframework.transaction.support.TransactionSynchronizationManager#isCurrentTransactionReadOnly() */ booleanreadOnly()defaultfalse;
/** * Defines zero (0) or more exception {@linkplain Class classes}, which must be * subclasses of {@link Throwable}, indicating which exception types must cause * a transaction rollback. * <p>By default, a transaction will be rolled back on {@link RuntimeException} * and {@link Error} but not on checked exceptions (business exceptions). See * {@link org.springframework.transaction.interceptor.DefaultTransactionAttribute#rollbackOn(Throwable)} * for a detailed explanation. * <p>This is the preferred way to construct a rollback rule (in contrast to * {@link #rollbackForClassName}), matching the exception type, its subclasses, * and its nested classes. See the {@linkplain Transactional class-level javadocs} * for further details on rollback rule semantics and warnings regarding possible * unintentional matches. * @see #rollbackForClassName * @see org.springframework.transaction.interceptor.RollbackRuleAttribute#RollbackRuleAttribute(Class) * @see org.springframework.transaction.interceptor.DefaultTransactionAttribute#rollbackOn(Throwable) */ Class<? extendsThrowable>[] rollbackFor() default {};
/** * Defines zero (0) or more exception name patterns (for exceptions which must be a * subclass of {@link Throwable}), indicating which exception types must cause * a transaction rollback. * <p>See the {@linkplain Transactional class-level javadocs} for further details * on rollback rule semantics, patterns, and warnings regarding possible * unintentional matches. * @see #rollbackFor * @see org.springframework.transaction.interceptor.RollbackRuleAttribute#RollbackRuleAttribute(String) * @see org.springframework.transaction.interceptor.DefaultTransactionAttribute#rollbackOn(Throwable) */ String[] rollbackForClassName() default {};
/** * Defines zero (0) or more exception {@link Class Classes}, which must be * subclasses of {@link Throwable}, indicating which exception types must * <b>not</b> cause a transaction rollback. * <p>This is the preferred way to construct a rollback rule (in contrast to * {@link #noRollbackForClassName}), matching the exception type, its subclasses, * and its nested classes. See the {@linkplain Transactional class-level javadocs} * for further details on rollback rule semantics and warnings regarding possible * unintentional matches. * @see #noRollbackForClassName * @see org.springframework.transaction.interceptor.NoRollbackRuleAttribute#NoRollbackRuleAttribute(Class) * @see org.springframework.transaction.interceptor.DefaultTransactionAttribute#rollbackOn(Throwable) */ Class<? extendsThrowable>[] noRollbackFor() default {};
/** * Defines zero (0) or more exception name patterns (for exceptions which must be a * subclass of {@link Throwable}) indicating which exception types must <b>not</b> * cause a transaction rollback. * <p>See the {@linkplain Transactional class-level javadocs} for further details * on rollback rule semantics, patterns, and warnings regarding possible * unintentional matches. * @see #noRollbackFor * @see org.springframework.transaction.interceptor.NoRollbackRuleAttribute#NoRollbackRuleAttribute(String) * @see org.springframework.transaction.interceptor.DefaultTransactionAttribute#rollbackOn(Throwable) */ String[] noRollbackForClassName() default {};