Spring Data: Override save method

Simply create your custom interface as usual and declare there the methods you want to ovverride with the same signature of the one exposed by CrudRepository (or JpaRepository, etc.). Suppose you have a MyEntity entity and a MyEntityRepository repository and you want to override the default auto-generated save method of MyEntityRepository which takes an only … Read more

Configure specific in memory database for testing purpose in Spring

Spring profiles can be used for this. This would be a specific way: Have environment specific properties files: application.properties: spring.profiles.active: dev application-dev.properties spring.jpa.database: MYSQL spring.jpa.hibernate.ddl-auto: update spring.datasource.url: jdbc:mysql://localhost:3306/dbname spring.datasource.username: username spring.datasource.password: password application-test.properties spring.jpa.database: HSQL Have both MySQL and H2 drivers in pom.xml, like this: <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <scope>test</scope> </dependency> … Read more

How to enable LockModeType.PESSIMISTIC_WRITE when looking up entities with Spring Data JPA?

@Lock is supported on CRUD methods as of version 1.6 of Spring Data JPA (in fact, there’s already a milestone available). See this ticket for more details. With that version you simply declare the following: interface WidgetRepository extends Repository<Widget, Long> { @Lock(LockModeType.PESSIMISTIC_WRITE) Widget findOne(Long id); } This will cause the CRUD implementation part of the … Read more

Cannot load driver class: com.mysql.jdbc.Driver Spring

If you’re using Maven, add this to your pom.xml: (Recommended) For MySQL 5.6, 5.7, 8.x and Java >= 8 use; <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.30</version> </dependency> (Legacy) For MySQL <= 5.5 or Java <= 7 or JDBC < 4.2 use; <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency> More details on Connector/J versions

how to show query while using query annotations with MongoRepository with spring data

I add the line (below) in application.properties and works fine: logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG for query: @Query(“{$and: [{‘$or’ : [{ ‘name’: {$regex : ?0, $options: ‘i’}}, {‘description’: {$regex : ?1, $options: ‘i’}}]}, { ‘deleted’ : ?2 }]}”) obtain this log: 2016-09-27 10:53:26.245 DEBUG 13604 — [nio-9090-exec-3] o.s.data.mongodb.core.MongoTemplate : find using query: { “$and” : [ { “$or” : … Read more

Are you supposed to have one repository per table in JPA?

As repository is a concept derived from Domain Driven Design, thinking about database tables is the wrong approach. By definition you access aggregate roots from a repository. Effectively a repository is simulating a collection of these. Now what forms an aggregate root? Probably even more interesting: what does not? That’s, of course, highly dependent on … Read more

What is the difference between an Spring Entity Manager and Spring Data Repository?

There are several layers of working with persistent data in Java/Spring: JDBC JdbcTemplate JPA (contains EntityManager) Spring Data JPA (contains Repository) Each abstraction shields developers from lower-level details, but it can bring its own complexities. JdbcTemplate is a thin abstraction over plain JDBC. Repository is an abstraction over EntityManager. It shields developers from some complex … Read more