setMaxResults for Spring-Data-JPA annotation?

As of Spring Data JPA 1.7.0 (Evans release train). You can use the newly introduced Top and First keywords that allow you to define query methods like this: findTop10ByLastnameOrderByFirstnameAsc(String lastname); Spring Data will automatically limit the results to the number you defined (defaulting to 1 if omitted). Note that the ordering of the results becomes … Read more

Difference between save and saveAndFlush in Spring data jpa

On saveAndFlush, changes will be flushed to DB immediately in this command. With save, this is not necessarily true, and might stay just in memory, until flush or commit commands are issued. But be aware, that even if you flush the changes in transaction and do not commit them, the changes still won’t be visible … Read more

Spring Data JPA – “No Property Found for Type” Exception

I ran into this same issue and found the solution here: https://dzone.com/articles/persistence-layer-spring-data I had renamed an entity property. But with Springs Automatic Custom Queries there was an interface defined for the old property name. public interface IFooDAO extends JpaRepository< Foo, Long >{ Foo findByOldPropName( final String name ); } The error indicated that it could … Read more

Disable all Database related auto configuration in Spring Boot

The way I would do similar thing is: @Configuration @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) @Profile (“client_app_profile_name”) public class ClientAppConfiguration { //it can be left blank } Write similar one for the server app (without excludes). Last step is to disable Auto Configuration from main spring boot class: @SpringBootApplication public class SomeApplication extends SpringBootServletInitializer { public … Read more

Does Spring Data JPA have any way to count entites using method name resolving?

As of Spring Data 1.7.1.RELEASE you can do it with two different ways, The new way, using query derivation for both count and delete queries. Read this, (Example 5). Example, public interface UserRepository extends CrudRepository<User, Integer> { long countByName(String name); } The old way, Using @Query annotation. Example, public interface UserRepository extends CrudRepository<User, Integer> { … Read more

What’s the difference between JPA and Spring Data JPA?

I saw Spring, JPA works around repositories (DAO layer: if I am not wrong). So I mean how it is different using ‘Spring JPA + Hibernate’ or only using ‘Hibernate’ directly? As you said, JPA is an specification while Hibernate is a particular implementation of that specification (these implementations are usually referred to as Providers). … Read more

When use getOne and findOne methods Spring Data JPA

TL;DR T findOne(ID id) (name in the old API) / Optional<T> findById(ID id) (name in the new API) relies on EntityManager.find() that performs an entity eager loading. T getOne(ID id) relies on EntityManager.getReference() that performs an entity lazy loading. So to ensure the effective loading of the entity, invoking a method on it is required. … Read more

How to add custom method to Spring Data JPA

You need to create a separate interface for your custom methods: public interface AccountRepository extends JpaRepository<Account, Long>, AccountRepositoryCustom { … } public interface AccountRepositoryCustom { public void customMethod(); } and provide an implementation class for that interface: public class AccountRepositoryImpl implements AccountRepositoryCustom { @Autowired @Lazy AccountRepository accountRepository; /* Optional – if you need it */ … Read more