0
I have a Primefaces3.5, JSF 2.1 and JPA 2.0 application
I am uploading a file in which it does not persistence in the bank but outputs in the console are ok. I still don’t understand why you’re not committing.
Why doesn’t it work?
Bean code:
public class FileBean implements Serializable {
private static final long serialVersionUID = 1L;
private String diretorioDestino = "c:\\temp\\";
@Inject
private EntityManager manager;
private UploadedFile uploadedFile;
public UploadedFile getFile() {
return uploadedFile;
}
public void setFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
public void handleFileUpload(FileUploadEvent event) throws IOException {
this.uploadedFile = event.getFile();
}
@SuppressWarnings("unchecked")
public void send() throws IOException {
if (getFile() == null) {
FacesUtil.addErrorMessage(FacesUtil.getMensagemI18n("escolher_arquivo"));
} else {
String nomeArquivo = uploadedFile.getFileName().substring(uploadedFile.getFileName().lastIndexOf("\\") + 1);
XStream ler = new XStream(new DomDriver());
ler.alias("lista", List.class);
ler.alias("historico", Historico.class);
ler.alias("produto", Produto.class);
List<Produto> listaProduto = (List<Produto>) ler.fromXML(new InputStreamReader(uploadedFile.getInputstream()));
List<Historico> invertida = new ArrayList<Historico>();
// Collections.reverse(invertida);
// for (Object objeto : invertida) {
// System.out.println(objeto);
for (Iterator<Produto> p = listaProduto.iterator(); p.hasNext();) {
Produto prod = p.next();
System.out.println(prod.getCodigoProduto() == null ? "Erro" : prod.getCodigoProduto());
System.out.println(prod.getDescricao());
for (Iterator<Historico> h = prod.getHistoricos().iterator(); h.hasNext();)
{
Historico hist = h.next();
System.out.println(hist.getMesesHistoricos() == null ? "Errro" :
hist.getMesesHistoricos());
System.out.println(hist.getQuantidade());
// prod.getHistoricos().add(hist);
invertida.add(hist);
}
Collections.reverse(invertida); // lista invertida para calcular
// o alfa
for (Historico hist : invertida) {
System.out.println(hist.getMesesHistoricos() == null ? "Errro" :
hist.getMesesHistoricos());
System.out.println(hist.getQuantidade());
}
// prod.setFatorAmortecimentoExponencial(fatorAmortecimentoExponencial);
manager.persist(prod);// persistir após calcular o alfa
}
//Upload(nomeArquivo, uploadedFile.getInputstream());
FacesUtil.addInfoMessage(FacesUtil.getMensagemI18n("arquivo_processado") + " : " + nomeArquivo);
}
}
}
Code of xhtml:
<h:form enctype="multipart/form-data">
<p:fileUpload id="import" value="#{fileBean.file}" mode="single" label="Enviar"
auto="true" fileUploadListener="#{fileBean.handleFileUpload}"
invalidFileMessage="#{msg.somente_excel}"
allowTypes="/(\.|\/)(xml)$/" />
<p:commandButton id="saveBtn" value="Processar" ajax="false"action="#{fileBean.send}"></p:commandButton>
<p:dialog widgetVar="importaDlg" id="idimportaDlg" height="200" header="Realizando Importação" width="400" modal="true"
closable="false" draggable="false" resizable="false">
<p:panelGrid columns="1" styleClass="semBorda">
<h:outputText value="#{msg.aguarde}" />
<p:graphicImage library="images" name="ajax-loading-bar.png" />
</p:panelGrid>
</p:dialog>
</h:form>
I think some PK or FK error occurs during persistence attempt.
– Reginaldo Rigo
when I do a persistence test with main method it works.
– user2509556