insert, update, delete using same page and input fields
kleinerroemer Jun 6, 2008 11:19 AMhey guys!
This is probably very easy, but I had a lot of troubles with a page which contains input fields for persons, and a list of all persons underneath it.
I would like to use these fields to either:
generate a new person if no person is selected in the list below or,
edit the selected person,
or
delete the selected person.
I ran into a lot of problems with my solution which looks like:
Bean:
@DataModel List<Athlet> athlets; @DataModelSelection Athlet athlet; @In (create = true) Athlet mathlet; public void delete() { if( !isSelected() ) return; logger.info( "removing athlet" + athlet ); athletdao.deleteAthlet( athlet ); loadAthletes(); FacesMessages.instance().add( "Athlet " + athlet + "deleted" ); athlet = null; } public void edit() { if( !isSelected() ) return; mathlet.setPending( athlet.isPending() ); athlet = mathlet; logger.info( "editing athlet" + athlet ); athletdao.editAthlet( athlet ); loadAthletes(); FacesMessages.instance().add( "Athlet " + athlet + "edited" ); } public void store() { AthleticaChangeHelper.changeUpdateInfo( mathlet ); mathlet.setPending(true); logger.info( "adding athlet" + mathlet ); athletdao.addAthlet( mathlet ); loadAthletes(); FacesMessages.instance().add( "Athlet " + mathlet + "added" ); mathlet = null; } public void clear() { logger.info("athlet cleared"); athlet = null; } public Athlet getSelection() { return athlet; } @Factory (value="athlets") public void loadAthletes() { logger.info("loading athletes"); athlets = complexathletdao.findAthlet( searchstring ); } private boolean isSelected() { return athlet != null; } public void setSelection() { mathlet = athlet; }
mathlet has event scope, and is basically used to get the data from the fields to the bean.
The xhtml is straight forward... the input fields are attached to mathlet, the corresponding buttons edit, delete, insert only get displayed if an athlet is (not) selected. Clear is used to reset the fields to blank (which is also not working)
My problems are that:
If I click the delete button I get an:
javax.ejb.EJBTransactionRolledbackException: Removing a detached instance
Exception
Editing also doesn't work that well, since I allways get a validation exception.
The only thing that works perfekt is inserting a new athlet.
So I would like to know if I'm heading the right way with this solution, and am just getting something wrong, or if this is just a bad way to do this kind of stuff...?
I've spend a lot of time with this issue, so any help is appreciated!
Greets