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

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.

Related

Do you write Hibernate code inside POJOs?

We have a web application which uses JSP, Servlet and Hibernate. The design pattern we got is MVC like, which means,
JSP -> Servlet -> Beans (POJO)
Now the question. Once of out developers has inserted the hibernate queries, hibernate session creation etc inside POJOs. Now inside these POJOs, there are methods like getAllEmployees(), getAllAgents() etc. This made it extremely hard for us to change the database tables (we handle database manually, using MySQL Workbench) and use automatic tools to re-generate the POJOs because these methods will be lost.
Now there are 2 arguments. One is that this maintaining hibernate queries, sessions inside POJOs is a good work, because it seems like pure MVC. The other argument is move the hibernatre code to Servlets and call the POJOs just like beans, only to set and get values.
We have not worked in Hibernate before. From above 2, what is the preferred place to write hibermnate code when it comes to hibernate?
Finally, please note we are not interested in Spring or other frameworks, we use pure JSP and Servlet with Hibernate
Probably, what you need is another layer of abstraction. Since your pojos are recreated after new migrations, you shouldn't insert code in it (I don't agree with that aproach, but that's just my opinion :-) )
JSP -> Servlet -> NewLayer -> POJO
I don't know where you put your business rules, but in this scenario it will be in the "NewLayer" wich would consist of a "hibrid" layer of service and dao.
I would recommend these readings to rethink your actual architecture:
https://softwareengineering.stackexchange.com/questions/220909/service-layer-vs-dao-why-both
Responsibilities and use of Service and DAO Layers

POJO or DTO approach

I am developing a new web application with Struts2, Spring and Hibernate as its core building blocks.
We have created POJO classes with respect to hibernate mapping files.There will be some inputs from users which needs to be updated in to the underlying database
e.g registration or updation.
We have few option like creating new POJO/DTO for action classes which will be filled by the Struts2 and than we can transfer them to the service layer where we can convert those DTO to the respected hibernate POJO else we can expose same POJO to struts2 so that the framework can fill them with the user input and we need not to do the work for conversion and creating extra set of classes.
Application will be not big in size and will have a medium size application tag.
My question is what is the best way to transfer this user input to underlying hibernate layer to perform data base specific work.
Thanks in advance
I'd prefer the "DTO" approach in this case since you then can validate the input first and trigger updates only when wanted.
However, you could use detached entities as DTOs and reattach them when you want to create or update them. If you don't want the web part of your application to depend on Hibernate and/or JPA you might need to create another set of classes (unless you don't use a single annotation).
You'll get both answers on this.
With Struts 2 I tend to use normal S2 action properties to gather form values/etc. and use BeanUtils to copy them to the Hibernate objects. The problem with exposing the Hibernate objects to the form, like with ModelDriven etc. is that you need to define whitelists/blacklists if you have columns that should not be set directly by the user. (Or handle the problem in a different way.)
That said, I'm not fundamentally opposed to the idea like a lot of people are, and they're arguably correct.

Dynamic fields in JSF beans

I want to let my client create his own fields and bean in the CMS dynamically.
As well, once he creates a form, I need to create an Hibernate Entity that could be saved to the database.
Is there a way to do it?
I am using JSF2 and Hibernate 3
With recompiling and without?
Creating tables and entities dynamically is IMO not a good idea. Hibernate is not really made for that and generating entities is only a small part of the problem. You would have to add them to the configuration, rebuild a session factory, update the model. And what about subsequent restarts of the application? Not recommended, just forget this approach...
Another option would be to use an Entity-Attribute-Value (EAV) model. This is something many CMS are doing. I've never implemented this with Hibernate but it's doable (and has already been done). Here are some resources:
Adding new persistable classes at runtime
[hibernate-dev] dynamic entities (has some sources attached)
But to be honest, I wouldn't implement my own CMS but rather reuse an existing one. One Hippo seems to be a candidate.
See also
The CCK buzz (Content Creation Kit) and the EAV problem
Related questions
Entity Attribute Value Database vs. strict Relational Model Ecommerce question
Approach to generic database design
How do you build extensible data model
I am looking for something similar to the drupal CCK, but in Java (in a Java CMS)?
Java Frameworks that support Entity-Attribute-Value Models
Implementing EAV pattern with Hibernate for User -> Settings relationship
Easiest way would be using a List<String> for the field names and a Map<String, Object> for the field values. Maps can be accessed in EL using dynamic keys like so:
<ui:repeat value="#{bean.fieldnames}" var="fieldname">
<h:inputText value="#{bean.fieldvalues[fieldname]}" /><br />
</ui:repeat>
A completely different alternative is to autogenerate classes using tools like ASM/Javassist and creating database tables on the fly. But that's a lot more work.

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.

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