Skip to content

Commit

Permalink
Add JmixDataRepository.getById method without fetch plan parameter jm…
Browse files Browse the repository at this point in the history
  • Loading branch information
dtaimanov committed Jun 13, 2023
1 parent 79f1215 commit 6caf5d2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.NoRepositoryBean;

import org.springframework.lang.Nullable;

import java.io.Serializable;
import java.util.*;

Expand Down Expand Up @@ -81,6 +81,11 @@ public T getById(ID id, FetchPlan fetchPlan) {
return idLoader(id).fetchPlan(fetchPlan).one();
}

@Override
public T getById(ID id) {
return idLoader(id).one();
}

@Override
public Iterable<T> findAll(FetchPlan fetchPlan) {
return allLoader().fetchPlan(fetchPlan).list();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;

import org.springframework.lang.Nullable;

import java.util.Optional;

/**
Expand Down Expand Up @@ -66,6 +66,15 @@ public interface JmixDataRepository<T, ID> extends PagingAndSortingRepository<T,
*/
T getById(ID id, FetchPlan fetchPlan);

/**
* Loads an entity by its {@code id}
*
* @param id must not be {@literal null}.
* @return the entity with the given id
* @throws IllegalStateException if nothing was loaded
*/
T getById(ID id);

/**
* Returns all instances of the type {@code T} loaded according to {@code fetchPlan}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@

package repository

import io.jmix.core.DataManager
import io.jmix.core.Metadata
import io.jmix.core.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.dao.IncorrectResultSizeDataAccessException
import org.springframework.data.domain.Page
Expand All @@ -57,6 +56,12 @@ class MiscDataRepositoriesTest extends DataSpec {
@Autowired
EmployeeRepository employeeRepository

@Autowired
private FetchPlanRepository fetchPlanRepository;

@Autowired
private EntityStates entityStates;

@Autowired
Metadata metadata

Expand Down Expand Up @@ -101,6 +106,29 @@ class MiscDataRepositoriesTest extends DataSpec {
customerRepository.findByName("third").size() == 0
}

void "test obtaining by id"() {
setup:
Customer first = customerRepository.findByName("first")[0]
FetchPlan instanceName = fetchPlanRepository.findFetchPlan(metadata.getClass(Customer), FetchPlan.INSTANCE_NAME)


expect:
first != null
customerRepository.existsById(first.id)

customerRepository.findById(first.id).get() == first
entityStates.isLoaded(customerRepository.findById(first.id).get(), "address")

customerRepository.findById(first.id, instanceName).get() == first
!entityStates.isLoaded(customerRepository.findById(first.id, instanceName).get(), "address")

customerRepository.getById(first.id) == first
entityStates.isLoaded(customerRepository.getById(first.id), "address")

customerRepository.getById(first.id, instanceName) == first
!entityStates.isLoaded(customerRepository.getById(first.id, instanceName), "address")
}

void "test remove method"() {
expect:
customerRepository.findAll().size() == 2
Expand Down

0 comments on commit 6caf5d2

Please sign in to comment.