Hibernate查询中的空指针异常

时间:2014-04-03 08:16:23

标签: java hibernate jsf-2 primefaces spring-security

我正在尝试执行hibernate查询我正在使用 JSF 2 Primefaces,Spring security 3和Hibernate 4 。我已经检查过hibernate与数据库进行通信,因为它可以在你启动程序时自动创建表。

我已尝试获取当前会话并在执行查询时创建会话但我不知道这是否是原因。我搜索并尝试了所有可能的解决方案,但我还没找到。 我总是收到以下错误:

错误

  

com.sun.faces.mgbean.ManagedBeanCreationException:无法在com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:193)实例化类:core.bean.HolasBean       在com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102)       at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)       at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)       at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)       在com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116).....

     

引起:java.lang.NullPointerException       在core.bean.HolasBean。(HolasBean.java:22)

第22行是查询

Query query = sessionFactory.getCurrentSession().createQuery(
            "from Holas");

尽可能用最简单的例子进行测试:

HolasBean

    @Named
@Scope("session")
public class HolasBean{


HolasBo holasBo;
HolasDao holasDao;

public void setUserBo(HolasBo holasBo) {
    this.holasBo = holasBo;
}

public String printMsgFromSpring() {          //This works

    return "HOla";

}
public String getMessage(){                  //Null pointer exception

    return holasDao.getMessageHiber();
}

HolasDao

@Repository
public class HolasDaoImpl implements HolasDao {

    @Autowired
     private SessionFactory sessionFactory;

    public String getMessageHiber() {
        List<Holas> holas = new ArrayList<Holas>();
        holas = sessionFactory.getCurrentSession().createQuery("from Holas").list();

        return holas.get(0).toString();
 }
}

模型:Holas.java

@Entity
@Table(name= "gm_holas")
public class Holas {

@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "nombre")
private String nombre;


//getters and setters
}

XHTML

<h:body>

        <h1>JSF 2.0 + Spring Example</h1>

    #{holasBean.printMsgFromSpring()}  //This is working
    #{holasBean.getMessage()}          //Null Pointer Exception

    </h:body>

* 编辑*

谢尔盖,当我发表评论时,我在网站上收到此错误:

  

物业&#39; holas&#39;在core.bean.HolasBean类型中找不到。

配置文件,以防您需要:

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     id="WebApp_ID"
     version="2.5">

<display-name>Project Name</display-name>

<!-- Spring Context Configuration' s Path definition -->

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
       /WEB-INF/applicationContext.xml
    </param-value>
</context-param>

<!-- SPRING SECURITY -->

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Spring listener. It is registered to Servlet Container -->

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
<listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
</listener>

<!-- Project Stage Level -->

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>

<!-- Welcome Page -->

<welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
</welcome-file-list>

<!-- JSF Servlet  -->

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Mapping for the http requests. -->
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

</web-app>

aplicationConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http:...

<!-- SPRING SECURITY -->    

<security:http use-expressions="true">
    <security:intercept-url pattern="/secured/*" access="isAuthenticated()"/>
    <security:form-login login-page="/index.xhtml" authentication-failure-url="/malvamos" default-target-url="/aleadormir"/>
    <security:logout logout-success-url="/index"/>
</security:http>

<security:authentication-manager>
    <security:authentication-provider user-service-ref="userServiceImpl" />
</security:authentication-manager>


<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

<mvc:annotation-driven />
<task:annotation-driven />
<context:annotation-config/>
<context:component-scan base-package="core" />



<!-- TRANSACTION MANAGERS -->

<tx:annotation-driven transaction-manager="txManager"/>

<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>



<!-- DATA SOURCES -->

<bean   id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/connection.properties" />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${cnn.driver}" />
    <property name="url" value="${cnn.url}" />
    <property name="username" value="${cnn.username}" />
    <property name="password" value="${cnn.password}" />
</bean>




<!-- SESSION FACTORIES -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource"><ref bean="dataSource"/></property>
    <property name="configLocation">
        <value>/WEB-INF/hibernate-config.xml</value>
    </property>
    <property name="annotatedClasses">
        <list>
            <value>core.model.Permission</value>
            <value>core.model.Role</value>
            <value>core.model.UserEntity</value>
            <value>core.model.Holas</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                ${cnn.dialect}
            </prop>
        </props>
    </property>
</bean>

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>web</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name> Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
    <hibernate.version>4.1.7.Final</hibernate.version>
    <spring.version>3.1.1.RELEASE</spring.version>
</properties>

<dependencies>

    <!-- Spring 3 dependencies -->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- SPRING SECURITY -->

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- JSF dependencies -->

    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.1.6</version>
    </dependency>

    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.1.6</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- Primefaces dependency -->

    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>4.0</version>
    </dependency>

    <!-- Hibernate dependencies -->

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.1.7.Final</version>
    </dependency>

    <!-- MySQL Java Connector dependency -->

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.17</version>
    </dependency>

    <!-- c3p0 dependency -->

    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
    </dependency>

</dependencies>

<repositories>
    <repository>
        <id>prime-repo</id>
        <name>PrimeFaces Maven Repository</name>
        <url>http://repository.primefaces.org</url>
        <layout>default</layout>
    </repository>
</repositories>
<build>
    <finalName>gesmo</finalName>
</build>
</project>

欢迎任何帮助或评论!!

0 个答案:

没有答案