在JPA中运行SQL脚本时SQL语法错误

时间:2018-09-29 21:21:03

标签: mysql jpa

我正在Java的persistence.xml文件中运行一个MySQL脚本,以将一些值输入到某些MySQL表中:

INSERT INTO names(name,surname,gender) VALUES('Emilie','Knudsen','female'); INSERT INTO names(name,surname,gender) VALUES('Aya','Knudsen','female'); INSERT INTO names(name,surname,gender) VALUES('Ida','Jørgensen','female'); INSERT INTO names(name,surname,gender) VALUES('Ellen','Mortensen','female'); INSERT INTO names(name,surname,gender) VALUES('Mathilde','Sørensen','female'); INSERT INTO names(name,surname,gender) VALUES('Emilie','Knudsen','female'); INSERT INTO names(name,surname,gender) VALUES('Ella','Rasmussen','female'); INSERT INTO names(name,surname,gender) VALUES('Maja','Larsen','female'); INSERT INTO names(name,surname,gender) VALUES('Sofie','Madsen','female'); INSERT INTO names(name,surname,gender) VALUES('Silje','Vad','female'); INSERT INTO names(name,surname,gender) VALUES('Sofie','Larsen','female'); INSERT INTO names(name,surname,gender) VALUES('Vigga','Petersen','female'); INSERT INTO names(name,surname,gender) VALUES('Victoria','Christensen','female'); INSERT INTO names(name,surname,gender) VALUES('Liv','Larsen','female'); INSERT INTO names(name,surname,gender) VALUES('Malou','Jørgensen','female'); INSERT INTO names(name,surname,gender) VALUES('Merle','Olsen','female'); INSERT INTO names(name,surname,gender) VALUES('Alberte','Andersen','female'); INSERT INTO names(name,surname,gender) VALUES('Emma','Kristensen','female'); INSERT INTO names(name,surname,gender) VALUES('Filippa','Christiansen','female'); INSERT INTO names(name,surname,gender) VALUES('Johanne','Poulsen','female'); INSERT INTO names(name,surname,gender) VALUES('Vigga','Jørgensen','female'); INSERT INTO names(name,surname,gender) VALUES('Ellen','Thomsen','female'); INSERT INTO names(name,surname,gender) VALUES('Caroline','Møller','female'); INSERT INTO names(name,surname,gender) VALUES('Laura','Møller','female'); INSERT INTO names(name,surname,gender) VALUES('Thea','Knudsen','female'); 

当我在MySQL工作台中运行脚本时,它运行良好(某些条目具有相同的名称,因为我将其用作外键)。

但是当我从persistence.xml文件中运行相同的脚本时,出现此错误:

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO names(name,surname,gender) VALUES('Aya','Knudsen','female'); INSERT ' at line 1

错误代码:1064

这是我的persistence.xml代码:

    <?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/persistence_2_1.xsd">
  <persistence-unit name="samplePU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/Women?zeroDateTimeBehavior=convertToNull"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.password" value="*****"/>
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
      <property name="javax.persistence.sql-load-script-source" value="createData.sql"/>
    </properties>
  </persistence-unit>
</persistence>

测试语法后,为什么会出现此问题?

1 个答案:

答案 0 :(得分:1)

您应该更新您的“脚本”,以在所有INSERT语句之间放置换行符。像这样

INSERT INTO names(name,surname,gender) VALUES('Emilie','Knudsen','female'); 
INSERT INTO names(name,surname,gender) VALUES('Aya','Knudsen','female'); 
INSERT INTO names(name,surname,gender) VALUES('Ida','Jørgensen','female'); 
INSERT INTO names(name,surname,gender) VALUES('Ellen','Mortensen','female'); 

某些JPA提供程序可以计算出行尾的位置并分隔JDBC语句,但是其他JPA提供程序则不能这样做。如您所见,您似乎将所有通过JDBC的语句放在一条语句中,该语句将失败。您还应该在JPA提供程序上引发一个错误,以处理此类情况。