0

I want to build a simple web application using Spring, Eclipselink and MySQL, running on Wildfly 8.2. I want to run a simple query, but I'm keep getting this error:

13:47:37,144 ERROR [io.undertow.request] (default task-9) UT005023: Exception handling request to /MovieTimeProject/hello: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Problem compiling [SELECT m FROM Movie m WHERE m.title LIKE 'Eternal sunshine of%']. 
[14, 19] The abstract schema type 'Movie' is unknown.
[28, 35] The state field path 'm.title' cannot be resolved to a valid type.
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) [jboss-servlet-api_3.1_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [jboss-servlet-api_3.1_spec-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:63) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:261) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:247) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:76) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:166) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:197) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:759) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_31]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_31]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_31]
Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Problem compiling [SELECT m FROM Movie m WHERE m.title LIKE 'Eternal sunshine of%']. 
[14, 19] The abstract schema type 'Movie' is unknown.
[28, 35] The state field path 'm.title' cannot be resolved to a valid type.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1616) [eclipselink.jar:2.6.0.v20150309-bf26070]
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1636) [eclipselink.jar:2.6.0.v20150309-bf26070]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_31]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_31]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_31]
    at java.lang.reflect.Method.invoke(Method.java:483) [rt.jar:1.8.0_31]
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:291) [spring-orm-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at com.sun.proxy.$Proxy60.createQuery(Unknown Source)
    at com.movietime.repositories.MovieRepository.findMoviesByTitle(MovieRepository.java:34) [classes:]
    at com.movietime.dataservices.DataService.findMoviesByTitle(DataService.java:27) [classes:]
    at com.movietime.dataservices.DataService$$FastClassBySpringCGLIB$$52da0303.invoke(<generated>) [spring-core-4.1.6.RELEASE.jar:]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [spring-core-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717) [spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) [spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) [spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) [spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653) [spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at com.movietime.dataservices.DataService$$EnhancerBySpringCGLIB$$283ef398.findMoviesByTitle(<generated>) [spring-core-4.1.6.RELEASE.jar:]
    at com.movietime.controller.HelloController.welcomePage(HelloController.java:29) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_31]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_31]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_31]
    at java.lang.reflect.Method.invoke(Method.java:483) [rt.jar:1.8.0_31]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    ... 30 more
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Problem compiling [SELECT m FROM Movie m WHERE m.title LIKE 'Eternal sunshine of%']. 
[14, 19] The abstract schema type 'Movie' is unknown.
[28, 35] The state field path 'm.title' cannot be resolved to a valid type.
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155) [eclipselink.jar:2.6.0.v20150309-bf26070]
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:347) [eclipselink.jar:2.6.0.v20150309-bf26070]
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278) [eclipselink.jar:2.6.0.v20150309-bf26070]
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163) [eclipselink.jar:2.6.0.v20150309-bf26070]
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142) [eclipselink.jar:2.6.0.v20150309-bf26070]
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116) [eclipselink.jar:2.6.0.v20150309-bf26070]
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102) [eclipselink.jar:2.6.0.v20150309-bf26070]
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86) [eclipselink.jar:2.6.0.v20150309-bf26070]
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1614) [eclipselink.jar:2.6.0.v20150309-bf26070]
    ... 63 more

I Googled a lot, and people are getting this error if they are not using JPQL correctly, however I am.

Here is my project structure.

Columns of the movies table.

The exception is thrown here, in the MovieRepository class:

package com.movietime.repositories;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.movietime.entities.Actor;
import com.movietime.entities.Movie;

@Repository
@Scope(value="singleton")
public class MovieRepository {

    @PersistenceContext
    private EntityManager em;

    //@Transactional
    public void register(Movie mov) {
        // Save employee
        this.em.persist(mov);
    }


    @SuppressWarnings("unchecked")
    public List<Movie> findMoviesByTitle(String title) {

        String queryString = "SELECT m FROM Movie m WHERE m.title LIKE '" + title + "%'";
        Query query = em.createQuery(queryString, Movie.class);

        return query.getResultList();
    }

}

Movie entity class:

/**
 * The persistent class for the movies database table.
 * 
 */
