How to pass data from database to JSF page? - java

I want to pass data from database to JSF page as a table. I know that I can use ArrayList but I need more efficient way because I have database tables with 2000+ rows and more. I;m not sure but the ArrayList will consume too much memory. Is there more memory efficient way to send the data to the JSF page?
P.S maybe using pagination?

You can start with a simple PrimeFaces - DataTable - Pagination Example... its really simple and efficient...
DataTable has built-in support for ajax pagination.
Then if you will deal with really huge data you can take a look at a more advanced example.
Take a look at the PrimeFaces DataTable - Lazy Loading example
DataTable has built-in support to deal with huge datasets. In order to enable lazy loading, a LazyDataModel needs to be implemented to query the datasource when pagination, sorting, filtering or live scrolling happens
And here is a link to all PrimeFaces Datatable ability's

With Richfaces you can use database backed pagination. There is a bunch of example/tutorials if you search for richfaces pagination. Here is one that looks promising:
http://www.myjeeva.com/2011/04/do-jsfrichfacesseam-lazydynamic-data-loading-pagination/

Related

how to implement server-side processing in DataTable in JSP pages using Spring MVC?

I need to show almost 300k data in the Data table if I am trying to set all data one time in the Data table then data table becoming slow, please suggest a way IN JSP pages where I can show large data in Data tables.
Like Nagesh Tripathi wrote, you will need pagination with lazy loading to make this efficient. With plain JSP this is pretty hard to do. Maybe you want to use JSF with PrimeFaces? In that case I can recommend you the following article that discusses how this can be done efficiently: https://blazebit.com/blog/2017/primefaces-datatable-pagination.html
Here is another example that shows how you can use the pagination feature of Blaze-Persistence on top of JPA with Angular on the front-end: https://github.com/Blazebit/blaze-persistence/tree/master/examples/spring-data-webmvc

JPA for querying in-memory data

During an import procedure, I read tabular data from excel sheets.
I want to perform some operation on this data: I want to sort, search by key, filter etc.
Is it possible to reliably perform this kind of operations via JQL?
thanks
I would use Space4J and simple Collections for something like this.
Personally I think all these ORM abstractions and layers are a distraction at the small scale problem domains, and are inflexible and added complexity at the other extreme.
You could use an in-memory database like HSQLDB, fill it, then use JPA to query the data. But wouldn't using some Maps and Lists be sufficient?
DataNucleus has its own in-memory query evaluator for JDOQL and JPQL syntaxis. I've never used it outside of a full JDO / JPA persistence environment but there's no real reason why it couldn't be made to work with a little coding
If you are using EclipseLink, you can execute most queries in memory against the cache.
You just need to read everything into the cache they execute a JPQL or Criteria query with the "CheckCacheOnly" option.
See,
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching/Query_Options

Java-Spring-Hibernate: How can I save a filter and, afterward, how could I use it?

I receive a task for a project which uses Spring, Hibernate and Wicket.
In a particular HTML page I must have the possibility to create a filter(set the name of the filter and its parameters). I must create a list of filters in this way.
In the same time, I must have the possibility to edit and delete the filter, and of course, to use that filter.
Any ideas? How could I do this?
if you're looking for a component to edit a list of items, then this might help: Building a ListEditor form component
you can use spring hibernate (or other ORM's) support to retrieve the data for your model without too much hassle.

Automatic entity mapping similar to O/R-mapping with JSF?

With JPA I do not need to code the SQL for every new attribute as the o/r-mapping is being done automatically.
As I am new to JSF, i am wondering whether there is a similar possiblity with JSF?
I do not want to add new code to a jsf datatable every time I change something at the corresponding entity.
JSF provides a way to manage events and the lifecycle of a request and its linked objects. Its always possible to use any ORM framework with it because JSF doesn't play directly with the database (it doesn't even know about it). Hibernate + JSF is a very common combination.
But if you are asking about using JSF managed beans with a JPA framework, have a look at Seam: http://www.seamframework.org/.
I do not want to add new code to a jsf datatable every time I change something at the corresponding entity.
If you want a dynamic datatable you will probably have to use an add-on to core JSF. I use IceFaces and it works very well. You can use the <ice:columns> component to generate columns dynamically. We use this to display the results of a query which may return different columns.

How do you do paged lists in JavaServer Faces?

I have a JSF application that I am converting over to use webservices instead of straight up database queries. There are some extremely long lists that before could be returned easily with a simple SQL query. I'd like to figured out how implement the paging using JSF/web services. Is there a good design pattern for doing paged web services?
If it matters, I'm currently using the Apache MyFaces reference implementation of JSF with the Tomahawk extensions (a set of JSF components created by the MyFaces development team prior to its donation to Apache).
It depends on whether you want to do client-side or server-side paging. If server side, your web services will have to include a couple of additional parameters (e.g. "startFrom" and "pageSize") which will let you specify which 'page' of the data to retrieve. Your service will probably also need to return the total result size so you can generate a paging control.
If you decide that's too much effort you can do client-side paging in your backing bean (or get a component to do it for you), however it's not recommended if you're talking about thousands of objects!
I like Seam's Query objects: http://docs.jboss.com/seam/2.1.0.BETA1/reference/en-US/html_single/#d0e7527
They basically abstract all the SQL/JPA in a Seam component that JSF can easily use.
If you don't want to use Seam and/or JPA you could implement a similar pattern.
Trinidad has a table component that supports paging, which may help. It is not ideal, but works well enough with Seam, as described in Pete Muir's Backing Trinidad's dataTable with Seam blog post.
If you don't find a JSF component you like, you'll need to write your own logic to set parameters for limit and offset in your EJB-QL (JPA) queries.
We used the RichFaces library Datatable: http://livedemo.exadel.com/richfaces-demo/richfaces/dataTable.jsf?tab=usage
It's quite simple, and if you're not using RichFaces already, it's really easy to integrate with MyFaces.
If you are getting all the results back from the webservice at once and can't include pagination into the actual web service call, you can try setting the list of items to a property on a managed bean. Then you can hook that up to the "value" attribute on a Tomahawk dataTable:
http://myfaces.apache.org/tomahawk-project/tomahawk/tagdoc/t_dataTable.html
and then you can use a Tomahawk dataScroller to paginate over the list of items stored in that property. Here is the reference for that component, it works well with the dataTable component:
http://myfaces.apache.org/tomahawk-project/tomahawk/tagdoc/t_dataScroller.html
You can include this inside the header/footer facets of the dataTable or as a separate compoment (you will need to specify the id of the dataTable in the 'for' attribute of the dataScroller.
There's other neat things you can do with the dataTable like sorting and toggling details for each row, but that can be implemented once you get the basic pagination working.
Hope that helps!

Categories