Internal Exception: java.sql.SQLException: Statement.executeUpdate() cannot be called with a statement that returns a ResultSet
Я повторяю пример по JPA из книги, и у меня не получается в тесте добавить данные из файла, который является sql-скриптом, во встроенную базу данных. Подскажите, пожалуйста, в чем может быть проблема?
У меня в проекте имеются следующие файлы.
Сущность Book:
package org.example;
import javax.persistence.*;
@Entity
@NamedQueries({
@NamedQuery(name = "findAllBooks", query = "SELECT b FROM Book b"),
@NamedQuery(name = "findBookH2G2", query = "SELECT b FROM Book b WHERE b.title ='H2G2'")
})
public class Book {
@Id
@GeneratedValue
private Long id;
private String title;
private Float price;
private String description;
private String isbn;
private Integer nbOfPage;
private Boolean illustrations;
public Book() {
}
public Long getId() {
return id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public Integer getNbOfPage() {
return nbOfPage;
}
public void setNbOfPage(Integer nbOfPage) {
this.nbOfPage = nbOfPage;
}
public Boolean getIllustrations() {
return illustrations;
}
public void setIllustrations(Boolean illustrations) {
this.illustrations = illustrations;
}
}
Файл 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="chapter04TestPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>org.example.Book</class>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.create-source" value="metadata"/>
<property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:chapter04DB;create=true"/>
<property name="javax.persistence.sql-load-script-source" value="META-INF/insert.sql"/>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
Скрипт insert.sql:
INSERT INTO BOOK(ID, TITLE, DESCRIPTION, ILLUSTRATIONS, ISBN, NBOFPAGE, PRICE)
values (1010, 'The Lord of the Rings', 'One ring to rule them all', 0, '9012-3456', 222, 23)
Файл с тестом:
package org.example;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import static org.junit.Assert.assertEquals;
public class AppTest {
private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("chapter04TestPU");
private EntityManager em;
@Before
public void initEntityManager() throws Exception {
em = emf.createEntityManager();
}
@After
public void closeEntityManager() throws Exception {
if (em != null) em.close();
}
@Test
public void shouldFindBook() throws Exception {
Book book = em.find(Book.class, 1010L);
// assertEquals("The Lord of the Rings", book.getTitle());
}
}
POM.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.1-b11</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.14.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Стек ошибки:
/usr/lib/jvm/jdk1.8.0_281/bin/java -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:/home/galina/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/203.7148.57/lib/idea_rt.jar=44847:/home/galina/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/203.7148.57/bin -Dfile.encoding=UTF-8 -classpath /home/galina/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/203.7148.57/lib/idea_rt.jar:/home/galina/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/203.7148.57/plugins/junit/lib/junit5-rt.jar:/home/galina/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/203.7148.57/plugins/junit/lib/junit-rt.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/charsets.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/deploy.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/ext/dnsns.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/ext/jaccess.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/ext/jfxrt.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/ext/localedata.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/ext/nashorn.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/ext/sunec.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/ext/zipfs.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/javaws.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/jce.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/jfr.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/jfxswt.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/jsse.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/management-agent.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/plugin.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/resources.jar:/usr/lib/jvm/jdk1.8.0_281/jre/lib/rt.jar:/home/galina/Примеры проектов/example/target/test-classes:/home/galina/Примеры проектов/example/target/classes:/home/galina/.m2/repository/org/eclipse/persistence/org.eclipse.persistence.jpa/2.7.7/org.eclipse.persistence.jpa-2.7.7.jar:/home/galina/.m2/repository/org/eclipse/persistence/jakarta.persistence/2.2.3/jakarta.persistence-2.2.3.jar:/home/galina/.m2/repository/org/eclipse/persistence/org.eclipse.persistence.asm/2.7.7/org.eclipse.persistence.asm-2.7.7.jar:/home/galina/.m2/repository/org/eclipse/persistence/org.eclipse.persistence.antlr/2.7.7/org.eclipse.persistence.antlr-2.7.7.jar:/home/galina/.m2/repository/org/eclipse/persistence/org.eclipse.persistence.jpa.jpql/2.7.7/org.eclipse.persistence.jpa.jpql-2.7.7.jar:/home/galina/.m2/repository/org/eclipse/persistence/org.eclipse.persistence.core/2.7.7/org.eclipse.persistence.core-2.7.7.jar:/home/galina/.m2/repository/org/glassfish/javax.el/3.0.1-b11/javax.el-3.0.1-b11.jar:/home/galina/.m2/repository/org/apache/derby/derby/10.14.2.0/derby-10.14.2.0.jar:/home/galina/.m2/repository/junit/junit/4.11/junit-4.11.jar:/home/galina/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 org.example.AppTest,shouldFindBook
[EL Info]: 2021-09-02 12:17:00.431--ServerSession(1764696127)--EclipseLink, version: Eclipse Persistence Services - 2.7.7.v20200504-69f2c2b80d
[EL Warning]: 2021-09-02 12:17:02.43--ServerSession(1764696127)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.7.v20200504-69f2c2b80d): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: Table/View 'SEQUENCE' does not exist.
Error Code: 30000
Call: DELETE FROM SEQUENCE WHERE SEQ_NAME = 'SEQ_GEN'
Query: DataModifyQuery(sql="DELETE FROM SEQUENCE WHERE SEQ_NAME = 'SEQ_GEN'")
[EL Warning]: 2021-09-02 12:17:02.661--ServerSession(1764696127)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.7.v20200504-69f2c2b80d): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: Syntax error: Encountered "<EOF>" at line 1, column 78.
Error Code: 30000
Call: INSERT INTO BOOK(ID, TITLE, DESCRIPTION, ILLUSTRATIONS, ISBN, NBOFPAGE, PRICE)
Query: DataModifyQuery(sql="INSERT INTO BOOK(ID, TITLE, DESCRIPTION, ILLUSTRATIONS, ISBN, NBOFPAGE, PRICE)")
[EL Warning]: 2021-09-02 12:17:02.672--ServerSession(1764696127)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.7.v20200504-69f2c2b80d): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Statement.executeUpdate() cannot be called with a statement that returns a ResultSet.
Error Code: 30000
Call: values (1010, 'The Lord of the Rings', 'One ring to rule them all', 0, '9012-3456', 222, 23)
Query: DataModifyQuery(sql="values (1010, 'The Lord of the Rings', 'One ring to rule them all', 0, '9012-3456', 222, 23)")
Process finished with exit code 0
Ответы (1 шт):
Автор решения: Галина
→ Ссылка
Было 2 ошибки:
в блоке persistence-unit не хватало строчки
<property name="javax.persistence.schema-generation.database.action" value="create"/>в файле со скриптом у меня был разрыв строки