What is the difference between Factory, Collection, Collection Factory, and Repository in Magento 2? - magento2

Can anyone explain the difference between Factory, Collection, Collection Factory, and Repository in Magento 2.
Updated
And alow in which situation we use which one.

Related

Deleting from multiple repositories in Spring Data

I have an rest api which deletes from multiple database table. Each table is accessed by different repositories, so in code it deletes by calling each repo in single method which is annotated by #Transactional.
repositoryA.deleteById(id);
repositoryB.deleteById(id);
repositoryC.deleteById(id);
Is it good approach and is there any better way to do it ?

Optimistic Locking in Spring Data JDBC

I noticed that Spring Data JDBC doesn't seem to implemented Optimistic Locking (something like a JPA's #Version annotation).
I was thinking on creating a #Modifying query which considers the version field and returns boolean to check manually if the update was successful or not. But I'm afraid this approach is limited to simple entities, not aggregates implying multiple tables.
What's the best way to implement optimistic locking for aggregates?
It depends on your situation. If you just have 7 aggregates of which 5 are single entity aggregates go for the #Modifying solution for the single aggregates and write custom methods for the other 2.
If you have more aggregates consisting of more then one class consider properly implementing it and submitting a PR. The issue is already there: https://jira.spring.io/projects/DATAJDBC/issues/DATAJDBC-219
The main code changes will be in SqlGenerator which would need to add a where clause for aggregate roots if they have a version attribute.
If you are interested in doing a PR and need more assistance, please leave comment on the issue.

Modified Count when updating a entity with MongoRepository

I am using Spring Data MongoDB Repository to connect to my mongo database.
I want to update a document in a collection by passing the criteria and specific fields to update.
I do that by setting the entity object directly with the fields to update along with _id field which would be used as criteria.
This is the code I am using,
Employee updatedEmployee=employeeRepository.save(employeeToUpdate);
When I used the save method, I see I do not get any return status , whether the update was successful or not.
I do not want to make another query to mongodb to fetch the document and compare it with my updated document to validate the changes?
Is there a way with repository or should I use MongoTemplate for this specific usecase alone.

Spring data JPA get child collection attributes using projections

I would like to fetch only few attributes from child collection entitites. So, is it possible using Spring JPA projections and Spring JPA repository?
Also, it should not execute multiple queries. Just one query to fetch the selected attributes form child collection.
I want to evaluate options first if it is available in Spring JPA before considering any other alternative.

On Service Layer how to return couple entity from repository which is responsible of one entity

if i know correct each Repository is on Entity
I have and ProductService then i need to get current product's images ,
there is not some repository for Images.
And my Product repositoy is responsible of product Entity.
Repository just return to us Entity objects because repositories should be unaware from DTO objects if i know correct.
So briefly How can i return Images entity. How should i get current selected product Images.
Between product and Images Foreign Key .
But product can have 10.000 Image row in database.
I need IQueryable data of them 10 by 10.
I tried Product.Images bla bla.. .but it didnt satisfied me.
Or should i create a new ImageRepository and inject it to Product service.
Then use it with filtered method which inside in BaseRepository.
How can i design it ?
Leave the product repository alone. You can change your service layer to accept interfaces instead of a BaseRepository. That interface could deal with either EF or some other type (not strictly EF\ORM repository). There are refactoring tools that can help with this by generating the interface from the existing BaseRepository (ex. ReSharper).