Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 341811 - Generate Entities from Tables receive syntax error on attributes
Summary: Generate Entities from Tables receive syntax error on attributes
Status: VERIFIED FIXED
Alias: None
Product: Dali JPA Tools
Classification: WebTools
Component: General (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows Server 2003
: P2 normal (vote)
Target Milestone: 3.0 RC2   Edit
Assignee: Leslie Davis CLA
QA Contact:
URL:
Whiteboard: PMC_approved
Keywords:
Depends on:
Blocks:
 
Reported: 2011-04-04 11:52 EDT by Jolene Moffitt CLA
Modified: 2011-11-01 18:27 EDT (History)
4 users (show)

See Also:
neil.hauge: pmc_approved? (david_williams)
raghunathan.srinivasan: pmc_approved+
neil.hauge: pmc_approved? (naci.dai)
neil.hauge: pmc_approved? (deboer)
neil.hauge: pmc_approved+
neil.hauge: pmc_approved? (kaloyan)
neil.hauge: pmc_approved? (cbridgha)
neil.hauge: review+


Attachments
proposed bug fix patch (2.55 KB, patch)
2011-04-20 13:04 EDT, Leslie Davis CLA
no flags Details | Diff
proposed bug fix patch (1.94 KB, patch)
2011-05-09 18:49 EDT, Leslie Davis CLA
no flags Details | Diff
proposed bug fix patch (6.91 KB, patch)
2011-05-16 14:27 EDT, Leslie Davis CLA
no flags Details | Diff
patch with additional recommended changes (10.77 KB, patch)
2011-05-18 16:19 EDT, Leslie Davis CLA
neil.hauge: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jolene Moffitt CLA 2011-04-04 11:52:18 EDT
Tested against I-3.3.0-20110331154142
Create a JPA 2.0 project (Can be either Generic or EL)
Connect to a Database (I used both oracle and DB2)
Generate Entities from Tables (Selected Employee and Phone)
Notice after the Entities are created you receive syntax errors on the version and type attributes.
In the structure view the attribute shows as _version_ and in the editor it shows as @Column(name=""VERSION"")
Syntax errors appear stating - Syntax error on token "VERSION", / expected

I showed this to Nan and she was able to re-create the issue.
Comment 1 Brian Vosburgh CLA 2011-04-04 14:53:06 EDT
DTP has both Oracle and DB2 listing "VERSION" as a reserved word. I'm not sure that's correct...

The recently-released database code now delimits reserved words when they are used as identifiers (which is what should be done). It appears the entity gen code is not appropriately escaping whatever it puts inside quotes.
Comment 2 Neil Hauge CLA 2011-04-04 15:00:32 EDT
At a glance, it would appear that it is reserved in DB2, but not in Oracle.  This should be verified and a bug can be filed against DTP if necessary, but the general escaping issue needs to be addressed in Dali.
Comment 3 Brian Vosburgh CLA 2011-04-05 11:53:56 EDT
I could not find any mention of Oracle reserving the word VERSION; so submitted DTP bug 341936 for incorrect Oracle driver behavior.

At least one version of DB2 have VERSION as a reserved word (http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.admin.doc/doc/r0001095.htm).
Comment 4 Leslie Davis CLA 2011-04-20 13:04:27 EDT
Created attachment 193722 [details]
proposed bug fix patch
Comment 5 Pascal Filion CLA 2011-04-22 13:11:26 EDT
I am also able to reproduce the issue. I was using Oracle XE 11g R2 (11.2 beta), the problem was with:

@Column(name="STATE")
private String _state_;
Comment 6 Brian Vosburgh CLA 2011-05-02 15:02:51 EDT
(In reply to comment #4)
> Created attachment 193722 [details]
> proposed bug fix patch

This patch is a bit too far-reaching. :-)

The DTP adapter code should return the identifier un-delimited. It's up to the client to figure out whether the returned string needs to be escaped. In particular, Java and XML will escape things differently, but both of them will end up calling AbstractDTPDriverAdapter.delimitName(...). So the escaping should be done in, possibly, GenericEntityGeneratorDatabaseAnnotationNameBuilder; since this is the crossover between database "identifier" and how that identifier gets put into Java source code.

Also, instead of adding a method to StringTools; we already have a method that should do what's necessary: StringTools.convertToJavaStringLiteral(String). This method also handles any other embedded characters that need to escaped.
Comment 7 Leslie Davis CLA 2011-05-09 18:49:11 EDT
Created attachment 195164 [details]
proposed bug fix patch
Comment 8 Neil Hauge CLA 2011-05-09 23:56:45 EDT
It would seem from the code that this patch wouldn't actually add the necessary delimiters that would need to be escaped unless I am not understanding what convertToJavaStringLiteral is supposed to do.
Comment 9 Brian Vosburgh CLA 2011-05-10 10:15:04 EDT
(In reply to comment #8)
> It would seem from the code that this patch wouldn't actually add the necessary
> delimiters that would need to be escaped unless I am not understanding what
> convertToJavaStringLiteral is supposed to do.

The column's "name" is already an "identifier"; i.e. it is already delimited if appropriate. (Calling it a "name" might not be entirely accurate, but it matches the annotation member's name....)

Also, I'm guessing, from a cursory butcher, there are a number of other places that should be calling StringTools.convertToJavaStringLiteral(...): join.vm calls customizer.quote(...) a couple of times; and there could be problems with tables, join tables, and join columns.... Just making work for Les. :-)
Comment 10 Leslie Davis CLA 2011-05-16 14:27:27 EDT
Created attachment 195772 [details]
proposed bug fix patch
Comment 11 Leslie Davis CLA 2011-05-18 16:19:37 EDT
Created attachment 196049 [details]
patch with additional recommended changes

Patch also includes fixes for @Table, @JoinTable, @JoinColumn annotations names to be properly escaped when name is reserved word.  Also the patch removes fixes improper leading/trailing "_" for the generated attribute names.
Comment 12 Neil Hauge CLA 2011-05-18 22:41:28 EDT
    Explain why you believe this is a stop-ship defect. Or, if it is a "hotbug" (requested by an adopter) please document it as such. 

This bug causes Dali to generate non-compiling classes in our Entity Gen functionality.  This is a regression, and is likely to be seen often in real world cases.

    Is there a work-around? If so, why do you believe the work-around is insufficient? 

Workaround is to fix generated classes, which is possible, but inconvenient.  It will also be broken again the next time generation is run.

    How has the fix been tested? Is there a test case attached to the bugzilla record? Has a JUnit Test been added? 

This fix has been tested over a series of days by Les and I.  

    Give a brief technical overview. Who has reviewed this fix? 

The change ensure that all columns and tables are properly delimited when necessary by using the annotationBuilder, and that these delimited names are properly escaped when when being written to Java.

    What is the risk associated with this fix? 

Low-medium.  Comprehensive testing is being used to minimize the risk of regression.
Comment 13 Neil Hauge CLA 2011-05-19 02:39:59 EDT
Approving so this can make it into Thursday's build.  We can roll back if there are any objections from the PMC.
Comment 14 Neil Hauge CLA 2011-05-19 14:36:52 EDT
Committed to RC2.
Comment 15 Jolene Moffitt CLA 2011-06-17 11:53:24 EDT
Verified in Build I-3.3.0RC4-20110603221533

Verified no syntax errors appear when you generate entites from tables.  See the link to view test steps for verification. 
http://wiki.eclipse.org/Dali_3.0_RC2