簡體   English   中英

Spring和Hibernate的嵌套異常是java.lang.ArrayIndexOutOfBoundsException:0],其根本原因是

[英]Spring and Hibernate nested exception is java.lang.ArrayIndexOutOfBoundsException: 0] with root cause

我正在使用springhibernate開發Web應用程序。 在這條線

Query query = currentSession.createQuery("from com.test.webcustomer.entity.Customer",Customer.class);

它得到一個錯誤,稱為:

嚴重:路徑為[/ webcustomer]的Servlet [dispatcher]的Servlet.service()拋出異常[請求處理失敗; 嵌套的異常是java.lang.ArrayIndexOutOfBoundsException:0],根本原因是java.lang.ArrayIndexOutOfBoundsException:0

這是我的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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>webcustomer2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>webcustomer2</name>


  <dependencies>
   <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/taglibs/standard -->
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    </dependency>


    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>


    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>


    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>


    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>




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

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.3.1.Final</version>
    </dependency>


    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.3.1.Final</version>
    </dependency>





    <!-- https://mvnrepository.com/artifact/ojdbc/ojdbc -->

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.3</version>
    </dependency>





    <dependency>
        <groupId>com.google.code.maven-play-plugin.com.mchange</groupId>
        <artifactId>c3p0-oracle-thin-extras</artifactId>
        <version>0.9.5</version>
    </dependency>

    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>mchange-commons-java</artifactId>
        <version>0.2.11</version>
    </dependency>

    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.2</version>
    </dependency>
    <!-- 
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.10</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.10</version>
        <scope>test</scope>
    </dependency>
     -->


    <dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version>
    </dependency>

  </dependencies>


<repositories>
    <repository>
    <id>codelds</id>
    <url>https://code.lds.org/nexus/content/groups/main-repo</url>
    </repository>
</repositories>

</project>

這是applicationConfiguration.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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx.xsd">

        <!-- Step 3: Add support for component scanning -->
        <context:component-scan base-package="com.test.webcustomer" />

        <!-- Step 4: Add support for conversion, formatting and validation support -->
        <mvc:annotation-driven/>

        <!-- Step 5: Define Spring MVC view resolver -->
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/view/" />
            <property name="suffix" value=".jsp" />
        </bean>

         <!-- Step 1: Define Database DataSource / connection pool -->
        <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
              destroy-method="close">
            <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
            <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe" />
            <property name="user" value="malan" />
            <property name="password" value="malan" /> 

            <!-- these are connection pool properties for C3P0 -->
            <property name="initialPoolSize" value="5"/>
            <property name="minPoolSize" value="5" />
            <property name="maxPoolSize" value="20" />
            <property name="maxIdleTime" value="30000" />
        </bean>  


         <!-- Step 2: Setup Hibernate session factory -->
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="dataSource" ref="myDataSource" />
            <property name="packagesToScan" value="com.malan.webcustomer.entity" />
            <property name="hibernateProperties">
               <props>
                  <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
                  <prop key="hibernate.show_sql">true</prop>
               </props>
            </property>


       </bean>  

       <!-- Step 3: Setup Hibernate transaction manager -->
        <bean id="myTransactionManager"
                class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>

        <!-- Step 4: Enable configuration of transactional behavior based on annotations -->
        <tx:annotation-driven transaction-manager="myTransactionManager" />

        <!-- Add support for reading web resources: css, images, js, etc ... -->
        <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>  

    </beans>

這是休眠配置xml。

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- JDBC Database connection settings -->
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">malan</property>
        <property name="connection.password">malan</property>

        <!-- JDBC connection pool settings ... using built-in test pool -->
        <property name="connection.pool_size">1</property>

        <!-- Select our SQL dialect -->
        <property name="dialect">org.hibernate.dialect.OracleDialect</property>

        <!-- Echo the SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Set the current session context -->
        <property name="current_session_context_class">thread</property>

    </session-factory>

</hibernate-configuration>

這是Customer.class

package com.test.webcustomer.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name="customer")
public class Customer {

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

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

    public Customer() {

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "Customer [id=" + id + ", firstName=" + firstName
                + ", lastName=" + lastName + "]";
    }




}

這是CustomerDOA類別

package com.test.webcustomer.doa;

import java.util.List;

import com.test.webcustomer.entity.Customer;

public interface CustomerDAO {

    public List<Customer> getCustomers();

}

這是CustomerDOAImpl類

package com.test.webcustomer.doa;

import java.util.*;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.test.webcustomer.entity.Customer;

@Repository
public class CustomerDAOImpl implements CustomerDAO {

    // need to inject the session factory
    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    public List<Customer> getCustomers() {
        System.out.println("In get Customer Method");
        // get the current hibernate session
        Session currentSession = sessionFactory.getCurrentSession();

        System.out.println(currentSession.getProperties());
        // create a query
        Query query = currentSession.createQuery("from com.test.webcustomer.entity.Customer",Customer.class);

        List<Customer>  customerList = query.getResultList();

        return customerList;
    }

}

我需要從客戶表中檢索數據。 客戶表中有2條記錄。 在這個發展中什么雲將成為問題。 我正在為此開發使用Oracle XE數據庫。 請為此提出一些解決方案。

如果問題在於執行HQL,請嘗試使用以下代碼

 Query query = currentSession.createQuery("from Customer");

您需要提供完整的堆棧跟蹤

[編輯]您也可以嘗試使用

Query query = currentSession.createSQLQuery("select *from customer");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM