web-dev-qa-db-ja.com

initメソッドの呼び出しに失敗しました。ネストされた例外はJava.lang.IllegalArgumentExceptionです:メソッドpublicabstractのクエリの検証に失敗しました

私はSpring MVC + Spring-data-jpa + querydslを開発しています。 querydslのコードを作成するとき(そのコードが正常に機能する前)、querydsl関連のコードを追加すると、何が問題になっているのかわからない次のエラーが発生し始めました。ご案内ください。

参考のためのエラー:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'visitRepository': Invocation of init method failed; nested exception is Java.lang.IllegalArgumentException: Validation failed for query for method public abstract Java.util.List com.journaldev.spring.repository.VisitRepository.findByVisitType()!
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.Java:1578)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.Java:753)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.Java:839)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.Java:538)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.Java:668)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.Java:634)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.Java:682)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.Java:553)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.Java:494)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.Java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.Java:158)
    at org.Apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.Java:1284)
    at org.Apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.Java:1197)
    at org.Apache.catalina.core.StandardWrapper.load(StandardWrapper.Java:1087)
    at org.Apache.catalina.core.StandardContext.loadOnStartup(StandardContext.Java:5262)
    at org.Apache.catalina.core.StandardContext.startInternal(StandardContext.Java:5550)
    at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
    at org.Apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.Java:1575)
    at org.Apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.Java:1565)
    at Java.util.concurrent.FutureTask.run(FutureTask.Java:262)
    at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1145)
    at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:615)
    at Java.lang.Thread.run(Thread.Java:745)
Caused by: Java.lang.IllegalArgumentException: Validation failed for query for method public abstract Java.util.List com.journaldev.spring.repository.VisitRepository.findByVisitType()!
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.Java:92)
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.Java:62)
    at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.Java:72)
    at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.Java:53)

Visit.Java

@Entity
@Table(name="VISIT")

    public class Visit {
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private Integer patientId;
        private String visitName;
        private String visitType;
        // setters and getters
    }

VisitRepository.Java

@Repository
public interface VisitRepository extends JpaRepository<Visit, Integer>,
        QueryDslPredicateExecutor<Visit>{
    List<Visit> findByVisitType(String visitType);

    @Query("SELECT v FROM VISIT v WHERE v.visitType='NEW'")
    List<Visit> findByVisitType();
}

永続性.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="spring-data-jpa-krishna" transaction-type="RESOURCE_LOCAL">
        <class>com.journaldev.spring.model.Visit</class>
    </persistence-unit>
</persistence>

pom.xml

<properties>
        <Java.version>1.7</Java.version>
        <org.springframework-version>4.2.4.RELEASE</org.springframework-version>
        <org.aspectj-version>1.7.4</org.aspectj-version>
        <org.slf4j-version>1.7.5</org.slf4j-version>
        <hibernate.version>5.1.0.Final</hibernate.version>
    </properties>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <!-- Hibernate Entity Manager -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- Apache Commons DBCP -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <!-- Spring ORM -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

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

        <!-- Querydsl dependencies -->
        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-core</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
            <version>3.3.2</version>
        </dependency>

        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>

        <!-- LOG4J -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.9.2.RELEASE</version>
        </dependency>
        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <!-- JSP API -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>

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

        <!-- Junit Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-Java</artifactId>
            <version>5.1.37</version>
        </dependency>
    </dependencies>

    <!-- Project Build -->
    <build>
        <finalName>spring-mvc-jpa-hibernate</finalName>
        <plugins>
            <plugin>
                <groupId>org.Apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>${Java.version}</source>
                    <target>${Java.version}</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>


            <plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>apt-maven-plugin</artifactId>
                <version>1.1.1</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <!-- Specifies the directory in which the query types are generated -->
                            <outputDirectory>target/generated-sources</outputDirectory>
                            <!-- States that the APT code generator should look for JPA annotations -->
                            <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

参考画像:

enter image description here

サーブレット-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />
    <context:component-scan base-package="com.journaldev.spring.controller" />

    <!-- Load database.properties file -->
    <context:property-placeholder location="classpath:database.properties" />

    <!-- Enable JPA Reporitories -->
    <jpa:repositories base-package="com.journaldev.spring.repository" />

    <!-- Enable Transactional Manner -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- Necessary to get the entity manager injected into the factory bean -->
    <beans:bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />


    <!-- ====== MYSQL DataSource ====== -->
    <beans:bean id="dataSource" class="org.Apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <beans:property name="driverClassName" value="${mysql.jdbc.driverClassName}" />
        <beans:property name="url" value="${mysql.jdbc.url}" />
        <beans:property name="username" value="${mysql.jdbc.userName}" />
        <beans:property name="password" value="${mysql.jdbc.password}" />
    </beans:bean>

    <!-- ====== Hibernate JPA Vendor Adaptor ======= -->
    <beans:bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <beans:property name="showSql" value="true"/>
        <beans:property name="generateDdl" value="true"/>
        <beans:property name="database" value="MYSQL"/>
    </beans:bean>


    <!-- Beans -->
    <beans:bean id="personService" class="com.journaldev.spring.service.PersonServiceImpl" />
    <beans:bean id="visitService" class="com.journaldev.spring.service.VisitServiceImpl" />


    <!-- ======== Entity Manager factory ======== -->
    <beans:bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <!-- Data Source -->
        <beans:property name="dataSource" ref="dataSource"/>

        <!-- JPA Vendor Adaptor -->
        <beans:property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>

        <!-- spring based scanning for entity classes-->
        <beans:property name="packagesToScan" value="com.journaldev.spring.*" />

        <beans:property name="jpaProperties">
            <beans:props>
                <beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>  <!-- validate | update | create | create-drop -->
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</beans:prop>
                <!-- <beans:prop key="hibernate.cache.use_query_cache">true</beans:prop> --> 
            </beans:props>
        </beans:property>
    </beans:bean>

    <!-- ======== Transaction Manager ==== -->
    <beans:bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <beans:property name="entityManagerFactory" ref="entityManagerFactory" />
    </beans:bean>

    <!-- This is for JSP -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
</beans:beans>
5
user4821194

わかりました、私はこれに対する解決策を得ました。テーブル名は使用しないでください。常にモデル/エンティティ名である必要があります。これ(@Query( "SELECT v FROM Visit v WHERE v.visitType = 'NEW'"))は問題を解決します。

@Repository
public interface VisitRepository extends JpaRepository<Visit, Integer>,
        QueryDslPredicateExecutor<Visit>{
    List<Visit> findByVisitType(String visitType);

    @Query("SELECT v FROM Visit v WHERE v.visitType='NEW'")
    List<Visit> findByVisitType();
}
8
user4821194

私は非常によく似た問題を抱えていました。私の質問は:

select fa from FlexAllowanceSnapshot s where s.employeeId=:employeeId

、あるべきだった:

select fa from FlexAllowanceSnapshot fa where fa.employeeId=:employeeId

SQLではなくHQLであることに注意する必要があるため、構文とルールは少し異なります。

0
Nestor Milyaev

hibernate.cfg.xmlファイルには、以下のようなテーブルのマッピングが含まれている必要があります。ファイルにないか確認してください。

......
<hibernate-configuration>
......
......
  <session-factory>
......
<mapping class="com.test.bean.dbBean.testTableHibernate"/>
......
 </session-factory>

</hibernate-configuration>
.....
0
Barani r

上記の方法を試してもエラーが発生する場合は、これを試すことができます(これで問題は解決しました)

@Query(value = "normal Sql query", nativeQuery=true).

値とnativeQueryを指定します

0
Ravi.Dudi