How to add two collections inside #Document annotation while developing POJO class? - mongodb

#Document(collection = "first_collection",collection = "second_collection")
public class Foo {
}

Providing multiple collection names is not supported.This annotation marks a class as being a domain object that we want to persist to the database.
Documentation:
https://docs.spring.io/spring-data/data-mongodb/docs/current/api/org/springframework/data/mongodb/core/mapping/Document.html

Related

How to add a derived list to a model object

I'd like to add a derived list to a DTO/model object when mapping from an entity. For example, I have an entity class named Company that has a list of Employees. I'd like for the Company Model object to contain the list of Employees as well as a list of Employees who are Managers. (This is not my actual class names, but this type of thing is what I'm trying to do). So, I want the Company DTO to have a list of Employees and a list of Managers where the Managers is a subset of Employees that is derived by filtering the list of Employees.
I tried using a mapping expression and a default method. However, I need to use the EmployeeMapper and it seems that I don't have access to the mapper.
Here's what I tried...
#Mapper(componentModel = "spring", uses = {EmployeeMapper.class})
public interface CompanyMapper extends IEntityMapper<CompanyModel, Entity> {
#Mapping(expression = "java(deriveManagers(model))", target = "managers")
EntityModel toModel(Company company);
default List<EmployeeModel> deriveManagers(Company company) {
List<EmployeeModel> managers = new ArrayList<EmployeeModel>();
company.getEmployees().forEach(nextEmployee -> {
if (nextEmployee.getJobTitle().equals(JobTitle.MANAGER)) {
managers.add(this.EmployeeMapper.toModel(nextEmployee);
}
});
return managers;
}
}
I decided to do this processing in the DTO/Model object rather than in the Mapper. The list can be derived as a subset of the EmployeeModel objects in the CompanyModel object. Therefore, I think that this is the right place to derive this list. It is not really a mapping from Entity to Model object as much as it is a derived attribute in the Model object.

EclipseContext get beans of type

I have classes:
#Creatable
#Singleton
public class Sample1 implements ISample {
}
#Creatable
#Singleton
public class Sample2 implements ISample {
}
How can i get all beans of type ISample from EclipseContext?
The Eclipse context does not support anything like this. All you can do is look for objects with a specific name (or specific class which is just converted to a name).
Additionally objects declared using #Creatable are not even created and added to the context until something actually uses them.

How to query using fields of subclasses for Spring data repository

Here is my entity class:
public class User {
#Id
UserIdentifier userIdentifier;
String name;
}
public class UserIdentifier {
String ssn;
String id;
}
Here is what I am trying to do:
public interface UserRepository extends MongoRepository<User, UserIdentifier>
{
User findBySsn(String ssn);
}
I get an exception message (runtime) saying:
No property ssn found on User!
How can I implement/declare such a query?
According to Spring Data Repositories reference:
Property expressions can refer only to a direct property of the managed entity, as shown in the preceding example. At query creation time you already make sure that the parsed property is a property of the managed domain class. However, you can also define constraints by traversing nested properties.
So, instead of
User findBySsn(String ssn);
the following worked (in my example):
User findByUserIdentifierSsn(String ssn);

Spring Data JPA JPQL queries on parent interface

Say I have a #MappedSuperClass like this:
#MappedSuperclass
public abstract class Rating
{
#Id
private Long id;
#Column(name="USER_ID")
private Long userId;
private int rating;
...
With a concrete child entity like this
#Entity
#Table(name="ACTIVITY_RATING")
public class ActivityRating extends Rating
{
private Long activitySpecificData;
...
Then there is a Spring Data JPA repository like this:
#NoRepositoryBean
public interface RatingRepository<R extends Rating> extends JpaRepository<R, ID>
{
public List<R> findByUserId(Long userId);
...
and this:
public interface ActivityRatingRepository extends RatingRepository<ActivityRating>
{
}
This all works great and I can call findByUserId() on any of specific rating repositories that extend RatingRepository. I am now wanting to write some JPQL in the RatingRepository that all the child interfaces can inherit. I just don't know what (or if it's even possible) to put after the FROM in the query. For example:
#Query("SELECT NEW com.foo.RatingCountVo(e.rating, COUNT(e.rating)) FROM ??????? e GROUP BY e.rating")
public List<RatingCountVo> getRatingCounts();
I can add this method to each of the individual repositories that extend RatingRepository but everything would be exactly the same except for the specific entity name. If I want to change the query, I'd then have to go to all the child repositories and update them individually. I really want the query to live in the parent class and not be duplicated. Is there any way to accomplish this?
I'm currently using spring-data-jpa 1.7.2 and eclipselink 2.5.2. I'm not necessarily opposed to switching to newer versions if necessary.
Will it work if you will split query into 3 parts: start, entity and end of query? Than, if it'll work, in each interface you define constant like
String ENTITY = "ActivityRating";
And then you can use it like
#Query(RatingRepository.QUERY_START + ENTITY + RatingRepository.QUERY_END)
List<RatingCountVo> getRatingCounts();
BTW, there is no need to define public modifier in interface.
UPDATE: here is described another way:
#Query("SELECT NEW com.foo.RatingCountVo(e.rating, COUNT(e.rating)) FROM #{#entityName} e GROUP BY e.rating

EntityFramework query

I have problem with EntityFramework querying. Suppose i have two entities: master and child. In child entity i have field (int)SNumber. Now i want query max number from childs:
labelMaxSNumber.text = (_context.Child.Select(s => s.SNumber)).Max();
This works normally on gui, but i want create this in DataModel:
labelMaxSNumber.text = _context.GetMaxSNumber();
I don't know where post this code: in ObjectContext partial class or in EntityObject partial class?
Yes it is possible. Simply create partial class for your context and add method:
public partial class YourContextName
{
public int GetMaxSNumber()
{
return this.Child.Max(c => c.SNumber);
}
}