JPA查询

时间:2015-08-08 21:25:12

标签: java jpa

在Glassfish服务器上,我是使用JPA注入的新手:当我尝试查询数据库(检索公司对象的完整列表)时,我收到以下错误:

2015-08-08T23:21:43.810+0200|Grave: Error Rendering View[/secure/searchCompany.xhtml]
javax.el.ELException: /secure/searchCompany.xhtml @38,59 value="#{searchCompanyBean.companies}": java.lang.NullPointerException
    at com.sun.faces.facelets.el.TagValueExpression.getValue(Unknown Source)
    at javax.faces.component.ComponentStateHelper.eval(Unknown Source)
    at javax.faces.component.ComponentStateHelper.eval(Unknown Source)
    at org.richfaces.component.UISequence.getValue(UISequence.java:179)
    at org.richfaces.component.UISequence.createExtendedDataModel(UISequence.java:113)
    at org.richfaces.component.UIDataTableBase.createExtendedDataModel(UIDataTableBase.java:211)
    at org.richfaces.component.UIDataAdaptor.getExtendedDataModel(UIDataAdaptor.java:467)
    at org.richfaces.component.UIDataAdaptor.setRowKey(UIDataAdaptor.java:280)
    at org.richfaces.renderkit.ExtendedDataTableRenderer.doEncodeChildren(ExtendedDataTableRenderer.java:720)
    at org.richfaces.renderkit.RendererBase.encodeChildren(RendererBase.java:158)
    at javax.faces.component.UIComponentBase.encodeChildren(Unknown Source)
    at javax.faces.component.UIComponent.encodeAll(Unknown Source)
    at org.richfaces.renderkit.RendererBase.renderChildren(RendererBase.java:282)
    at org.richfaces.renderkit.html.PanelRenderer.doEncodeEnd(PanelRenderer.java:183)
    at org.richfaces.renderkit.RendererBase.encodeEnd(RendererBase.java:180)
    at javax.faces.component.UIComponentBase.encodeEnd(Unknown Source)
    at javax.faces.component.UIComponent.encodeAll(Unknown Source)
    at javax.faces.render.Renderer.encodeChildren(Unknown Source)
    at javax.faces.component.UIComponentBase.encodeChildren(Unknown Source)
    at javax.faces.component.UIComponent.encodeAll(Unknown Source)
    at javax.faces.component.UIComponent.encodeAll(Unknown Source)
    at javax.faces.component.UIComponent.encodeAll(Unknown Source)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(Unknown Source)
    at com.sun.faces.application.view.MultiViewHandler.renderView(Unknown Source)
    at javax.faces.application.ViewHandlerWrapper.renderView(Unknown Source)
    at javax.faces.application.ViewHandlerWrapper.renderView(Unknown Source)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(Unknown Source)
    at com.sun.faces.lifecycle.Phase.doPhase(Unknown Source)
    at com.sun.faces.lifecycle.LifecycleImpl.render(Unknown Source)
    at javax.faces.webapp.FacesServlet.service(Unknown Source)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.el.ELException: java.lang.NullPointerException
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:368)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(Unknown Source)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(Unknown Source)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:140)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:204)
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    ... 57 more
Caused by: java.lang.NullPointerException
    at com.sdzee.dao.CompanyDao.searchCompanies(CompanyDao.java:30)
    at com.sdzee.bean.SearchCompanyBean.getCompanies(SearchCompanyBean.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:363)
    ... 63 more

