Why The Returned Data Type of findAll Method Are Different Between CrudRepository And JpaRepositiory? - spring-data-jpa

The returned data types of findAll method are Iterable and List for CrudRepository and JpaRespository. And JpaRepository extends PagingAndSortingRepository which extends CrudRepository. I am wondering why the returned data type are different. Any insight?

CrudRepository is defined in spring-data-commons which is used by all the Spring Data modules.
The idea behind using Iterable was to allow for stores to not materialise the full result when the method returns.
Queries in JPA return List instances and therefore there is no reason not to return List instances.
In practice no module returns a non-collection Iterable and the team just discussed that we might change the return type to Collection in the not too far future.

Related

Understanding Firestore's Underlying Serialisation Mechanism

I would like to get some information on how the serialisation/deserialisation mechanism works in Firestore. The issue I am having is that I am passing a Scala (JVM language) object into the Firestore create method but it blows up at the point of serialising that data. After some investigation, it appears that Firestore requires values created from classes which have an empty public constructor, why is this a constraint? This is something that Scala classes do not have. Is there anyway to side-step Firestore's serialisation and provide my own?
Firestore requires values created from classes which have an empty public constructor, why is this a constraint?
When you load data from Firestore, you have the option to read a Java object from the document by calling DocumentSnapshot.toObject(Class<T> valueType). In order to create the object that it returns, the Firestore SDK must be able to call a constructor on the class that you pass in. And the only constructor it can reasonably call, is one without any arguments, as it has no way to determine what argument values to pass in otherwise.
Note that calling toObject is not the only option to create an object out of a DocumentSnapshot. You can also construct the Java object yourself, and extract the individual values from the snapshot by calling its get methods.
A quick search seems to hint that it is possible to add a no-argument constructor in Scala too, so I also recommend checking out:
Scala case classes with new firebase-server-sdk
A quick overview of using Firebase in a Scala application

Why RealmCollectionType methods return Results and not RealmCollectionType/AnyRealmCollection?

I recently moved from Array to RealmCollectionType because it provides more effective filters. Now I want to migrate my unit-tests as well, but I don't like the In-memory Realm because it requires me to setup a lot of links and relations between my objects. I was trying to mock Results and LinkingObjects by conforming my mock to RealmCollectionType. Unfortunately, I'm stuck implementing filter operation because it should return Results which is declared as final.
What is the purpose of filters to narrow it's return type to Results?
RealmCollection.filter(...) returns a Results because that's the query result container in Realm. It does share some common interface elements with other collection types in Realm (like LinkingObjects and List), which is why it conforms to the RealmCollection protocol.
If you'd like to test code that's generic on say the Collection protocol in the Swift standard library, which RealmCollection inherits from, you can do so.

Linq to Entities without generic parameter

If this is a property implementation where Context is an Entity Framework DbContext and Tours is a DbSet...
public IQueryable ListQuery => Context.Tours;
... then the calling code has limited ability to continue the query. For example, I can't even call ToList() on the result.
How do I return an un-typed query, such that I can do things like Take and Skip on the result, without the calling code knowing what the type is?
I don't want the type to leak out of the interface, because then my data access code is tied to one EF model in particular.
I cast the result to IQueryable<object>. I'm not sure why that's allowable. Is it because its covariant?

JpaRepository vs CRUDRepository findAll

I have a simple question: why JpaRepository is returning List of entities but CrudRepository returns Iterable of entities?
Is it done on purpose? I guess it's because CrudRepository is more generic interface and there may be some specific repository which returns Iterable.
It makes harder to use CrudRepository without using specific JpaRepository..
Thanks
The class CrudRepository is part of the Spring Data Commons project and is the recommended interface to extend regardless of the actual data store used.
The reason CrudRepository methods return Iterable and not List (or Set) is because some data stores allow streaming of results and using a Collection type would result in loss of functionality for such stores.
JpaRepository extends PagingAndSortingRepository
and PagingAndSortingRepository extends CrudRepository.
This allows JpaRepository to have a more specific return type of Itrable which is List

How to filter GWT requestFactory results?

I have a question about how to do data filtering with RequestFactory in GWT. I am currently working on an application which is backed by a MySQL database. My persistence layer is based on JPA/Hibernate. I am using RequestFactory to query my database for all my listing-related operations.
So for example, I have a Person object : In my PersonRequestContext I have a method which allows me to list persons. The method signature is :
Request<List<PersonProxy>> listPersons(Integer firstResult, Integer maxResults);
As you may have guessed, the corresponding query is something like this :
entityManager.createQuery("SELECT p FROM Person p ORDER BY p.id").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
Now, I would like to filter the result based on table columns. So I wanted to use some kind of Filter class abstraction to solve it. The problem is that as we all know, it's not possible to pass non-primitive objects in to the requestFactory method.
Have you ever experienced this kind of thing ? And how did you deal with it to solve the problem?
Your assertion that only primitive types can be passed to a Request method is incorrect. See the documentation on transportable types. You can create a ValueProxy hierarchy to model your filters.