JPA entitymanager.persist()异常

时间:2017-12-12 13:27:38

标签: jpa wildfly derby

所以我试图在我的数据库(Derby)中写入数据,我收到了这个错误。 javax.persistence.PersistenceException:org.hibernate.exception.SQLGrammarException:无法准备语句

Admin.html



<form action="RegisterServlet" method="post">
<h2>Új tag regisztrálása</h2>
UserName: <input type="text" name="Regname"><br>
Password: <input type="password" name="Regpassword"><br>
FelhasznaloID: <input type="number" name="Regid"><br>
<input type="radio" name="tag" value="semtag" checked> SEMTAg<br>
  <input type="radio" name="tag" value="nemsemtag"> Nem SEMTag<br>
<input type="submit" value="Regisztráció"><br>
&#13;
&#13;
&#13;

RegisterServlet.java

package com.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.services.CreateFelhasznalo;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet implements Servlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public RegisterServlet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String foo= "semtag";
    String fooka="nemsemtag";
    PrintWriter out=response.getWriter();
    String name=request.getParameter("Regname");
    String password=request.getParameter("Regpassword");
    int regid=Integer.parseInt(request.getParameter("Regid"));
    String semtag=request.getParameter("tag");
    if(semtag.equals(foo))
        CreateFelhasznalo.NewFelhasznalo(regid, name, password, "I");
    if(semtag.equals(fooka))
        CreateFelhasznalo.NewFelhasznalo(regid, name, password, "N");
    out.println("Sikeres regisztráció!");
}

}

Felhasznalok.java

package com.entity;

import java.io.Serializable;
import javax.persistence.*;


/**
 * The persistent class for the FELHASZNALOK database table.
 * 
 */
@Entity
@NamedQuery(name="Felhasznalok.findAll", query="SELECT f FROM Felhasznalok f")
public class Felhasznalok implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO) 
private int felhid;

private String password;

private String semtag;

private String username;

public Felhasznalok() {
}

public int getFelhid() {
    return this.felhid;
}

public void setFelhid(int felhid) {
    this.felhid = felhid;
}

public String getPassword() {
    return this.password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getSemtag() {
    return this.semtag;
}

public void setSemtag(String semtag) {
    this.semtag = semtag;
}

public String getUsername() {
    return this.username;
}

public void setUsername(String username) {
    this.username = username;
}
public String toString() {
      return "Felhasznalok [FelhID=" + felhid + ", UserName=" + username + ",Password=" + password + ", Semtag=" + semtag +  "]";
   }
}

CreateFelhasznalo.java

package com.services;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.entity.Felhasznalok;

public class CreateFelhasznalo {

   public static void main( String[ ] args ) {}

public static void NewFelhasznalo(int felhid,String username,String 
password,String semtag) {

  EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "Eclipselink_JPA" );

  EntityManager entitymanager = emfactory.createEntityManager( );
  entitymanager.getTransaction( ).begin( );

  Felhasznalok felhasznalo = new Felhasznalok( ); 
  felhasznalo.setFelhid( felhid );
  felhasznalo.setUsername( username );
  felhasznalo.setPassword( password );
  felhasznalo.setSemtag( semtag );

  entitymanager.merge( felhasznalo );
  entitymanager.getTransaction( ).commit( );

  entitymanager.close( );
  emfactory.close( );
   }
}

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" 
version="2.5">
    <display-name>SEMeszkozkolcsonzo</display-name>
    <welcome-file-list>
    <welcome-file>login.html</welcome-file>
   </welcome-file-list>
  <servlet>
  <description></description>
 <display-name>FirstServlet</display-name>
 <servlet-name>FirstServlet</servlet-name>
 <servlet-class>com.servlets.LoginServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>FirstServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>SecondServlet</display-name>
<servlet-name>SecondServlet</servlet-name>
<servlet-class>com.servlets.ProfileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SecondServlet</servlet-name>
<url-pattern>/ProfileServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>LogoutServlet</display-name>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.servlets.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/LogoutServlet</url-pattern>
 </servlet-mapping>
 <servlet>
<description></description>
<display-name>ToolServlet</display-name>
<servlet-name>ToolServlet</servlet-name>
<servlet-class>com.servlets.ToolServlet</servlet-class>
</servlet>
 <servlet-mapping>
<servlet-name>ToolServlet</servlet-name>
<url-pattern>/ToolServlet</url-pattern>
 </servlet-mapping>
<servlet>
<description></description>
<display-name>KolcsServlet</display-name>
<servlet-name>KolcsServlet</servlet-name>
<servlet-class>com.servlets.KolcsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>KolcsServlet</servlet-name>
<url-pattern>/KolcsServlet</url-pattern>
 </servlet-mapping>
 <servlet>
<description></description>
<display-name>FelhServlet</display-name>
<servlet-name>FelhServlet</servlet-name>
<servlet-class>com.servlets.FelhServlet</servlet-class>
 </servlet>
 <servlet-mapping>