2015-08-08T23:21:43.813+0200|Infos: Exception when handling error trying to reset the response.
java.lang.NullPointerException
    at com.sdzee.dao.CompanyDao.searchCompanies(CompanyDao.java:30)
    at com.sdzee.bean.SearchCompanyBean.getCompanies(SearchCompanyBean.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:363)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(Unknown Source)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(Unknown Source)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:140)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:204)
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(Unknown Source)
    at javax.faces.component.ComponentStateHelper.eval(Unknown Source)
    at javax.faces.component.ComponentStateHelper.eval(Unknown Source)
    at org.richfaces.component.UISequence.getValue(UISequence.java:179)
    at org.richfaces.component.UISequence.createExtendedDataModel(UISequence.java:113)
    at org.richfaces.component.UIDataTableBase.createExtendedDataModel(UIDataTableBase.java:211)
    at org.richfaces.component.UIDataAdaptor.getExtendedDataModel(UIDataAdaptor.java:467)
    at org.richfaces.component.UIDataAdaptor.setRowKey(UIDataAdaptor.java:280)
    at org.richfaces.renderkit.ExtendedDataTableRenderer.doEncodeChildren(ExtendedDataTableRenderer.java:720)
    at org.richfaces.renderkit.RendererBase.encodeChildren(RendererBase.java:158)
    at javax.faces.component.UIComponentBase.encodeChildren(Unknown Source)
    at javax.faces.component.UIComponent.encodeAll(Unknown Source)
    at org.richfaces.renderkit.RendererBase.renderChildren(RendererBase.java:282)
    at org.richfaces.renderkit.html.PanelRenderer.doEncodeEnd(PanelRenderer.java:183)
    at org.richfaces.renderkit.RendererBase.encodeEnd(RendererBase.java:180)
    at javax.faces.component.UIComponentBase.encodeEnd(Unknown Source)
    at javax.faces.component.UIComponent.encodeAll(Unknown Source)
    at javax.faces.render.Renderer.encodeChildren(Unknown Source)
    at javax.faces.component.UIComponentBase.encodeChildren(Unknown Source)
    at javax.faces.component.UIComponent.encodeAll(Unknown Source)
    at javax.faces.component.UIComponent.encodeAll(Unknown Source)
    at javax.faces.component.UIComponent.encodeAll(Unknown Source)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(Unknown Source)
    at com.sun.faces.application.view.MultiViewHandler.renderView(Unknown Source)
    at javax.faces.application.ViewHandlerWrapper.renderView(Unknown Source)
    at javax.faces.application.ViewHandlerWrapper.renderView(Unknown Source)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(Unknown Source)
    at com.sun.faces.lifecycle.Phase.doPhase(Unknown Source)
    at com.sun.faces.lifecycle.LifecycleImpl.render(Unknown Source)
    at javax.faces.webapp.FacesServlet.service(Unknown Source)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:745)

2015-08-08T23:21:43.814+0200|Avertissement: StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
    at com.sdzee.dao.CompanyDao.searchCompanies(CompanyDao.java:30)
    at com.sdzee.bean.SearchCompanyBean.getCompanies(SearchCompanyBean.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:363)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(Unknown Source)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(Unknown Source)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:140)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:204)
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(Unknown Source)
    at javax.faces.component.ComponentStateHelper.eval(Unknown Source)
    at javax.faces.component.ComponentStateHelper.eval(Unknown Source)
    at org.richfaces.component.UISequence.getValue(UISequence.java:179)
    at org.richfaces.component.UISequence.createExtendedDataModel(UISequence.java:113)
    at org.richfaces.component.UIDataTableBase.createExtendedDataModel(UIDataTableBase.java:211)
    at org.richfaces.component.UIDataAdaptor.getExtendedDataModel(UIDataAdaptor.java:467)
    at org.richfaces.component.UIDataAdaptor.setRowKey(UIDataAdaptor.java:280)
    at org.richfaces.renderkit.ExtendedDataTableRenderer.doEncodeChildren(ExtendedDataTableRenderer.java:720)
    at org.richfaces.renderkit.RendererBase.encodeChildren(RendererBase.java:158)
    at javax.faces.component.UIComponentBase.encodeChildren(Unknown Source)
    at javax.faces.component.UIComponent.encodeAll(Unknown Source)
    at org.richfaces.renderkit.RendererBase.renderChildren(RendererBase.java:282)
    at org.richfaces.renderkit.html.PanelRenderer.doEncodeEnd(PanelRenderer.java:183)
    at org.richfaces.renderkit.RendererBase.encodeEnd(RendererBase.java:180)
    at javax.faces.component.UIComponentBase.encodeEnd(Unknown Source)
    at javax.faces.component.UIComponent.encodeAll(Unknown Source)
    at javax.faces.render.Renderer.encodeChildren(Unknown Source)
    at javax.faces.component.UIComponentBase.encodeChildren(Unknown Source)
    at javax.faces.component.UIComponent.encodeAll(Unknown Source)
    at javax.faces.component.UIComponent.encodeAll(Unknown Source)
    at javax.faces.component.UIComponent.encodeAll(Unknown Source)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(Unknown Source)
    at com.sun.faces.application.view.MultiViewHandler.renderView(Unknown Source)
    at javax.faces.application.ViewHandlerWrapper.renderView(Unknown Source)
    at javax.faces.application.ViewHandlerWrapper.renderView(Unknown Source)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(Unknown Source)
    at com.sun.faces.lifecycle.Phase.doPhase(Unknown Source)
    at com.sun.faces.lifecycle.LifecycleImpl.render(Unknown Source)
    at javax.faces.webapp.FacesServlet.service(Unknown Source)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:745)

在CompanyDao.java文件中,第30行是:

Query requete = em.createQuery( JPQL_SEARCH_COMPANIES );

CompanyDao.java文件包含:

