[ 
https://issues.apache.org/jira/browse/OPENJPA-1145?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18142290#comment-18142290
 ] 

Richard Zowalla commented on OPENJPA-1145:
------------------------------------------

Still reproducible on current master (4.2.0-SNAPSHOT, 4bad79e2f) with Derby. 
Reproducer, e.g. in 
openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/repro:

{code:java|title=User.java}
package org.apache.openjpa.persistence.repro;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

/** Entity without @Table, so the default table name is the reserved word USER. 
*/
@Entity
public class User {
    @Id
    public int id;

    public String name;
}
{code}
{code:java|title=TestRepro1145.java}
package org.apache.openjpa.persistence.repro;

import jakarta.persistence.EntityManager;

import org.apache.openjpa.persistence.test.SingleEMFTestCase;

/**
 * OPENJPA-1145: the default table name of an entity named User is not checked 
against the reserved words,
 * so CREATE TABLE User fails on Derby.
 */
public class TestRepro1145 extends SingleEMFTestCase {
    @Override
    public void setUp() {
        setUp(User.class, CLEAR_TABLES);
    }

    public void testPersistEntityNamedUser() {
        try (EntityManager em = emf.createEntityManager()) {
            em.getTransaction().begin();
            User u = new User();
            u.id = 1;
            u.name = "n";
            em.persist(u);
            em.getTransaction().commit();
            em.clear();
            assertEquals(1, em.createQuery("select u from User u", 
User.class).getResultList().size());
        }
    }
}
{code}

Result:
{noformat}
Syntax error: Encountered "User" at line 1, column 14. {stmnt CREATE TABLE User 
(id INTEGER NOT NULL, name VARCHAR(255), PRIMARY KEY (id))} [code=30000, 
state=42X01]
{noformat}
Column names are checked against the reserved words, the default table name is 
not.

> Reserved keywords in the DBDictionary isn't being used for table names
> ----------------------------------------------------------------------
>
>                 Key: OPENJPA-1145
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-1145
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jdbc
>            Reporter: Richard G. Curtis
>            Priority: Major
>
> First reported on the mailing list[1].
> --------
> I ran into a problem(I think) when doing some testing on Derby where I have
> an Entity named User and I'm having OpenJPA build my DB tables. Since 'User'
> is a reserved word on Derby, the SQL that OpenJPA generates to create my
> User table blows up[2].
> Each DBDictionary has a set of 'reserved words' but that list of words isn't
> used for table names, they are only used for column names. If a column name
> matches a reserved word, we append a 0 to the name or something like that.
> I'm wondering if we should do something similar with the table names?.
> I'm going to try to find some time soon here to look at the spec to see if
> this issue is discussed. Any thoughts or comments?
> -Rick
> [1]http://n2.nabble.com/Using-reserved-keywords-for-Entity-names-td3156045.html
> [2
> 6484  embeddables  TRACE  [main] openjpa.jdbc.SQL - <t 24634836, conn
> 9566840> executing stmnt 24008846 CREATE TABLE User (id INTEGER NOT NULL,
> USER0 VARCHAR(255), user_name VARCHAR(255), city VARCHAR(255), state
> VARCHAR(255), street VARCHAR(255), zip INTEGER, COORDINATES_ID INTEGER,
> number VARCHAR(255), type VARCHAR(255), PRIMARY KEY (id))
> 6484  embeddables  TRACE  [main] openjpa.jdbc.SQL - <t 24634836, conn
> 9566840> [0 ms] spent
> Exception in thread "main" <openjpa-0.0.0-rnull nonfatal general error>
> org.apache.openjpa.persistence.PersistenceException: Syntax error:
> Encountered "User" at line 1, column 14. {stmnt 24008846 CREATE TABLE User
> (id INTEGER NOT NULL, USER0 VARCHAR(255), user_name VARCHAR(255), city
> VARCHAR(255), state VARCHAR(255), street VARCHAR(255), zip INTEGER,
> COORDINATES_ID INTEGER, number VARCHAR(255), type VARCHAR(255), PRIMARY KEY
> (id))} [code=30000, state=42X01]
>        at 
> org.apache.openjpa.jdbc.meta.MappingTool.record(MappingTool.java:553)
>        at 
> org.apache.openjpa.jdbc.meta.MappingTool.record(MappingTool.java:453)
>        at 
> ....JDBCBrokerFactory.synchronizeMappings(JDBCBrokerFactory.java:159)



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to