Spring ROO sorting - spring

I have a set of Entities generated using Spring ROO. After several commands, basically using web mvc, I have a set of links to get the contents from a database. I need to order the contents according to title, instead of the order in the table. How can I do it? Is there any Spring ROO commando or annotation to order the items.
For more information, I am using Spring Roo 1.2.1, and the finder I got is the following.
public static List<Deporte> Deporte.findAllDeportes() {
return entityManager().createQuery("SELECT o FROM Deporte o", Deporte.class).getResultList();
}
Thanks in advance.

Spring Roo uses JPA for the persistence. Search for JPA tutorials and documentation to find what you're looking for.
JPQL uses the order by clause to order results returned by a query. This order by is very similar to the SQL order by clause:
SELECT o FROM Deporte o order by o.title

Related

Native Query in spring boot JPA

I'm using spring boot 2.6 and spring boot JPA version 2.6.4. I was interested in pull last five records from mysql table by using next query
#Query(value="SELECT * FROM produit p ORDER BY p.id DESC LIMIT 5")
public Collection getProduit();
Normally in the #Query block, i need to add the nativeQuery = true after the value property but the nativeQuery attribute is not defined. What am i missing?
Please help, already 5 hours searching on this.
When using a native query you need to define a mapping of the values to for example your own type. You could use interface based projections when you only need to read data see interface based projections. Then your could would look like
#Query(nativeQuery=true, value="SELECT * FROM produit p ORDER BY p.id DESC LIMIT 5")
Set<Projection> getProduit();
Another thing to check would be what import is used for the #Query annotation, maybe it is the wrong one.

how to approach such an assigment: Spring Project, H2 database, simple model (String name, BigDecimal price)

how would you approach problem of simple app, allowing users to summarise/calculate average of price values of inventory stored? MVC model, Spring, H2. Do I need Hibernate to achieve that? How to access fields of particular items stored?
Is it a requirement to use H2? If no, just 'read' the inventory and calculate on the fly. Build the minimal solution.
If yes, I personally prefer to go with Spring Boot / Spring Data / Hibernate as this is widely used and better to maintain than a self-build solution. You could get the information with a custom query at the repository - something like:
#Query(value = "SELECT AVG(price) FROM product")
public Double getAveragePrice();
With https://bootify.io you can setup your Spring Boot app with the database model, and can add the custom logic on top.

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");

Database specific queries in a Spring Hibernate application

In a dao class implementation,I want to use different sql query depending upon the underlying database. Since my SQL query is complex which selects from a database view and uses "UNION" key word and uses database specific functions, I can not use JPQL (or HQL). I did some search on Stackoverflow and threads suggest the good way would be to find out the dialect used in the application. Can anyone provide some code example?
EDIT : My apologies, I did not explain my question well enough. In my dao class implementation , I want to determine the database ( say mysql or oracle) on which my application is running and then execute the appropriate query. I need the code like jdbcTemplate.findOutTheDialect().
JPA have the native queries for that.
An example you can find here.
You can use spring JdbcTemplate to connect and query from your database.
For Example..
String query = "SELECT COUNTRY_NAME FROM COUNTRY WHERE MCC_COUNTRY NOT IN ('Reserved') ORDER BY COUNTRY_NAME";
jdbcTemplate.query(query, new ObjectRowMapper());
Where "ObjectRowMapper" will be a class to map return resultset to list of Objects.

Projection on a MongoDb Query using Spring data and QueryDSL

I have a Spring MVC/Spring Data / Mongo DB application.
I have setted up my environement according the the spring data documentation and my repositories work fine (I can execute queries with predicates)
I was wondering if it was possible to execute a type safe query (using Spring Data and QueryDSL) while making a projection (I want only a few fields of a very big document).
The QueryDSL documentation gives an example for Hibernate but states it can be done in all modules QueryDSL Documentation (but I haven't been able to find out how to do it with Mongo)
here's the code snippet for hibernate
class CustomerDTO {
#QueryProjection
public CustomerDTO(long id, String name){
...
}
QCustomer customer = QCustomer.customer;
JPQLQuery query = new HibernateQuery(session);
List<CustomerDTO> dtos = qry.from(customer).list(new QCustomerDTO(customer.id, customer.name));
Any Ideas ?
This is currently not supported. Feel free to add a ticket for it into our Issue tracker.
The Lucene and Mongodb modules of Querydsl support only direct projections from the query root, but for custom projections something could be figured out.
I've just built a projection like this:
Criteria c1 = Criteria.where("field.name").is("val")
Criteria projection = Criteria.where("field").is(1)
BasicQuery query = new BasicQuery(c1.getCriteriaObject(), projection.getCriteriaObject())

Resources