Hibernate - SQL语法中有错误;查看与MariaDB服务器版本

时间:2017-06-30 16:21:10

标签: hibernate jpa

我有三个文件:

  1. Message.java

    public class Message {
        @Id
        @GeneratedValue
        private Long id;
    
        private String text;
    
        public String getText() {
            return text;
        }
    
        public void setText(String text) {
           this.text = text;
        }
    }
    
  2. persistence.xml

  3. <?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_2_1.xsd">
      <persistence-unit name="HelloWorldPU">
        <!-- <jta-data-source>myDB</jta-data-source> -->
        <class>org.jpwh.model.helloworld.Message</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
          <property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
          <property name="hibernate.format_sql" value="true" />
          <property name="hibernate.use_sql_comments" value="true" />
          <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
          <property name="javax.persistence.jdbc.user" value="laki" />
          <property name="javax.persistence.jdbc.password" value="laki" />
          <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/nekretnine?autoreconnect=true" />
          <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
          <property name="hibernate.max_fetch_depth" value="3" />
        </properties>
      </persistence-unit>
    </persistence>

    1. Main.java

      public class Main {
          public static void main(String[] args) {
              EntityManagerFactory emf = Persistence.createEntityManagerFactory("HelloWorldPU");
             EntityManager em = emf.createEntityManager();
             Message message = new Message();
             message.setText("kurcina");
             em.persist(message);
          }  
      }
      
    2. 当我执行Main类表时,在DB中创建了Message,但是我得到了这些异常

      WARN: GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement
      org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
      	at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
      	at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlString(SchemaCreatorImpl.java:440)
      	at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlStrings(SchemaCreatorImpl.java:424)
      	at org.hibernate.tool.schema.internal.SchemaCreatorImpl.createFromMetadata(SchemaCreatorImpl.java:290)
      	at org.hibernate.tool.schema.internal.SchemaCreatorImpl.performCreation(SchemaCreatorImpl.java:166)
      	at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:135)
      	at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:121)
      	at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:155)
      	at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72)
      	at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:309)
      	at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:452)
      	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:889)
      	at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
      	at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
      	at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
      	at tutorial.Main.main(Main.java:19)
      Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'sequence hibernate_sequence start with 1 increment by 1' at line 1
      	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:536)
      	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:513)
      	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:115)
      	at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:1983)
      	at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:1936)
      	at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:891)
      	at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:795)
      	at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
      	... 15 more
      
      Jun 30, 2017 5:51:41 PM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
      INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@4566d049'
      Jun 30, 2017 5:51:41 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
      WARN: SQL Error: 1064, SQLState: 42000
      Jun 30, 2017 5:51:41 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
      ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'value for hibernate_sequence' at line 1
      Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
      	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)
      	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
      	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
      	at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:787)
      	at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:765)
      	at tutorial.Main.main(Main.java:25)
      Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
      	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.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
      	at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:80)
      	at org.hibernate.id.enhanced.SequenceStructure$1.getNextValue(SequenceStructure.java:95)
      	at org.hibernate.id.enhanced.NoopOptimizer.generate(NoopOptimizer.java:40)
      	at org.hibernate.id.enhanced.SequenceStyleGenerator.generate(SequenceStyleGenerator.java:432)
      	at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:105)
      	at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67)
      	at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
      	at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
      	at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
      	at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:780)
      	... 2 more
      Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'value for hibernate_sequence' at line 1
      	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:536)
      	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:513)
      	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:115)
      	at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:1983)
      	at com.mysql.cj.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1826)
      	at com.mysql.cj.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1923)
      	at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:71)
      	... 11 more

      有什么想法吗?根据我发现,当用户编写SQL查询时,会报告MariaDB的错误,但我没有任何疑问。

1 个答案:

答案 0 :(得分:1)

您使用的是HSQLDB数据库的方言,但您的数据库是MariaDB / MySQL。

使用正确的方言。

你告诉Hibernate为你删除并创建架构,所以它就是这样做的。显然,创建模式需要执行SQL查询。