@Entity(name="Movie")
@Table(name="movies")
@NamedQuery(name="Movie.findAll", query="SELECT m FROM Movie m")
public class Movie implements Serializable {
    private static final long serialVersionUID = 1L;
    private int movieid;
    private String imdbid;
    @Column(name = "title")
    private String title;
    private String year;
    private List<Akatitle> akatitles;
    private List<Director> directors;
    private List<Distributor> distributors;
    private List<Editor> editors;
    private List<Genre> genres;
    private List<Keyword> keywords;
    private List<Language> languages;
    private List<Literature> literatures;
    private List<Location> locations;
    private List<Actor> actors;
    private List<Composer> composers;
    private List<Writer> writers;
    private List<Mpaarating> mpaaratings;
    private List<Plot> plots;
    private List<Prodcompany> prodcompanies;
    private List<Producer> producers;
    private List<Quote> quotes;
    private List<Rating> ratings;
    private List<Releasedate> releasedates;
    private List<Runningtime> runningtimes;
    private List<Soundtrack> soundtracks;
    private List<Tagline> taglines;
    private List<Technical> technicals;
    private List<Trivia> trivias;

    public Movie() {
    }

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public int getMovieid() {
        return this.movieid;
    }

    public void setMovieid(int movieid) {
        this.movieid = movieid;
    }


    public String getImdbid() {
        return this.imdbid;
    }

    public void setImdbid(String imdbid) {
        this.imdbid = imdbid;
    }


    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


    public String getYear() {
        return this.year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    //bi-directional many-to-one association to Akatitle
    @OneToMany(mappedBy="movie")
    @JoinColumn(name="movieid")
    public List<Akatitle> getAkatitles() {
        return this.akatitles;
    }

    public void setAkatitles(List<Akatitle> akatitles) {
        this.akatitles = akatitles;
    }

    public Akatitle addAkatitle(Akatitle akatitle) {
        getAkatitles().add(akatitle);
        akatitle.setMovie(this);

        return akatitle;
    }

    public Akatitle removeAkatitle(Akatitle akatitle) {
        getAkatitles().remove(akatitle);
        akatitle.setMovie(null);

        return akatitle;
    }

//...

}

Here is my persistence.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="MovieTime" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes> 
        <properties>
                <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
                <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/movietime2"/>
                <property name="javax.persistence.jdbc.user" value="root"/>
                <property name="javax.persistence.jdbc.password" value="root"/>
        </properties>
    </persistence-unit>
</persistence>

spring-servlet.xml file:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- Use @Component annotations for bean definitions -->
    <context:component-scan base-package="com.movietime" />

    <!-- Use @Controller annotations for MVC controller definitions -->
    <mvc:annotation-driven enable-matrix-variables="true"/>

    <!-- View resolver -->
    <bean class=
        "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/pages/"/>
      <property name="suffix" value=".jsp" />
    </bean>

    <!-- Defining the dataSource -->
    <bean id="dataSource" name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/movietime2" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

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

     <!-- Define EclipseLink JPA Vendor Adapter -->
    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
        <property name="databasePlatform"
            value="org.eclipse.persistence.platform.database.MySQLPlatform" />
        <property name="generateDdl" value="false" />
        <property name="showSql" value="true" />
    </bean>


    <!-- Setting up JPA Entity Manager Factory -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
        <property name="packagesToScan" value="com.movietime.entities" />
        <property name="persistenceUnitName" value="MovieTime"></property>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="dataSource" ref="dataSource"/>
        <!-- 
        <property name="loadTimeWeaver">
            <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
        </property>
        -->
    </bean>

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

    <!-- Detect @Transactional -->
    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>
LordScone
  • 3,207
  • 2
  • 18
  • 24
  • just a guess, but the Movie class isn't an entity in the persistence unit. Turn on EclipseLink logging to finest, and see what entities are being picked up. You might try defining the classes explicitly in the persistence.xml – Chris Apr 06 '15 at 13:52
  • Chris! Thanks, I've tried explicitly list all the entities, it didn't help. Isn't it because I'm using RESOURCE_LOCAL? – LordScone Apr 06 '15 at 20:59
  • No, the transaction-type should have no effect on this issue - the problem is your "Movie" within the JPQL does not correspond to an entity name within your persistence unit. This is either because the entity wasn't found (you should get other errors using the Movie class within your project) or that you named it incorrectly. Check that the classes you are using/deployed are the ones you have shown here and not some older version - it is odd that you are explicitly using Entity(name="Movie") rather then letting it default and some older version in your server might have it wrong. – Chris Apr 07 '15 at 13:15

1 Answers1

0

You have not specified the name of the persistence unit to use within your annotation, which should then use a "default" persistence unit. Try specifying your "MovieTime" persistence unit instead:

@PersistenceContext(unitName="MovieTime")
private EntityManager em;
Chris
  • 20,138
  • 2
  • 29
  • 43