-
1. Re: Call a managed bean function from converter
paucarre Sep 27, 2009 2:24 PM (in response to paucarre)Now I use the @Converter annotation and when I modify one of the fields of the form where the value has to be converted appears, then the conversation crashes. It does not call any converter function, it seems to crash when trying to load the SEAM component
I have the following annotations:
@Name(tendaConverter
)
@Converter
@BypassInterceptorsI do not understand why there is no documentation (even in comercial books) to describe how to convert an entity identification field in an entity.
Netbeans web generator creates converters for each entity getting the component from the facesContext. BUt, as I said in the previous post, it is not possible in SEAM because the framework changes the class name so it can not be casted. -
2. Re: Call a managed bean function from converter
paucarre Sep 27, 2009 7:56 PM (in response to paucarre)Finally I have solved it after 3 hours of trial and error :S
Here you have the code:package org.ajpollenca.compres.converter; import java.io.Serializable; .... import org.jboss.seam.annotations.intercept.BypassInterceptors; @Name("tendaConverter") @Scope(ScopeType.CONVERSATION) @Converter @BypassInterceptors public class TendaConverter implements javax.faces.convert.Converter, Serializable { @Transactional public Object getAsObject(FacesContext facesContext, UIComponent arg1, String value) { TendaSearcherBean tendaSearcherBean = (TendaSearcherBean) Component .getInstance("tendaSearcherBean"); return tendaSearcherBean.findByNom(value); } @Transactional public String getAsString(FacesContext arg0, UIComponent arg1, Object object) { if (object == null) { return ""; } else { return object.toString(); } } }
... and ...
package org.ajpollenca.compres.session; import javax.persistence.EntityManager; import javax.persistence.Query; import org.ajpollenca.compres.entity.Tenda; import org.jboss.seam.Component; import org.jboss.seam.annotations.Name; @Name("tendaSearcherBean") public class TendaSearcherBean { public Tenda findByNom(String nom) { EntityManager entityManager; entityManager = (EntityManager) Component.getInstance("entityManager"); Query query = entityManager.createQuery( "select tenda from org.ajpollenca.compres.entity.Tenda tenda where tenda.nom = :nom"); query.setParameter("nom", nom); Tenda tenda = (Tenda) query.getSingleResult(); return tenda; } }
At least I have learnt many SEAM concepts
Hope this helps
-
3. Re: Call a managed bean function from converter
paucarre Sep 27, 2009 11:13 PM (in response to paucarre)And even better:
package org.ajpollenca.compres.session; import java.io.Serializable; import javax.persistence.EntityManager; import javax.persistence.Query; import org.ajpollenca.compres.entity.Tenda; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Name; @Name("tendaSearcherBean") public class TendaSearcherBean implements TendaSearcher, Serializable { @In EntityManager entityManager; public Tenda findByNom(String nom) { Query query = entityManager.createQuery( "select tenda from org.ajpollenca.compres.entity.Tenda tenda where tenda.nom = :nom"); query.setParameter("nom", nom); Tenda tenda = (Tenda) query.getSingleResult(); return tenda; } }
-
4. Re: Call a managed bean function from converter
phantasmo Sep 28, 2009 5:34 PM (in response to paucarre)I didn't entirely understand your usecase, but it seems to me you should check out <s:convertEntity />. -
5. Re: Call a managed bean function from converter
paucarre Oct 3, 2009 9:29 PM (in response to paucarre)Hi,
convertEntity just transforms an entity instance into an identity object string. This is not useful for most cases because the entity id is usually an integer.
If I use the
nom
attribute as an identifier then I have no way in JPA to automatically generate theid
attribute value. I can not use @GeneratedValue annotation because it must come along with @Id annotation.It is even worse if I drop the
id
attribute from the table and makenom
attribute the identifier because then I can not change thenom
value due to its references.Am I right??