【问题标题】:org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "CREATE FUNCTION"org.hibernate.tool.schema.spi.CommandAcceptanceException:执行 DDL“CREATE FUNCTION”时出错
【发布时间】:2021-10-01 11:15:20
【问题描述】:

我正在尝试创建一个函数,通过在 persistence.xml 中指定运行应用程序时使用 SQL 脚本计算 2 个点之间的距离(经度和纬度)

<property name="javax.persistence.schema-generation.create-script-source"
           value="calculate_distance_function.sql" />

但是我遇到了 CommandAcceptanceException 并且无法弄清楚原因。 我什至尝试使用

<property name="javax.persistence.sql-load-script-source"
          value="calculate_distance_function.sql" />

但仍然无法正常工作,并出现以下错误。

00:06:21,512 INFO [stdout] (ServerService 线程池 -- 72) 休眠:创建函数计算距离(Lat1 DOUBLE,Long1 DOUBLE, Lat2 DOUBLE, Long2 DOUBLE ) 返回双倍

00:06:21,513 警告 [org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl] (ServerService Thread Pool -- 72) GenerationTarget 遇到 异常接受命令:执行 DDL 时出错“CREATE FUNCTION CalcDistance(Lat1 DOUBLE,Long1 DOUBLE,Lat2 DOUBLE,Long2 DOUBLE) RETURNS double" 通过 JDBC 语句: org.hibernate.tool.schema.spi.CommandAcceptanceException:错误 执行 DDL "CREATE FUNCTION CalcDistance(Lat1 DOUBLE, Long1 DOUBLE、Lat2 DOUBLE、Long2 DOUBLE ) 通过 JDBC 语句返回 double" 在 org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) 在 org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlString(SchemaCreatorImpl.java:440) 在 org.hibernate.tool.schema.internal.SchemaCreatorImpl.createFromScript(SchemaCreatorImpl.java:192) 在 org.hibernate.tool.schema.internal.SchemaCreatorImpl.performCreation(SchemaCreatorImpl.java:162) 在 org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:135) 在 org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:121) 在 org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:129) 在 org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72) 在 org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:310) 在 org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:467) 在 org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:939) 在 org.jboss.as.jpa.hibernate5.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44) 在 org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:167) 在 org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:125) 在 org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:650) 在 org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:209) 在 org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35) 在 org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1985) 在 org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1487) 在 org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1378) 在 java.lang.Thread.run(Thread.java:748) 在 org.jboss.threads.JBossThread.run(JBossThread.java:485) 原因: java.sql.SQLSyntaxErrorException:您的 SQL 中有错误 句法;检查与您的 MySQL 服务器版本相对应的手册 在第 1 行的 '' 附近使用正确的语法 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:118) 在 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95) 在 com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) 在 com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:790) 在 com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:675) 在 org.jboss.jca.adapters.jdbc.WrappedStatement.execute(WrappedStatement.java:198) 在 org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54) ... 21 更多

这是我的 persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
    version="2.1">

    <persistence-unit name="Persistence"
        transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <jta-data-source>java:jboss/datasources/tontapatt_db</jta-data-source>
        
        <class>fr.eql.ai109.tontapatt.entity.ZipCodeCity</class>
        <properties>
            <property
                name="javax.persistence.schema-generation.database.action"
                value="create" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.dialect"
                value="org.hibernate.dialect.MySQL8Dialect" />
          <property name="javax.persistence.schema-generation.create-script-source"
           value="calculate_distance_function.sql" />
        <!--        <property name="javax.persistence.sql-load-script-source"
          value="calculate_distance_function.sql" /> -->
        </properties>
    </persistence-unit>

</persistence>

更改 MySQL 的方言也无济于事。 如果我尝试在 MySQL 工作台中打开脚本并运行它,它工作正常,我可以看到添加的功能。这是我的 SQL 函数脚本

CREATE DEFINER=`root`@`localhost` FUNCTION `CalcDistance`( Lat1 DOUBLE, Long1 DOUBLE, Lat2 DOUBLE, Long2 DOUBLE ) RETURNS double 
BEGIN 
DECLARE r INT;    
DECLARE phi1 DOUBLE;    
DECLARE phi2 DOUBLE;    
DECLARE dphi DOUBLE;    
DECLARE dlam DOUBLE;    
DECLARE a DOUBLE;    
DECLARE c DOUBLE;    
DECLARE d DOUBLE;
SET r = 6371000;    
SET phi1 = Lat1 * PI() / 180;    
SET phi2 = Lat2 * PI() / 180;    
SET dphi = ( Lat2 - Lat1 ) * PI() / 180;    
SET dlam = ( Long2 - Long1 ) * PI() / 180;   
SET a = SIN( dphi / 2 ) * SIN( dphi / 2 ) + COS( phi1 ) * COS( phi2 ) * SIN( dlam / 2 ) * SIN( dlam / 2 );    
SET c = 2 * ATAN2( SQRT( a ), SQRT( 1 - a ) );    
SET d = r * c / 1000;    
RETURN d;    
END

我什至尝试在它之前和之后添加一个分隔符。

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: java mysql hibernate jpa persistence.xml


    【解决方案1】:

    你能在第一行尝试单引号而不是反引号吗?

    改变

    DEFINER=`root`@`localhost` FUNCTION `CalcDistance` 
    

    DEFINER='root'@'localhost' FUNCTION 'CalcDistance' (Note single quotes) 
    

    这是 MySQL 官方文档中的一个示例,它运行良好。

    CREATE DEFINER = 'admin'@'localhost' PROCEDURE account_count()
    BEGIN
      SELECT 'Number of accounts:', COUNT(*) FROM mysql.user;
    END;
    

    【讨论】:

    • 我不确定我是否理解使用单引号的意思。我的理解是将整个函数创建语句放在一行上。这工作正常。谢谢
    猜你喜欢
    • 2020-04-27
    • 2020-08-28
    • 2017-10-20
    • 2023-01-11
    • 2019-05-23
    • 2019-03-26
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多