I'm starting with EJB. I can insert and list from a database but I can not delete records.
My entity:
Code:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Client {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
My ManagedBean:
Code:
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.naming.InitialContext;
import javax.naming.NamingException;
@ManagedBean
@ViewScoped
public class ClienteMB {
private Client client = new Client();
private List<Client> lstClient = new ArrayList<Client>();
public String includeClient(){
InitialContext ini;
try {
ini = new InitialContext();
ClientBean clientBean = (ClientBean)ini.lookup("java:module/ClientBean");
clientBean.save(client);
client = new Client();
lstClient = clientBean.searchClient();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String deleteClient(){
InitialContext ini;
try {
ini = new InitialContext();
ClientBean clientBean = (ClientBean)ini.lookup("java:module/ClientBean");
clientBean.delete(client);
client = new Client();
lstClient = clientBean.searchClient();
} catch (NamingException e) {
e.printStackTrace();
}
return null;
}
public Cliente getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
public List<Client> getLstClient() {
return lstClient;
}
public void setLstClient(List<Client> lstClient) {
this.lstClient = lstClient;
}
}
My EJB:
Code:
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class ClientBean {
@PersistenceContext
EntityManager em;
public void save(Client client) {
em.persist(client);
}
public void delete(Client client) {
em.remove(client);
}
public List<Client> searchClient(){
return em.createQuery("FROM Client").getResultList();
}
}
My xhtml page:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head><title>App</title></h:head>
<h:body>
<h:form>
<h:outputLabel value="Name: " for="name" />
<h:inputText id="name" value="#{clientMB.client.name}"
required="true" requiredMessage="NEED"
maxlength="200"/>
<h:commandButton value="Save" action="#{clientMB.includeClient()}" />
</h:form>
<h:dataTable id="tabela" value="#{clientMB.lstClient}" var="item">
<h:column>
<f:facet name="header">
<h:outputText value="ID" />
</f:facet>
<h:outputText value="#{item.id}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{item.name}"/>
</h:column>
<h:column>
<h:commandButton value="Delete"
action="#{clientMB.deleteClient()}" />
</h:column>
</h:dataTable>
</h:body>
</html>
The delete button does not give error but does not delete from the database. Why?