@Stateless
public class CompanyDao {
    private static final String JPQL_SEARCH_COMPANIES = "SELECT c FROM Company c where c.name like :companyName and c.city like :companyCity";
    private static final String PARAM_COMPANY_NAME = "companyName";
    private static final String PARAM_COMPANY_CITY = "companyCity";

    // Injection of the manager, which manages the connexion with the Database
    @PersistenceContext( unitName = "bdd_sdzee_PU" )
    private EntityManager em;

    // Search all the companies

    public List<Company> searchCompanies(String companyName, String companyCity) throws DAOException {
        List<Company> listCompanies = null;
        Query requete = em.createQuery( JPQL_SEARCH_COMPANIES );
        requete.setParameter( PARAM_COMPANY_NAME, "%"+companyName+"%");
        requete.setParameter( PARAM_COMPANY_CITY, "%"+companyCity+"%");
        try {
            listCompanies = requete.getResultList();
        } catch ( NoResultException e ) {
            return null;
        } catch ( Exception e ) {
            throw new DAOException( e );
        }
        return listCompanies;
    }   
}

searchCompany.xhtml文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition template="/resources/template/layout.xhtml"
                    xmlns="http://www.w3.org/1999/xhtml"
                    xmlns:f="http://java.sun.com/jsf/core"
                    xmlns:h="http://java.sun.com/jsf/html"
                    xmlns:ui="http://java.sun.com/jsf/facelets"
                    xmlns:p="http://primefaces.org/ui"
                    xmlns:a4j="http://richfaces.org/a4j"
                    xmlns:rich="http://richfaces.org/rich"
                    xmlns:fn="http://java.sun.com/jsp/jstl/functions">
    <ui:define name="content">
        <style type="text/css">
            .rf-edt-r-sel{
                 font-weight: normal;
                 background-color: rgb(95,180,251);
            }
        </style>
        <h:form>
            <rich:panel style="width:1024px">
                <h:panelGrid columns="5" border="0" cellpadding="0" styleClass="panelGridCenter" columnClasses="columnDefaultClass, columnDefaultClass, column200Class, columnDefaultClass, columnDefaultClass">
                    <h:outputText value="#{msg['company']} : " for="companyName" style="text-align:right"/>
                    <rich:autocomplete value="#{searchCompanyBean.companyName}" id="companyName" mode="lazyClient" autocompleteList="#{searchCompanyBean.listOfCompanyNames}" style="vertical-align: middle"/>
                    <h:outputText />
                    <h:outputText value="#{msg['city']} : " for="companyCity" style="text-align:right"/>
                    <rich:autocomplete value="#{searchCompanyBean.companyCity}" id="companyCity" mode="lazyClient" autocompleteList="#{searchCompanyBean.listOfCompanyCities}" style="vertical-align: middle" />
                </h:panelGrid>      
                <h:panelGroup style="display:block; text-align:center">
                    <a4j:commandButton value="#{msg['search']}" action="#{searchCompanyBean.searchCompanies}" render="@form" execute="@form" />
                </h:panelGroup>     
            </rich:panel>
            <rich:panel style="width:1024px">
                <rich:extendedDataTable id="table" 
                        value="#{searchCompanyBean.companies}" 
                        var="company" frozenColumns="1"
                        sortPriority="#{sortCompanyBean.sortPriorities}"
                        selectionMode="multiple"
                        style="height:500px; width:1000px;"
                        selection="#{searchCompanyBean.selectionCompanies}">
                    <a4j:ajax execute="@form" 
                        event="selectionchange" 
                        listener="#{searchCompanyBean.selectionListener}"
                        render="details"/>
                    <rich:column width="150px"
                        sortBy="#{company.name}"
                        sortOrder="#{sortCompanyBean.sortsOrders['name']}"
                        sortType="custom"
                        headerClass="headerClass1">
                        <f:facet name="header">
                            <h:panelGrid columns="2">
                                <a4j:commandLink execute="@this" value="#{msg['name']}" render="table"
                                                 action="#{sortCompanyBean.sort}">
                                    <f:param name="sortProperty" value="name"/>
                                </a4j:commandLink>
                                <h:graphicImage value="/resources/default/images/icons/arrow_up.png"
                                                rendered="#{sortCompanyBean.sortsOrders['name']=='descending'}"/>
                                <h:graphicImage value="/resources/default/images/icons/arrow_down.png"
                                                rendered="#{sortCompanyBean.sortsOrders['name']=='ascending'}"/>
                            </h:panelGrid>
                        </f:facet>                  
                        #{company.name}
                    </rich:column>
                    <rich:column width="500px"
                        sortBy="#{company.comment}"
                        sortOrder="#{sortCompanyBean.sortsOrders['comment']}"
                        sortType="custom"
                        headerClass="headerClass1">
                        <f:facet name="header">
                            <h:panelGrid columns="2">
                                <a4j:commandLink execute="@this" value="#{msg['comment']}" render="table"
                                                 action="#{sortCompanyBean.sort}">
                                    <f:param name="sortProperty" value="comment"/>
                                </a4j:commandLink>
                                <h:graphicImage value="/resources/default/images/icons/arrow_up.png"
                                                rendered="#{sortCompanyBean.sortsOrders['comment']=='descending'}"/>
                                <h:graphicImage value="/resources/default/images/icons/arrow_down.png"
                                                rendered="#{sortCompanyBean.sortsOrders['comment']=='ascending'}"/>
                            </h:panelGrid>
                        </f:facet>                  
                        #{company.comment}
                    </rich:column>
                </rich:extendedDataTable>
                <rich:contextMenu target="table" mode="ajax">
                    <rich:menuItem
                                    label="#{msg['edit']}"
                                    actionListener="#{maPopup.ouvrir}"
                                    mode="ajax"
                                    icon="/resources/default/images/icons/edit_detail.png"/>
                </rich:contextMenu>
            </rich:panel>
            <rich:messages />
        </h:form>    
    </ui:define>
