Spring Boot and how to configure connection details to MongoDB?

Just to quote Boot Docs: You can set spring.data.mongodb.uri property to change the url, or alternatively specify a host/port. For example, you might declare the following in your application.properties: spring.data.mongodb.host=mongoserver spring.data.mongodb.port=27017 All available options for spring.data.mongodb prefix are fields of MongoProperties: private String host; private int port = DBPort.PORT; private String uri = “mongodb://localhost/test”; private … Read more

What’s the difference between Spring Data’s MongoTemplate and MongoRepository?

“Convenient” and “powerful to use” are contradicting goals to some degree. Repositories are by far more convenient than templates but the latter of course give you more fine-grained control over what to execute. As the repository programming model is available for multiple Spring Data modules, you’ll find more in-depth documentation for it in the general … Read more

Spring Data: “delete by” is supported?

Deprecated answer (Spring Data JPA <=1.6.x): @Modifying annotation to the rescue. You will need to provide your custom SQL behaviour though. public interface UserRepository extends JpaRepository<User, Long> { @Modifying @Query(“delete from User u where u.firstName = ?1”) void deleteUsersByFirstName(String firstName); } Update: In modern versions of Spring Data JPA (>=1.7.x) query derivation for delete, remove … Read more

Spring Data JPA map the native query result to Non-Entity POJO

I think the easiest way to do that is to use so called projection. It can map query results to interfaces. Using SqlResultSetMapping is inconvienient and makes your code ugly :). An example right from spring data JPA source code: public interface UserRepository extends JpaRepository<User, Integer> { @Query(value = “SELECT firstname, lastname FROM SD_User WHERE … Read more

How are Spring Data repositories actually implemented?

First of all, there’s no code generation going on, which means: no CGLib, no byte-code generation at all. The fundamental approach is that a JDK proxy instance is created programmatically using Spring’s ProxyFactory API to back the interface and a MethodInterceptor intercepts all calls to the instance and routes the method into the appropriate places: … Read more

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

How to fetch FetchType.LAZY associations with JPA and Hibernate in a Spring Controller

You will have to make an explicit call on the lazy collection in order to initialize it (common practice is to call .size() for this purpose). In Hibernate there is a dedicated method for this (Hibernate.initialize()), but JPA has no equivalent of that. Of course you will have to make sure that the invocation is … 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