spring-data-jpa

Spring boot JPA no returning existing result using findById

心不动则不痛 提交于 2021-02-11 02:45:31
问题 I have created a pretty small and simple Spring Boot app using the Oracle database and some JPA queries. This is the code snippet which is not returning data, which is actually exists in database. letterRecipientNonOas = letterRecipientNonOasRepository .findById(Long.valueOf(letterRecipientDTO.getNonOas().getId())) .orElseThrow(() -> new EntityNotFoundException(LetterRecipientNonOas.class, Constant.MESSAGE_ENTITY_NOT_FOUND)); here findById is returning empty result set. this is my repository

How to set placeholder values in properties file in spring

不问归期 提交于 2021-02-10 07:40:43
问题 Below is application.properties file app.not.found=app with {0} name can not be found. How to replace {0} with some value in spring? I am using below code to read properties file values. env.getProperty("app.not.found") but not getting how to set placeholder values. 回答1: Use MessageFormat.format(String pattern, Object ... arguments) . It accepts an array in second parameter, which will replace 0, 1 , 2 ... sequentially. MessageFormat.format(env.getProperty("app.not.found"), obj) obj will

Creating tables and importing data from .sql files in Spring boot

故事扮演 提交于 2021-02-10 06:42:08
问题 Current apporach: application.properties spring.datasource.url=jdbc:mysql://localhost:3306/db_name spring.datasource.username=root spring.datasource.password=admin spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.testWhileIdle = true spring.datasource.validationQuery = SELECT 1 spring.jpa.show-sql = true spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy spring.jpa.properties.hibernate.dialect=org

Unable to Field identify bean named 'entityManagerFactory' as using Auto-wiring annotation to Repository

折月煮酒 提交于 2021-02-10 06:18:28
问题 Error APPLICATION FAILED TO START Description: Field ProRepo in com.pvs.products.testservice.ProService required a bean named 'entityManagerFactory' that could not be found. The injection point has the following annotations: @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean named 'entityManagerFactory' in your configuration. Code build.gradle plugins { id 'org.springframework.boot' version '2.1.12.RELEASE' id 'io.spring.dependency

JPA use Spring Data Specification for delete and update

扶醉桌前 提交于 2021-02-09 11:33:57
问题 JPA Specification is powerful in order to build WHERE clauses. But, it seems to be defined only for SELECT (with findAll method). Is it possible to have the same behavior with DELETE and UPDATE? So that, it would be possible to avoid selecting scope before deleting or updating it. Thx 回答1: You can use CriteriaUpdate and CriteriaDelete. Im building my specification from a RSQL query here but it can be done in a lot of other ways as well (see https://www.programcreek.com/java-api-examples/index

JPA use Spring Data Specification for delete and update

社会主义新天地 提交于 2021-02-09 11:33:34
问题 JPA Specification is powerful in order to build WHERE clauses. But, it seems to be defined only for SELECT (with findAll method). Is it possible to have the same behavior with DELETE and UPDATE? So that, it would be possible to avoid selecting scope before deleting or updating it. Thx 回答1: You can use CriteriaUpdate and CriteriaDelete. Im building my specification from a RSQL query here but it can be done in a lot of other ways as well (see https://www.programcreek.com/java-api-examples/index

Test annotated with @DataJpaTest not autowiring field annotated with @Autowired

无人久伴 提交于 2021-02-09 11:11:58
问题 I have a Spring Boot app which contains a Spring Data Jpa repository. I need to run a unit (or component?) test around this repository. I do not have a lot of experience with Spring Data Jpa. Here's my test. It's trivially simple, and I cannot get it to pass. import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import static org.junit.Assert.assertNotNull; @DataJpaTest public class

Spring Data MongoDB Slow MongoTemplate.find() Performance

青春壹個敷衍的年華 提交于 2021-02-09 09:57:38
问题 I'm having performance issues when querying ~12,000 user documents, indexed by 1 column, (companyId), no other filter. The whole collection only has ~27000. It took me about 12 seconds to get the ~12000 rows of data... I tried running explain for this query: db.instoreMember.find({companyId:"5b6be3e2096abd567974f924"}).explain(); result follows: { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "production.instoreMember", "indexFilterSet" : false, "parsedQuery" : { "companyId" : { "$eq

Spring Data MongoDB Slow MongoTemplate.find() Performance

China☆狼群 提交于 2021-02-09 09:57:12
问题 I'm having performance issues when querying ~12,000 user documents, indexed by 1 column, (companyId), no other filter. The whole collection only has ~27000. It took me about 12 seconds to get the ~12000 rows of data... I tried running explain for this query: db.instoreMember.find({companyId:"5b6be3e2096abd567974f924"}).explain(); result follows: { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "production.instoreMember", "indexFilterSet" : false, "parsedQuery" : { "companyId" : { "$eq

How to write a Spring Data JPA Specification with multiple joins?

不羁的心 提交于 2021-02-08 10:17:17
问题 The project I'm working on was generated with JHipster with support for entity filtering, which uses Spring Data JPA Specifications under the hood. The model is as follows (in JDL): entity Student { name String } entity Course { name String } entity Enrollment { } entity Attendance { date LocalDate } relationship OneToMany { Student to Enrollment(student required), Course to Enrollment(course required), Enrollment to Attendance(enrollment required) } filter all service all with serviceClass