Я новичок в спящем режиме и JPA, я пытаюсь извлечь List
элементов из моей базы данных с помощью EntityManager
query.getResultList()
но ничего не возвращает. Вот мой боб для поиска данных: `EntityManager query.getResultList() возвращает пустой список
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import com.algaworks.financeiro.model.Lancamento;
import com.algaworks.financeiro.util.JpaUtil;
@ManagedBean
@ViewScoped
public class ConsultaLancamentosBean
{
private List<Lancamento> lancamentos;
public void consultar()
{
EntityManager manager = JpaUtil.getEntityManager();
TypedQuery<Lancamento> query = manager.createQuery
(
"from Lancamento", Lancamento.class
);
this.lancamentos = query.getResultList();
manager.close();
}
public List<Lancamento> getLancamentos()
{
return lancamentos;
}
}
Вид для ищущей фасоли:`
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Consulta de lançamentos</title>
</h:head>
<h:body>
<f:metadata>
<f:viewAction action="#{consultaLancamentosBean.consultar}" />
</f:metadata>
<h1>Consulta de lançamentos</h1>
<h:form id="frm">
<h:dataTable value="#{consultaLancamentosBean.lancamentos}"
var="lancamento" border="1" cellspacing="0"
cellpadding="2">
<h:column>
<f:facet name="header">
<h:outputText value="Pessoa"/>
</f:facet>
<h:outputText value="#{lancamento.pessoa.nome}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Descrição"/>
</f:facet>
<h:outputText value="#{lancamento.descricao}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Tipo"/>
</f:facet>
<h:outputText value="#{lancamento.tipo}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Valor"/>
</f:facet>
<h:outputText value="#{lancamento.valor}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Data de vencimento"/>
</f:facet>
<h:outputText value="#{lancamento.dataVencimento}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Data de pagamento"/>
</f:facet>
<h:outputText value="#{lancamento.dataPagamento}"/>
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
getLancamentos()
возвращает пустой список, кто-то знает, почему это происходит? Заранее спасибо
Update: Я проверил, что все в порядке с подключением и все таблицы имеют данные, я изменил боб иметь только один метод: `
public List<Lancamento> getLancamentos()
{
EntityManager manager = JpaUtil.getEntityManager();
TypedQuery<Lancamento> query = manager.createQuery("SELECT lan FROM Lancamento lan", Lancamento.class);
List<Lancamento> lancamentos;
lancamentos = query.getResultList();
return lancamentos;
}
And my new view is:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>The size is:</title>
<h:outputLabel value="#{consultaLancamentosBean.lancamentos}"> </h:outputLabel>
</html>
`
Но теперь, когда я пытаюсь запустить код выводит: HTTP Status 500 - Могли не инициализировать класс com.algaworks.financeiro.util.JpaUtil
Я уже ввезли этот класс
как о вы его отлаживаете? Посмотрите на сгенерированный SQL. Посмотрите на значение переменных в разных местах. Посмотрите, почему вы опустили «SELECT lan» из JPQL. –
Я пропустил «SELECT lan», потому что ebbok сказал мне сделать это, он сказал, что вам не нужно указывать столбцы при работе с JPQL –
, ваша книга неверна. Спецификация JPA достаточно четко говорит о том, что «SELECT {alias}» является обязательным. Только плохая документация говорит о вещах, которые не переносятся –