Seam s:convertEntity + glassfish + toplink

.... doesn't go very well together, since Seam wants to access your entityManager, and would want it to be a Hibernate instance...

This is a simple workaround, by replacing s:convertEntity with a custom tag that gives the same result. By remembering the entities displayed to the s:selectItems, using the value of entity.toString() (which is className + hashCode() (which should be unique).).

You might want to tune it maybe not to use hashcode (in case of non uniqueness), and maybe access your entitymanager (toplink) - instead of storing entities in memory.. But in any case this is a good starting point:

package com.petersalomonsen.jsf.persistence.EntityConverter
import java.util.HashMap;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

public class EntityConverter implements Converter {
static HashMap<String,Object> entitiesToRemember = new HashMap<String,Object>();

public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
return entitiesToRemember.get(arg2);
}

public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
entitiesToRemember.put(arg2.toString(), arg2);
return arg2.toString();
}
}


This goes into your faces-config.xml:

<converter>
<converter-id>EntityConverter</converter-id>
<converter-class>com.petersalomonsen.jsf.persistence.EntityConverter</converter-class>
</converter>


And here in your xhtml, s:convertEntity is replaced by the custom converter (<f:converter converterId="EntityConverter" />):

<h:selectOneMenu value="#{myItem}">
<s:selectItems value="#{myItemTypes}" var="mit" label="#{mit.name}" />
<f:converter converterId="EntityConverter" />
<!-- <s:convertEntity /> -->
</h:selectOneMenu>

Comments

Popular posts from this blog

My VNC based development environment with Visual Studio Code running on Ubuntu

Create PDF's in Angular / Typescript

Angular 5 - Ahead of Time build configuration for rollup