使用 jpa 出现 NoViableAltException

发布于 2024-09-15 00:37:04 字数 6571 浏览 5 评论 0原文

我在运行 jpa 时遇到问题。当我尝试运行 Login.java 时出现以下异常

WicketMessage:无法使用构造函数 public de.test.pages.LoginPage() 实例化页面

根本原因:

NoViableAltException(93!=[364:1: selectExpression 返回 [对象节点] : (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) 在 de.test.pages.LoginPage.(来源未知) 在 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) 在 org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.processEvents(BookmarkablePageRequestTarget.java:234) 在org.apache.wicket.request.AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:92) 在 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) 在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) 在 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) 在 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 公共类快速入门用户{ @ID @GenerateValue(策略 = GenerationType.TABLE) 私有 int id; 私有字符串名字; 私有字符串姓氏; 私有字符串密码; 私有字符串用户名;

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;
}

感谢

您的阅读。

乙二胺四乙酸

i´ve got a problem to get jpa running. Following exception appears when i try to run the Login.java

WicketMessage: Can't instantiate page using constructor public de.test.pages.LoginPage()

Root cause:

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

The method LoginPage() in Login.java looks like this:

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();

The 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>

Although the table should be created automatically, i had to create the table on myself.

At least the entity model QuickstartUser.java

@Entity
public class QuickstartUser {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private int id;
private String firstName;
private String lastName;
private String password;
private String username;

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;
}

}

Thank you for reading.

BVA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昵称有卵用 2024-09-22 00:37:04

这样的查询,

我目前正在学习 JPA,但我通常会编写像SELECT * FROM Quickstart_user

as

SELECT q FROM Quickstart_user q

您可以尝试更改代码并看看这是否有效吗?
附带说明一下,我还发现,对于我的一些项目,Hibernate JPA 实现比 Eclipse 效果更好。

I'm currently studying JPA, but I usually write a query like

SELECT * FROM quickstart_user

as

SELECT q FROM quickstart_user q

Can you try to change your code and see if this will work?
As a side note, I also found that, for some of my projects, the Hibernate JPA implementation works better than the Eclipse one.

浮生未歇 2024-09-22 00:37:04

这并不是您问题的真正答案,但是:

您永远不应该从前端组件内部调用数据库代码,这是模型和视图的糟糕组合。

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

这样您就可以单独测试每一层

This is not really the answer to your problem, but:

You should never call database code from inside a front end component, this is a terrible mix of model and view.

You should create a service to access your database (using JPA or whatever) and inject this service into either your page (using component instantiation listeners with wicket-spring or wicket-guice) or your wicket application.

That way you can test each layer in isolation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文