</ui:composition>

SearchCompanyBean是:

@ManagedBean
@RequestScoped

public class SearchCompanyBean implements Serializable {

    private static final long serialVersionUID = 1L;
    // Injection de notre EJB (Session Bean Stateless)
    @EJB
    private @Inject CompanyService das;

    private CompanyDao companyDao = new CompanyDao();
    private Company company;
    private List<Company> companies;
    private List<String> listOfCompanyNames = null;
    private List<String> listOfCompanyCities = null;
    private String companyName;
    private String companyCity;
    private String selectionMode = "multiple";
    private Collection<Object> selection = null;

    private List<Company> selectionCompanies = null;
    private Company selectionCompany = new Company();

    public void selectionListener(AjaxBehaviorEvent event) {
        UIExtendedDataTable dataTable = (UIExtendedDataTable)event.getComponent();
        selectionCompanies = new ArrayList<Company>();
        Object originalKey = dataTable.getRowKey();
        for (Object selectionKey : selection) {
            dataTable.setRowKey(selectionKey);
//            if (dataTable.isRowAvailable()) {
                selectionCompanies.add((Company)dataTable.getRowData());
//            }
        }
        dataTable.setRowKey(originalKey);
    }

    public Collection<Object> getSelection() {
        return selection;
    }

    public void setSelection(Collection<Object> selection) {
        this.selection = selection;
    }

    public Company getSelectionCompany() {
        if (selectionCompanies == null || selectionCompanies.isEmpty()) {
            return null;
        }
        return selectionCompanies.get(0);
    }

    public void setSelectionCompany(Company company) {
        this.company = company;
    }

    public List<Company> getSelectionCompanies() {
        return selectionCompanies;
    }

    public void setSelectionCompanies(List<Company> selectionCompanies) {
        this.selectionCompanies = selectionCompanies;
    }

    public void searchCompanies(){
        companies = companyDao.searchCompanies(companyName, companyCity);
    }

    public Company getCompany(){
        return company;
    }

    public List<Company> getCompanies(){
        if (companies==null){
            companies = companyDao.searchCompanies("",""); // By default, when displaying the page, all the Companies are listed
        }
        return companies;
    }    
}

persistence.xml文件是:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="bdd_sdzee_PU" transaction-type="JTA">
        <jta-data-source>jdbc/mysql_resource</jta-data-source>
        <class>com.sdzee.entities.Utilisateur</class>       
        <class>com.sdzee.entities.Company</class>       
        <properties>
            <property name="eclipselink.ddl-generation" value="create-tables"/>
            <property name="eclipselink.logging.level.sql" value="FINEST"/>
            <property name="eclipselink.logging.parameters" value="true"/>
        </properties> 
     </persistence-unit>
</persistence>

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

如果您的帖子包含完整的CompanyDao类,则会出现以下错误

java.lang.NullPointerException
    at com.sdzee.dao.CompanyDao.searchCompanies(CompanyDao.java:30)

是因为您在忘记初始化时使用em变量。

Query requete = em.createQuery( JPQL_SEARCH_COMPANIES ); // Error --> "em" still null

答案 1 :(得分:0)

在SearchCompanyBean中,您可以创建自己的CompanyDao对象:

private CompanyDao companyDao = new CompanyDao();

但是,在这种情况下,不会发生字段注入,因为容器不知道它们(如PersistenceContext)。

因此,为了启用注入,您必须在SearchCompanyBean中注入CompanyDao,以使容器处理对象和依赖项的生命周期(就像您使用CompanyService一样)。