How to query state stored aggregate without view table in axon framework? - spring-boot

I have a state stored aggregate which uses generic jpa repository. I do not want to apply cqrs on this aggregate so that I want to query directly this table to get all aggregates or filter by fields. This is my aggregate repository configuration;
fun rewardRepository(): Repository<Reward> =
GenericJpaRepository.builder(Reward::class.java)
.entityManagerProvider(entityManagerProvider)
.eventBus(eventBus)
.build()
How can I query this table by using spring data jpa?

Related

How to return model object from jpa query

I'm developing a Spring Boot application with Spring Data JPA. I want to return the promedio field when the calificacionSemestral1 field is a specific value and the calificacionSemestral2 field is a specific value.
Entity
DER
Doing this using JPQL is it possible?
I know that I can create a DTO with the same fields or do two queries, but I want to avoid doing it if I can get the result in a single query.

In Spring Data JPA using Schema Multi Tenant, how to join with other schema although the same entity

I am using schema-based multitenancy in Spring Data JPA.
properties.put("hibernate.multiTenancy", MultiTenancyStrategy.SCHEMA);
The table structure of the "Common" schema and the "A" schema is the same.
These two schemas are multi-tenant structures that use the same entity.
There's an entry called User in "A" schema and "Common" schema.
How do I join these two entities?
I want to create a query like this. (The schema is currently selected as A by Tenant setting.)
select u.* from user u left join common.user u2 on u.common_id = u2.id;
#Entity
public class Entity {
#Id
private Long id;
private Long commonId; // right?
}
Should I use native query without using JPQL?
That's not possible with JPA.
Btw. JPA doesn't know multitenancy that's a Hibernate feature.

How to map a Spring Data JPA repository entity into a view model?

Here is the situation, I want to fetch an entity from database and map it to a new view domain model which has more or less properties, if this view model has more properties, signs the extra properties with default value. I want a map technique in JPA to complete this, which is similar to MyBatis mapping mechanism.
So how to do it?
Just load the entity, copy it over in the new entity, fill the unset properties with the desired default values and store it using JPA (possibly via Spring Data JPA).
For copying over the data from one entity to another you might want to look int Dozer or similar libraries.
You could also misuse Spring Data's projection support to query the original entity, but return it as the target entity with methods similar to the following:
interface SourceRepository<Source, Long> extends CrudRepository<Source, Long> {
List<Target> findTargetBy();
}
The resulting Target entities then could be stored again using another repository (you might have to set version and id properties to null to make it clear to the framework that these are new entities.

How to limit the results of a Spring Repository with custom query

I am using a Spring repository to query a database, but because of the nature of my application I need to use a custom #Query in order to JOIN FETCH lazy collections.
This process works fine, but now I need to limit the result to a single record. I understand that Spring has the notion of findFirst or findTop1 in the method names, but this does not appear to work when you have a custom query.
How can I use a custom query and limit the result to 1 record when using a Spring repository?
you need to pass a Pageable param in your query method
#Query("select e from Entity e LEFT JOIN FETCH e.list")
public Page<Entity> find(Pageable pageable);
and call the method passing the object
repository.find(new PageRequest(0, 1));

jpa pagination suppress count query

We are using Spring Data JPA Repository.
For pagination we are passing the Pageable object to the JPA Repository findBy Methods.
Since in our UI , we are not displaying the total count of records, we don't want the count query to be fired .
Is there any way to suppress the count query fired during pagination ?
Have your repository method return List<Entity> rather than Page<Entity>, and I believe it won't run the count query.
See "Special Parameter Handling" in Spring Data JPA documentation section "1.2.2 Defining query methods".

Resources