У меня Spring-boot, findByIdAndDeletedFalse(id) работал, но findById(id) не работаетJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Гость
 У меня Spring-boot, findByIdAndDeletedFalse(id) работал, но findById(id) не работает

Сообщение Гость »

Код: Выделить всё

findById
method in Spring JPA is not returning a record for

Код: Выделить всё

PlanEntity
with an ID of 5, even though the record exists in the database,but

Код: Выделить всё

findByIdAndDeletedFalse
method returns that record with an ID of 5 correctly.
Link of my project.
I checked all the relationships in the plan table and its related entities,they were all correct.
The following is my generic repository:

Код: Выделить всё

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;

import java.util.List;
import java.util.Optional;
@NoRepositoryBean
public interface GenericRepository extends JpaRepository  {
List findByDeletedFalseOrderByIdDesc();
List findByIdIn(List EntityIds);
Optional findById(Integer EntityId);
Page findByDeletedFalseOrderByIdDesc(Pageable pageable);
Optional findByIdAndDeletedFalse(Integer id);
}
and the follwoing is my PlanRepository:

Код: Выделить всё

import edu.educate.model.ElementEntity;
import edu.educate.model.PlansEntity;
import edu.educate.repository.baseRepository.GenericRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PlansRepository extends GenericRepository {
List findByElementStatusNotIn(List elementEntities);

Page findAll(Specification dateSpec, Pageable pageable);

List findAll(Specification dateSpec);
}
and these are my service methods:

Код: Выделить всё

@AllArgsConstructor
public class GenericServiceImpl implements GenericService {

protected final GenericRepository repository;
protected final String entityName;

@Override
public T getEntity(Integer id) {
Optional entity = repository.findByIdAndDeletedFalse(id);

if (entity.isEmpty()) {
throw new ItemNotFoundException(entityName + " id: " + id);
}

return entity.get();
}
@Override
@Transactional
public Optional getEntityById(Integer EntityId){
return repository.findById(EntityId);
}
}
and my Test methods:

Код: Выделить всё

@Test
public void testFindById() {

PlansEntity plan = plansService.getEntityById(5).get();

assertNotNull(plan);
}

@Test
public void testFindByIdAndDeletedFalse () {

PlansEntity plan = plansService.getEntity(5);

assertNotNull(plan);
}
output of

Код: Выделить всё

testFindById()
:

Код: Выделить всё

java.util.NoSuchElementException: No value present

at java.base/java.util.Optional.get(Optional.java:143)
``



Источник: https://stackoverflow.com/questions/781 ... es-not-wor
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»