Sunday, 15 June 2014

jpa - Accessing the EntityManager from a JSF Converter class -



jpa - Accessing the EntityManager from a JSF Converter class -

this question has reply here:

how inject @ejb, @persistencecontext, @inject, @autowired, etc in @facesconverter? 3 answers

i'm trying write converter class convert between employee object , string (for display purposes). end goal have selectone field on form displays list of employee numbers in database.

here's have far:

@facesconverter(value = "employeeconverter") public class employeeconverter implements converter { @override public object getasobject(facescontext context, uicomponent component, string value) { employee tmp = helper.findemployee(em, value); homecoming tmp; } @override public string getasstring(facescontext context, uicomponent component, object value) { employee tmp = (employee) value; homecoming tmp.getemployee_number(); } }

so, issue i'm running helper class (which runs sql queries me - can include if needed) requires pass entitymanager. i've figured out through searching, can't include entitymanager in class because it's outside of "scope" of web stuff.

i'm new using java web, , new using databases java. can explain how can utilize converter way query database , have select box employee numbers in it.

since converter methods containing reference facescontext, can utilize 1 evaluate el-expression homecoming service in question.

if not using dedicated dataservices, utilize "helper bean", nil contain reference entitymanager:

@named public class randomhelperbean{ @persistencecontext(unitname = "yourpersistencecontext") entitymanager em; public entitymanager getem(){ homecoming em; } }

and within converter:

public object getasobject(facescontext context, uicomponent component, string value) { randomhelperbean rhb= context.getcurrentinstance().getapplication().evaluateexpressionget(context, "#{randomhelperbean}", randomhelperbean.class); entitymanager em = rhb.getem(); employee tmp = helper.findemployee(em, value); homecoming tmp; }

pretty much untested, should this.

jsf jpa converter

No comments:

Post a Comment