So I have a basic JSF Datatable, the relavent part is:
<h:dataTable value="#{actorTableBackingBean.allActors}" var="actor">
<h:column headerText="Actor Name" sortBy="#{actor.firstName}">
<h:outputText value="#{actor.firstName}" />
</h:column>
<h:column>
<h:form>
<h:commandButton value="Delete Actor"
action="#{actorTableBackingBean.deleteActor(actor)}"/>
</h:form>
</h:column>
<h:column>
<h:form>
<h:commandButton value="Randomize Actor Name"
action="#{actorTableBackingBean.editActor(actor)}"/>
</h:form>
</h:column>
</h:dataTable>
And this is what ActorTableBackingBean looks like:
@Named
@RequestScoped
public class ActorTableBackingBean implements Serializable {
@Inject
ActorDao actorDao;
private List<Actor> allActors;
public List<Actor> getAllActors() {
return allActors;
}
@PostConstruct
public void fillUp(){
allActors = actorDao.getAllT();
}
public String deleteActor(Actor a){
removeActor(a);
return "/allActors.xhtml";
}
private String removeActor(Actor a){
try{
actorDao.deleteActor(a);
return null;
}catch (Exception e){
return null;
}
}
public String editActor(Actor actor){
actor.setFirstName("SomeRandonName");
actorDao.editActor(actor);
return "/allActors.xhtml";
}
}
And finally ActorDao:
@Stateless
public class ActorDao extends GeneralDao<Actor> implements Serializable {
@Override
protected Class<Actor> getClassType() {
return Actor.class;
}
@Override
public Actor getWithId(int id){
TypedQuery<Actor> typedQuery =
em.createQuery("Select a From Actor a WHERE a.actorId =" + id,Actor.class);
return typedQuery.getSingleResult();
}
public void editActor(Actor a){
em.merge(a);
}
public void deleteActor(Actor a){
em.remove(a);
}
}
So as you can see edit Actor calls em.merge(a) and this works just fine. However em.remove(a) will return:
Caused by: java.lang.IllegalArgumentException: Entity must be managed to call remove: com.tugay.sakkillaa.model.Actor@6ae667f, try merging the detached and try the remove again.
Even if I try:
public void deleteActor(Actor a){
em.merge(a);
em.remove(a);
}
I am still getting the same exception.
So how it works for editing the row, but not for deleting it?
Only way I could make it work was:
public void deleteActor(Actor a){
Actor actorToBeRemoved = getWithId(a.getActorId());
em.remove(actorToBeRemoved);
}
What is it that I am doing wrong, or not able to understand?