Ola Amigos
Estou fazendo um trabalho com essas ferramentas acima, Fiz o mapeamento das tabelas, criei os arquivos de persistencia etc... a minha duvida eh oq fazer agora, oq devo inserir nesses arquivos??? como faco o CRUD funcionar de fato? Segue um dos arquivos criados no mapeamento. desde ja agradeco a ajuda!!!!
[code]
package tcc;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
-
@author Fabio
*/
@Entity
@Table(name = “usuario”)
@XmlRootElement
@NamedQueries({
@NamedQuery(name = “Usuario.findAll”, query = “SELECT u FROM Usuario u”),
@NamedQuery(name = “Usuario.findByIdUsuario”, query = “SELECT u FROM Usuario u WHERE u.idUsuario = :idUsuario”),
@NamedQuery(name = “Usuario.findByTipo”, query = “SELECT u FROM Usuario u WHERE u.tipo = :tipo”),
@NamedQuery(name = “Usuario.findByPassword”, query = “SELECT u FROM Usuario u WHERE u.password = :password”),
@NamedQuery(name = “Usuario.findByNome”, query = “SELECT u FROM Usuario u WHERE u.nome = :nome”)})
public class Usuario implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@NotNull
@Column(name = “idUsuario”)
private Integer idUsuario;
@Basic(optional = false)
@NotNull
@Column(name = “Tipo”)
private int tipo;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = “Password”)
private String password;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = “Nome”)
private String nome;public Usuario() {
}public Usuario(Integer idUsuario) {
this.idUsuario = idUsuario;
}public Usuario(Integer idUsuario, int tipo, String password, String nome) {
this.idUsuario = idUsuario;
this.tipo = tipo;
this.password = password;
this.nome = nome;
}public Integer getIdUsuario() {
return idUsuario;
}public void setIdUsuario(Integer idUsuario) {
this.idUsuario = idUsuario;
}public int getTipo() {
return tipo;
}public void setTipo(int tipo) {
this.tipo = tipo;
}public String getPassword() {
return password;
}public void setPassword(String password) {
this.password = password;
}public String getNome() {
return nome;
}public void setNome(String nome) {
this.nome = nome;
}@Override
public int hashCode() {
int hash = 0;
hash += (idUsuario != null ? idUsuario.hashCode() : 0);
return hash;
}@Override
public boolean equals(Object object) {
// TODO: Warning - this method won’t work in the case the id fields are not set
if (!(object instanceof Usuario)) {
return false;
}
Usuario other = (Usuario) object;
if ((this.idUsuario == null && other.idUsuario != null) || (this.idUsuario != null && !this.idUsuario.equals(other.idUsuario))) {
return false;
}
return true;
}@Override
public String toString() {
return “tcc.Usuario[ idUsuario=” + idUsuario + " ]";
}
}
[/code]
package tcc;
import tcc.Usuario;
import tcc.util.JsfUtil;
import tcc.util.PaginationHelper;
import tcc.UsuarioFacade;
import java.io.Serializable;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
@ManagedBean(name = "usuarioController")
@SessionScoped
public class UsuarioController implements Serializable {
private Usuario current;
private DataModel items = null;
@EJB
private tcc.UsuarioFacade ejbFacade;
private PaginationHelper pagination;
private int selectedItemIndex;
public UsuarioController() {
}
public Usuario getSelected() {
if (current == null) {
current = new Usuario();
selectedItemIndex = -1;
}
return current;
}
private UsuarioFacade getFacade() {
return ejbFacade;
}
public PaginationHelper getPagination() {
if (pagination == null) {
pagination = new PaginationHelper(10) {
@Override
public int getItemsCount() {
return getFacade().count();
}
@Override
public DataModel createPageDataModel() {
return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
}
};
}
return pagination;
}
public String prepareList() {
recreateModel();
return "List";
}
public String prepareView() {
current = (Usuario) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
return "View";
}
public String prepareCreate() {
current = new Usuario();
selectedItemIndex = -1;
return "Create";
}
public String create() {
try {
getFacade().create(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("UsuarioCreated"));
return prepareCreate();
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
public String prepareEdit() {
current = (Usuario) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
return "Edit";
}
public String update() {
try {
getFacade().edit(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("UsuarioUpdated"));
return "View";
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
public String destroy() {
current = (Usuario) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
performDestroy();
recreateModel();
return "List";
}
public String destroyAndView() {
performDestroy();
recreateModel();
updateCurrentItem();
if (selectedItemIndex >= 0) {
return "View";
} else {
// all items were removed - go back to list
recreateModel();
return "List";
}
}
private void performDestroy() {
try {
getFacade().remove(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("UsuarioDeleted"));
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
}
}
private void updateCurrentItem() {
int count = getFacade().count();
if (selectedItemIndex >= count) {
// selected index cannot be bigger than number of items:
selectedItemIndex = count - 1;
// go to previous page if last page disappeared:
if (pagination.getPageFirstItem() >= count) {
pagination.previousPage();
}
}
if (selectedItemIndex >= 0) {
current = getFacade().findRange(new int[]{selectedItemIndex, selectedItemIndex + 1}).get(0);
}
}
public DataModel getItems() {
if (items == null) {
items = getPagination().createPageDataModel();
}
return items;
}
private void recreateModel() {
items = null;
}
public String next() {
getPagination().nextPage();
recreateModel();
return "List";
}
public String previous() {
getPagination().previousPage();
recreateModel();
return "List";
}
public SelectItem[] getItemsAvailableSelectMany() {
return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
}
public SelectItem[] getItemsAvailableSelectOne() {
return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
}
@FacesConverter(forClass = Usuario.class)
public static class UsuarioControllerConverter implements Converter {
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
UsuarioController controller = (UsuarioController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "usuarioController");
return controller.ejbFacade.find(getKey(value));
}
java.lang.Integer getKey(String value) {
java.lang.Integer key;
key = Integer.valueOf(value);
return key;
}
String getStringKey(java.lang.Integer value) {
StringBuffer sb = new StringBuffer();
sb.append(value);
return sb.toString();
}
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof Usuario) {
Usuario o = (Usuario) object;
return getStringKey(o.getIdUsuario());
} else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + UsuarioController.class.getName());
}
}
}
}
package tcc;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
/**
*
* @author Fabio
*/
@Stateless
public class UsuarioFacade extends AbstractFacade<Usuario> {
@PersistenceContext(unitName = "TCC_FinalPU")
private EntityManager em;
protected EntityManager getEntityManager() {
return em;
}
public UsuarioFacade() {
super(Usuario.class);
}
}