Jsf DataModel Impl That Supports Paging with JPA - java

I need a javax.faces.model.DataModel implementation that retrieves table page elements using a JPA SELECT query as the user scrolls to a new page in the data table.
Something like javax.faces.model.ResultSetDataModel but it shall uses JPQL to fetch elements. Or is there a way to use javax.faces.model.ResultSetDataModel in JPA environment?
Thanks

I have an example on GitHub showing a custom DataModel that supports paging and selection of items. Have a look at the SelectableDataModel and PageableDataModel where the paging is done.
Those custom DataModel classes use a DataProvider to fetch the paged data from anywhere. In the example I use a DataProvider implementation to get data from a DB via JPA.
I created this example for a session at the JAX 2011. The slides for this session are available too, but only in German.
The example is built with MyFaces 2.1.3, OpenWebBeans (CDI), Hibernate (JPA) and an in memory Derby DB.

Related

Get Data from 2 tables in spring boot jpa

I have one table which has all the api audit information - Table name : api_audit
I have one table which has extra information about every api call - Table name : api_audit_info
Inside api_audit I have primary key as "transaction_id".
I want all the data from api_audit table and some data from api_audit_info table.
I have written a custom query like -
#Query(select c from ApiAudit c INNER JOIN ApiAudiInfo t ON c.transactionId = t.msgId)
But the issue is that the result type that I am getting this way contains only ApiAudit type data.
What shall I do to get data from both the tables. Please help.
Note: I am using JpaRepository as I need paginated data.
I am fairly new to Spring boot and JPA so not sure exactly which direction to look to.
Whenever I need to join data from more than 1 table, I am using Jdbi.
Here you have the official documentation:
Remember to include the required dependencies and configure a bean for Jdbi in your project.
Then I create a repository class, POJO and query with all of the information which I need. For example:
select c.transaction_id as transactionId, t.name as name from ApiAudit c INNER JOIN ApiAudiInfo t ON c.transactionId = t.msgId
Here you have some code samples from official documentation
After you map your data to POJO, you can use
public PageImpl(List<T> content, Pageable pageable, long total)
to return paginated data.
There is a big chance that there is a better solution, but this works for me every time.

How to render data from multiple tables from MySQL using React, Spring Boot?

I have four tables in a database. When the user uses the Search option from the React application I want the search button to query all the tables and display data from all the tables which is AWS RDS MYSQL. How should I proceed? I am using Spring boot, mysql, and react.
I would recommend taking a look at JPA Respositories. Since your db seams to be small, a simple repository method like that one should make the trick.
However, if you have a more complicated requirement, you can use a Spring Projection. Create a query that retrieves all the fields you'll need, even if they're from different tables, and map the result into a Spring Projection
Using spring data jpa you should create a method in your repository that returns a list of your projection class:
public interface MyRepository extends Repository<MyEntityProjection, Long> {
#Query("SELECT ... WHERE field = ?1")
List<MyEntityProjection> getData(String param);
}
The projection class should be something like this:
public interface MyEntityProjection {
String getField();
String getField2();
}
Adding as many fields as your query returns.
Read the docs I linked for more information and examples.

I have to show the all values in lookup tables values as dropdown in UI I am using hibernate session query and I am storing in cache

But the cache is not storing values its querying each Tuple(line) of database which is even worse than implementing cache
I have done this steps
Marking entity as #Cachable and #Cache(read only , my region)
and session(set Cachable = true)
In hibernate configuration I have set use query cache= true
I am using ehcache
I want all the my POJO objects from the database table using cache someone please help me
Thanks in advance
this may be caused with many reasons, first of all, are you config query cache in your configuration XML file (witch is hibernate.cfg.xml in pure Hibernate project or persistence.xml in JPA Project)

Nulls last in hibernate 3.6 GA and Spring 4

I am using PostgreSQL database with Hibernate and Spring Rest.
I am getting sorted data in Hibernate Criteria from Postman but when data is sorted on descending order the NULL values come first, I believe this may be how PostgreSQL is sending data to Hibernate.
I know in Hibernate 4.2.x and 4.3.x there are ways to implement this using Order as shown below
Criteria criteria = ...;
criteria.addOrder( Order.desc( "name" ).nulls(NullPrecedence.LAST) );
But there is no similar implementation in 3.6 as I was getting compilation when I tried the same.
From Hibernate order by with nulls last I tried setting the Hibernate properties:
hibernate.order_by.default_null_ordering=last
It did not help me.
I am setting it as Hibernate configuration parameter, but it's not working.
I have to modify legacy code which is using Criteria so how can I implement NULLs ordering while using Criteria in Hibernate 3.6?

Are Hibernate filters only applied after the data has been loaded from db?

I've found some contradicting information on the net. Does anyone know whether Hibernate filters affect the generated sql, or is it just filtering the data as it's read from the database?
Hibernate filters affect the where clause of the generated SQL.
The Introduction to Hibernate Filters is a nice article on filters and provides a demo application allowing to play with them.
If you enable SQL in Hibernate using show_sql"(+"format_sql"),
and execute query with enabled filter, you will see the result.
For example:
select
item0_.ID as ID0_
from
ITEMS item0_
where
item0_.deleted = 'FALSE' <-- here is filtering

Categories

Resources