<servlet-name>FelhServlet</servlet-name>
<url-pattern>/FelhServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>RegisterServlet</display-name>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.servlets.RegisterServlet</servlet-class>
 </servlet>
 <servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/RegisterServlet</url-pattern>
 </servlet-mapping>
 <session-config>
<session-timeout>30</session-timeout>
 </session-config>
 </web-app>

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
   http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

   <persistence-unit name="Eclipselink_JPA" transaction-type="RESOURCE_LOCAL">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>

  <class>com.entity.Felhasznalok</class>
  <class>com.entity.Eszkoz</class>
  <class>com.entity.Kolcsonzesek</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/sample;create=true" />
        <property name="javax.persistence.jdbc.password" value="app"/>
        <property name="javax.persistence.jdbc.user" value="app"/>
        <property name="eclipselink.logging.level" value="FINE"/>
        <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
        <property name="eclipselink.ddl-generation.output-mode" value="database" />
    </properties>
</persistence-unit>

</persistence>

错误日志

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1608)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:1171)
    at com.services.CreateFelhasznalo.NewFelhasznalo(CreateFelhasznalo.java:25)
    at com.servlets.RegisterServlet.doPost(RegisterServlet.java:45)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
    at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
    at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
    at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:326)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:812)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.hibernate.exception.SQLGrammarException: could not prepare statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:182)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:72)
    at org.hibernate.id.enhanced.SequenceStructure$1.getNextValue(SequenceStructure.java:94)
    at org.hibernate.id.enhanced.NoopOptimizer.generate(NoopOptimizer.java:40)
    at org.hibernate.id.enhanced.SequenceStyleGenerator.generate(SequenceStyleGenerator.java:412)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:105)
    at org.hibernate.jpa.event.internal.core.JpaMergeEventListener.saveWithGeneratedId(JpaMergeEventListener.java:56)
    at org.hibernate.event.internal.DefaultMergeEventListener.saveTransientEntity(DefaultMergeEventListener.java:254)
    at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:234)
    at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:300)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:169)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:68)
    at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:843)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:825)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:830)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:1161)
    ... 43 more
Caused by: java.sql.SQLSyntaxErrorException: SEQUENCE 'APP.HIBERNATE_SEQUENCE' nem létezik.
    at org.apache.derby.client.am.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
    at org.apache.derby.client.am.ClientConnection.prepareStatement(Unknown Source)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$1.doPrepare(StatementPreparerImpl.java:87)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)
    ... 58 more
Caused by: ERROR 42X94: SEQUENCE 'APP.HIBERNATE_SEQUENCE' nem létezik.
    at org.apache.derby.client.am.ClientStatement.completeSqlca(Unknown Source)
    at org.apache.derby.client.net.NetStatementReply.parsePrepareError(Unknown Source)
    at org.apache.derby.client.net.NetStatementReply.parsePRPSQLSTTreply(Unknown Source)
    at org.apache.derby.client.net.NetStatementReply.readPrepareDescribeOutput(Unknown Source)
    at org.apache.derby.client.net.StatementReply.readPrepareDescribeOutput(Unknown Source)
    at org.apache.derby.client.net.NetStatement.readPrepareDescribeOutput_(Unknown Source)
    at org.apache.derby.client.am.ClientStatement.readPrepareDescribeOutput(Unknown Source)
    at org.apache.derby.client.am.ClientPreparedStatement.readPrepareDescribeInputOutput(Unknown Source)
    at org.apache.derby.client.am.ClientPreparedStatement.flowPrepareDescribeInputOutput(Unknown Source)
    at org.apache.derby.client.am.ClientPreparedStatement.prepare(Unknown Source)
    at org.apache.derby.client.am.ClientConnection.prepareStatementX(Unknown Source)
    ... 61 more

我正在使用derby 10.2,Wildfly 11和eclipse进行我的项目。 我已经设置了数据库。 它是一个基本的webapp,你可以借用工具,我有这个管理界面的问题,我想在这里添加新用户到数据库。 当我运行应用程序时,它可以连接到数据库但在CreateFelhasznalo.java中 在方法entitymanager.merge(felhasznalo);它只是抛出异常。 有关初学者的提示吗?

更新:

当我在本地作为java应用程序运行它,并且我提供数据时,它完美地工作。

我认为在某个地方我附加了实体,但我不知道为什么。

过程:

在登录页面,我提供用户名和密码,我检查数据库中的那些是否正确,如果他是管理员,我会用登录名开始会话(在我的情况下,它& #39; s Admin),然后我打开管理控制台。如果他不是管理员,则用户无法访问此管理控制台,我对该部分没有任何问题。

当我想注册一个新用户时出现问题,我有一个html表单,我可以在其中添加用户名和密码,并检查他是否是管理员,在te telet连接到数据库和广告之后它与JPA,这是我的问题开始的地方,因为错误日志表明当我使用entitymanager.persist(felhasznalo)和javax.persistence.PersistenceException时,实体被分离:org.hibernate.exception.SQLGrammarException:我无法准备语句使用entitymanager.merge(felhasznalo)

任何猜测我哪里出错?

0 个答案:

没有答案