Hibernate: select people0_.id as id1_0_, people0_.birth as birth2_0_, people0_.email as email3_0_, people0_.last_name as last_nam4_0_ from jpa_people people0_ where people0_.last_name=?
Repository 接口是 Spring Data 的一个核心接口,它不提供任何方法,开发者需要在自己定义的接口中声明需要的方法 publicinterfaceRepository<T, ID extendsSerializable> { } Spring Data可以让我们只定义接口,只要遵循 Spring Data的规范,就无需写实现类。 与继承 Repository 等价的一种方式,就是在持久层接口上使用 @RepositoryDefinition 注解,并为其指定 domainClass 和 idClass 属性。如下两种方式是完全等价的
//where lastName like ?% and id < ? List<People> getByLastNameStartingWithAndIdLessThan(String lastName, Integer id);
//where lastName like %? and id < ? List<People> getByLastNameEndingWithAndIdLessThan(String lastName, Integer id);
//where email in (?, ?, ?) or birth < ? List<People> getByEmailInOrBirthLessThan(List<String> emails, Date birth);
//查询id最大的people @Query("select p from People p where p.id = (select max(p2.id) from People p2)") People getMaxIdPeople();
//使用占位符的方式传递参数 @Query("select p from People p where p.lastName = ?1 and p.email = ?2") List<People> getPeople(String last, String email); //命名参数的方式 @Query("select p from People p where p.lastName = :lastName and p.email = :email") List<People> getPeople1(@Param("lastName") String last, @Param("email") String email); @Query("select p from People p where p.lastName like %:lastName% and p.email like %:email%") List<People> getPeople2(@Param("lastName") String last, @Param("email") String email);
publicinterfacePeopleRepositoryextendsRepository<People, Integer> { @Modifying @Query("update People p set p.email = :email where p.id = :id") voidupdatePeopleEmail(@Param("id") String id,@Param("email") String email); }
在使用修改的情况下面,如果不加上@Modifying就会报错“Not supported for DML operations ”
加上了@Modifying还是会报另外一个错误nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query 这个因为需要事务,我需要把这个update()放到service中,其中加上 @Transactional注解
1 2 3 4 5 6 7 8 9
Spring Data 提供了默认的事务处理方式,即所有的查询均声明为只读事务。 对于自定义的方法,如需改变 Spring Data 提供的事务默认方式,可以在方法上注解 @Transactional 声明
进行多个 Repository 操作时,也应该使它们在同一个事务中处理,按照分层架构的思想, 这部分属于业务逻辑层,因此,需要在 Service 层实现对多个 Repository 的调用,并在相应的方法上声明事务。
/** * Extension of {@link CrudRepository} to provide additional methods to retrieve entities using the pagination and * sorting abstraction. * * @author Oliver Gierke * @see Sort * @see Pageable * @see Page */ @NoRepositoryBean publicinterfacePagingAndSortingRepository<T, ID> extendsCrudRepository<T, ID> {
/** * 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); }
Hibernate: select people0_.id as id1_1_, people0_.address_id as address_5_1_, people0_.birth as birth2_1_, people0_.email as email3_1_, people0_.last_name as last_nam4_1_ from jpa_people people0_ order by people0_.id asc, people0_.email desc limit ?, ? Hibernate: select count(people0_.id) as col_0_0_ from jpa_people people0_
/** * Flushes all pending changes to the database. */ voidflush();
/** * Saves an entity and flushes changes instantly. * * @param entity * @return the saved entity */ <S extendsT> S saveAndFlush(S entity);
/** * Deletes the given entities in a batch which means it will create a single {@link Query}. Assume that we will clear * the {@link javax.persistence.EntityManager} after the call. * * @param entities */ voiddeleteInBatch(Iterable<T> entities);
/** * Deletes all entities in a batch call. */ voiddeleteAllInBatch();
/** * Returns a reference to the entity with the given identifier. * * @param id must not be {@literal null}. * @return a reference to the entity with the given identifier. * @see EntityManager#getReference(Class, Object) * @throws javax.persistence.EntityNotFoundException if no entity exists for given {@code id}. */ T getOne(ID id);
Hibernate: select people0_.id as id1_1_0_, people0_.address_id as address_5_1_0_, people0_.birth as birth2_1_0_, people0_.email as email3_1_0_, people0_.last_name as last_nam4_1_0_ from jpa_people people0_ where people0_.id=? Hibernate: update jpa_people set address_id=?, birth=?, email=?, last_name=? where id=? false