You cannot (usually) make an EJB/JPB Domain Model Object be a JSF backing bean because - while it's technically possible - they operate in different ways.

More often you'd use a backing bean as a container that fetches (and updates) the actual EJB, which you can present as a property of the backing bean.

Note that attempting to load objects from a database in a backing bean's constructor also isn't a good idea. For one thing, your database connection is best not hard-wired into the bean, and you can't inject the connection (or entityManager) at construction time. It's better to either do the object fetches in a @PostConstruct or in a separate pre-display method.

Customer surveys are for companies who didn't pay proper attention to begin with.

Johan Rignas
Ranch Hand

Joined: Jul 17, 2011
Posts: 30

posted Jul 18, 2011 16:31:12

0

Ok. Thanks. But what about something like this:

ModelBean:

BackingBean:

If I just could get the value of a textfield with a given id it seems all will be solved. Any idea how to do this?
Something like this:

Johan Rignas
Ranch Hand

Joined: Jul 17, 2011
Posts: 30

posted Jul 19, 2011 03:20:18

0

Think I solved it with just a conversion inside my previous updateClicked():

Johan Rignas
Ranch Hand

Joined: Jul 17, 2011
Posts: 30

posted Jul 19, 2011 15:25:26

0

Showed up this gives me the old value of the textField and not the new one.

Now I wounder: is there any hints on how I by a textfield can update a given row in a database with Beans?

WCS(worst case scenario) is that I have to go: jquery -> servlet -> database for an update and leave all beans out of it

In addition to not being able to use an Entity EJB as a backing bean, injecting an EJB into a backing bean doesn't usually work well for the same basic reason: you usually want the same EJB for every use of the backing bean, and injection requires you to specify a specific object at compile-time. You can, however, inject a DAO or Session EJB into the backing bean and use it to retrieve the instance that you want to work on. Once you do that, then the page can reference the bean and its properties using EL.

Please note that JSF is all about doing things for you. There's usually something wrong when your action processor method starts mucking around with the FacesContext, since the bean properties are automatically updated by JSF itself before the action method is invoked. And if one or more items fail validation, the bean won't be updated and the action method won't get invoked, so you can always be sure that only data that passes validation goes to the action processor.

Here's a minimalist sketch of a typical EJB/JSF interaction. It's not intended to be usable "as-is", but it does illustrate the major points. It's designed to display a form where you enter a keyvalue as text, then retrieve the EJB for that key, then redisplay the form with a few ejb properties:

Now for the View definition:

Johan Rignas
Ranch Hand

Joined: Jul 17, 2011
Posts: 30

posted Jul 20, 2011 17:24:25

0

Thanks!! Tried to do the same thing as you and it worked!

But I would like the values to show directly. Becouse of this I made one extra view page. This is how it looks:

That's what I do as well. For CRUD operations, I usually have 2 methods used to prep a backing bean for edit and display and name them "beginAdd" and "beginEdit". One sets up a blank EJB to be added, the other fetches an existing EJB.

Because "beginEdit" normally takes a primary key (after all, you have to know which EJB to edit!), I usually inject the backing bean into a setup bean which implements the initial choice to create or edit and allows input of the key value. Then the setup bean's action processor invokes the beginEdit(key) method in the injected editor backing bean.