使用jpa的NoViableAltException

时间:2010-08-18 08:33:23

标签: java jpa wicket eclipselink

我遇到了让jpa运行的问题。当我尝试运行Login.java

时出现以下异常
  

WicketMessage:无法使用构造函数public de.test.pages.LoginPage()

实例化页面      

根本原因:

     

NoViableAltException(93!= [364:1:selectExpression返回[Object node] :( n = aggregateExpression | n = scalarExpression | OBJECT LEFT_ROUND_BRACKET n = variableAccessOrTypeConstant RIGHT_ROUND_BRACKET | n = constructorExpression | n = mapEntryExpression);])   在org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.selectExpression(JPQLParser.java:5893)   在org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.selectItem(JPQLParser.java:1356)   在org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.selectClause(JPQLParser.java:1270)   在org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.selectStatement(JPQLParser.java:351)   在org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.document(JPQLParser.java:275)   在org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser.parse(JPQLParser.java:130)   在org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser.buildParseTree(JPQLParser.java:91)   在org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:207)   在org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:182)   在org.eclipse.persistence.internal.jpa.EJBQueryImpl。(EJBQueryImpl.java:134)   在org.eclipse.persistence.internal.jpa.EJBQueryImpl。(EJBQueryImpl.java:118)   在org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1352)   at de.test.pages.LoginPage。(未知来源)   at java.lang.reflect.Constructor.newInstance(Constructor.java:513)   在org.apache.wicket.session.DefaultPageFactory.createPage(DefaultPageFactory.java:192)   在org.apache.wicket.session.DefaultPageFactory.newPage(DefaultPageFactory.java:57)   在org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.newPage(BookmarkablePageRequestTarget.java:298)   在org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.getPage(BookmarkablePageRequestTarget.java:320)   at org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.processEvents(BookmarkablePageRequestTarget.java:234)   at org.apache.wicket.request.AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:92)   at org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1250)   在org.apache.wicket.RequestCycle.step(RequestCycle.java:1329)   在org.apache.wicket.RequestCycle.steps(RequestCycle.java:1428)   在org.apache.wicket.RequestCycle.request(RequestCycle.java:545)   在org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:479)   在org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:312)   在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)   在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)   在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)   在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)   在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)   在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)   在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)   在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)   at org.apache.coyote.http11.Http11BaseProtocol $ Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)   在org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)   在org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)   at org.apache.tomcat.util.threads.ThreadPool $ ControlRunnable.run(ThreadPool.java:689)   在java.lang.Thread.run(Thread.java:637)

Login.java中的LoginPage()方法如下所示:

public LoginPage(){

    EntityManagerFactory factory = Persistence.createEntityManagerFactory("quickstartUser");
    EntityManager em = factory.createEntityManager();

    // Read the existing entries
    Query q = em.createQuery("SELECT * FROM quickstart_user");
    // Persons should be empty

    // Do we have entries?
    int createNewEntries = q.getResultList().size();

    Label label = new Label("result", "Result: ");
    add(label);

    // It is always good practice to close the EntityManager so that
    // resources are conserved.
    em.close();

persistance.xml

<persistence-unit name="quickstartUser" transaction-type="RESOURCE_LOCAL">

    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

    <exclude-unlisted-classes>false</exclude-unlisted-classes>

    <properties>
        <property name="eclipselink.jdbc.driver" value="org.postgresql.Driver" />
        <property name="eclipselink.jdbc.url" value="jdbc:postgresql://localhost:5432/test" />
        <!-- I work in this example without user / password.-->
        <property name="eclipselink.jdbc.user" value="" />
        <property name="eclipselink.jdbc.password" value="" />

        <!-- EclipseLink should create the database schema automatically -->

        <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
        <property name="eclipselink.ddl-generation.output-mode" value="database" />
    </properties>

</persistence-unit>

虽然应该自动创建表,但我必须自己创建表。

至少是实体模型QuickstartUser.java

@Entity 公共类QuickstartUser {     @ID     @GeneratedValue(strategy = GenerationType.TABLE)     private int id;     private String firstName;     private String lastName;     私有字符串密码;     私有字符串用户名;

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;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}

}

感谢您的阅读。

BVA

2 个答案:

答案 0 :(得分:1)

我目前正在学习JPA,但我通常会写一个像

这样的查询

SELECT * FROM quickstart_user

作为

SELECT q FROM quickstart_user q

您可以尝试更改代码,看看这是否有效? 作为旁注,我还发现,对于我的一些项目,Hibernate JPA实现比Eclipse更好。

答案 1 :(得分:1)

这不是你问题的答案,但是:

你永远不应该从前端组件中调用数据库代码,这是模型和视图的可怕组合。

您应该创建一个服务来访问您的数据库(使用JPA或其他)并将此服务注入您的页面(使用具有wicket-spring或wicket-guice的组件实例化侦听器)或您的wicket应用程序。

这样你可以孤立地测试每一层