Eu dei uma olhada no Exemplo, e estou fazendo desse forma.
DAO
package negocio.entidade;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
@Entity
@Table(name = "anexo")
@NamedQueries({
@NamedQuery(name = "Anexo.findAll", query = "SELECT a FROM Anexo a"),
@NamedQuery(name = "Anexo.findByAnexoId", query = "SELECT a FROM Anexo a WHERE a.anexoId = :anexoId")})
public class Anexo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "anexo_id", nullable = false)
private Integer anexoId;
@Lob
@Column(name = "anexo")
private byte[] anexo;
public Anexo() {
}
public Anexo(Integer anexoId) {
this.anexoId = anexoId;
}
public Integer getAnexoId() {
return anexoId;
}
public void setAnexoId(Integer anexoId) {
this.anexoId = anexoId;
}
public byte[] getAnexo() {
return anexo;
}
public void setAnexo(byte[] anexo) {
this.anexo = anexo;
}
@Override
public int hashCode() {
int hash = 0;
hash += (anexoId != null ? anexoId.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 Anexo)) {
return false;
}
Anexo other = (Anexo) object;
if ((this.anexoId == null && other.anexoId != null) || (this.anexoId != null && !this.anexoId.equals(other.anexoId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "negocio.entidade.Anexo[ anexoId=" + anexoId + " ]";
}
}
Ae que vem minha dúvida , quero jogar para minha tabela ou gravar em uma pasta.
Estou seguindo esse exemplo.
MB
package apresentacao.managedbean;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import negocio.entidade.Anexo;
import org.primefaces.event.FileUploadEvent;
@ManagedBean(name = "anexoMB")
@SessionScoped
public class AnexoMB implements Serializable {
private EntityManagerFactory factory;
private byte[] anexo;
public void handleFileUpload(FileUploadEvent event) throws ServletException {
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
byte[] foto = event.getFile().getContents();
Anexo imagem = new Anexo();
imagem.setAnexo(anexo);
this.factory = Persistence.createEntityManagerFactory(null);
EntityManager entityManager = this.factory.createEntityManager();
entityManager.getTransaction().begin();
try {
entityManager.getTransaction().commit();
} catch (Exception e) {
entityManager.getTransaction().rollback();
throw new ServletException(e);
} finally {
entityManager.close();
}
}
}
XHML
<h:form id="UpImagem" enctype="multipart/form-data">
<p:messages showDetail="true"/>
<p:fileUpload id="upload" update="foto" fileUploadListener="#{AnexoMB.handleFileUpload}" auto="true"/>
</h:form>
WEB.XML
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class> org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>