How to return model object from jpa query - spring-boot

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.

Related

Is it necessary to use Entity annotation for Select query records from Database

I have spring boot application with JPA and MySQL. The table is already created and data are present. From the spring boot application, we need to create two API - get a particular record using id and get all the records. I created a model class but confused with usage of #Entity. Because I'm not going to insert/delete a record from the database. I want to use only for select the record.
For Select query (findBy), do we need to use #Entity annotation at the top of model class?
Yes, you would need to use #Entity to mark your model class, this tells JPA that your class is used for mapping to and from a database table. If you want to make sure that you don't accidentally overwrite the data, you could implement a check via #EntityListener, as described in this answer.

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

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?

Handling filtering by a particular field in JPA Spring Boot

I want to implement a filtering feature based on the properties of an entity that I have stored in my db.
I'm using a JPA query to do so. The problem that I'm facing is concerned with entity manager which requires the class of the object that is required to return.
public List<CountryEntity> getSortedCountries(String field, String type) {
return entityManager.createQuery(GET_ALL_SORTED.concat(field).concat(" " + type), CountryEntity.class).getResultList();
}
When I select only one field, let's say the name of the country, the query returns a String and not an object of type CountryEntity.
What is the best approach to handle this problem? Should I create classes for every single case or is there another way that I'm missing?

Expose custom query in Spring Boot Rest API with multiple joins

I have an Spring REST Api and a MySQL Database, now I would like to expose the result of an custom query with multiple joins.
I have tried multiple suggestions that I found online but none of them were working for me so far.
What I want to do is something like a read only DTO that has all the fields of my custom query so that in the end I have one api page exposing the DTO data as JSON so my client (Angular) can read the data from there.
I already tried to:
create an #RestController with an injected EntityManager that executes a NativeQuery and then populates the DTO with the returned data but since my DTO is no Entity I get an Hibernate Mapping Exception
create a custom Repository and its Impl but with a similar outcome
place the Query inside an existing #Entity that is part of the Query statement
What am I missing here? Do I have to annotate my DTO maybe? Cuttently it's just a POJO, I think the #Entity annotation is not the right thing here since I don't want a Table created from my DTO.
Fixed it by letting the Query return an Array of type Object and afterwards mapping it to the DTO Constructor.

How to get column names of a table using spring data jpa

I am using spring boot, spring data JPA, I am searching for solution to get all column names of a given table. But could not found as per my requirements
Not want a solution with native query.Looking for general solution using spring data abstraction.
I am able to get the column names using normal java but i want to fetch them using spring data JPA.
they are several ways
public interface TableMetadataRepository extends JpaRepository<TableMetadata, TableMetadataKey>
{
TableMetadata findByTableName(String tableName);
}
then you can go for
List<TableMetadata> metadata = tableMetadataRepository.findAll()
TableMetadata metadataofspecifictable = tableMetadataRepository.findByTableName("urtable");

Resources