Код: Выделить всё
findById
Код: Выделить всё
PlanEntity
Код: Выделить всё
findByIdAndDeletedFalse
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);
}
Код: Выделить всё
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);
}
Код: Выделить всё
@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);
}
}
Код: Выделить всё
@Test
public void testFindById() {
PlansEntity plan = plansService.getEntityById(5).get();
assertNotNull(plan);
}
@Test
public void testFindByIdAndDeletedFalse () {
PlansEntity plan = plansService.getEntity(5);
assertNotNull(plan);
}
Код: Выделить всё
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