Spring Data – Multi-column searches

Here is sample of such Specification for User: public static Specification<User> containsTextInName(String text) { if (!text.contains(“%”)) { text = “%” + text + “%”; } String finalText = text; return (root, query, builder) -> builder.or( builder.like(root.get(“lastname”), finalText), builder.like(root.get(“firstname”), finalText) ); } or even more customizable implementation: public static Specification<User> containsTextInAttributes(String text, List<String> attributes) { if … Read more

How to store @Query sql in external file for CrudRepository?

Use below steps. Create jpa-named-queries.property file in src/main/resources–>META-INF Folder Defile your query in given properties file. Above screenshot look closely.Here Group is Entity name, while Method should match with method define in Repository interface. Query should have object name instead table name and instead of column name provide variable name given in entity for respective … Read more

java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;

This error occurs because JPA 1 API is brought in, but method getValidationMode exists only since JPA 2. Instead of following <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0.2</version> </dependency> for example one offered by Hibernate can be used: <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.1.Final</version> </dependency>

Spring Data JPA: Query by Example?

This is now possible with Spring Data. Check out http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example Person person = new Person(); person.setLastname(“Smith”); Example<Person> example = Example.of(person); List<Person> results = personRepository.findAll(example); Note that this requires very recent 2016 versions <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.10.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> <version>1.12.1.RELEASE</version> </dependency> see https://github.com/paulvi/com.example.spring.findbyexample

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