I'm writing an application in Open Liberty. I've got this persistent entity (shortened for simplicity):
User.java
@Entity
@Table(name = "User")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
}
This causes the following error:
[INFO] [WARNING ] CWWJP9991W: Exception [EclipseLink-4002] (Eclipse Persistence Services - 3.0.3.v202208190922): org.eclipse.persistence.exceptions.DatabaseException
[INFO] Internal exception: java.sql.SQLSyntaxErrorException: Syntax error: Encountered "User" at line 1, column 14.
[INFO] Error number: 30000
[INFO] Call: CREATE TABLE User (ID BIGINT NOT NULL, PRIMARY KEY (ID))
[INFO] Query: DataModifyQuery(sql="CREATE TABLE User (ID BIGINT NOT NULL, PRIMARY KEY (ID))")
Replacing "User" in the table name with anything else fixes this and makes the app work as expected.
Here's my persistence.xml file. Not sure if it's needed here. I'm using a Derby database. Following the answer in this thread did not solve this: SQLSyntaxErrorException on creating tables in Apache Derby using JPA
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
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_2.xsd">
<persistence-unit name="jpa-unit" transaction-type="JTA">
<jta-data-source>jdbc/my-project</jta-data-source>
<properties>
<property name="jakarta.persistence.schema-generation.database.action"
value="create"/>
<property name="jakarta.persistence.schema-generation.scripts.action"
value="create"/>
<property name="jakarta.persistence.schema-generation.scripts.create-target"
value="createDDL.ddl"/>
</properties>
</persistence-unit>
</persistence>
USER
is a reserved word in Apache Derby. Refer to the list of reserved words. A solution is to use the Table
annotation to specify a non-conflicting name value for the table, such as,
@Entity
@Table(name = "Users")
public class User {