This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 76357 - Entity Classes from Database: support automatically generated columns
Summary: Entity Classes from Database: support automatically generated columns
Status: RESOLVED FIXED
Alias: None
Product: javaee
Classification: Unclassified
Component: Persistence (show other bugs)
Version: 5.x
Hardware: All All
: P2 blocker with 4 votes (vote)
Assignee: Dongmei Cao
URL:
Keywords: API, API_REVIEW_FAST
: 87023 125787 129870 131805 149321 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-05-14 19:10 UTC by hsalameh
Modified: 2011-05-09 02:43 UTC (History)
3 users (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments
Patch file for changes to API (7.63 KB, text/plain)
2008-06-10 01:53 UTC, David Vancouvering
Details
Updated patch for API change (45.00 KB, text/plain)
2008-06-27 08:25 UTC, David Vancouvering
Details

Note You need to log in before you can comment on or make changes to this bug.
Description hsalameh 2006-05-14 19:10:42 UTC
when I generate Entity Classes from the database, 
i get the Id column as follows:

@Id
@Column(name = "CATEGORY_ID")
private Integer categoryId;

But I would like to have this line in it also:
@GeneratedValue(strategy=GenerationType.IDENTITY)


I know it might not be easy for the wizard to detect this and provide it 
automatically, but it would be nice to have the option to provide custom code 
to be added to the Id property to be applied to all generated classes.
Right now, I have to go into every class and add this line, and import the 
proper packages. This is a lot of work when you have a large number of classes.
Comment 1 Rochelle Raccah 2008-01-22 22:49:05 UTC
*** Issue 125787 has been marked as a duplicate of this issue. ***
Comment 2 jonathanvilalopez 2008-01-25 10:18:41 UTC
I think the system knows if a column is of type IDENTITY ( when reversing )and has to generate by itself the
GeneratedValue annotation.
Comment 3 Andrei Badea 2008-01-30 10:57:39 UTC
*** Issue 87023 has been marked as a duplicate of this issue. ***
Comment 4 Andrei Badea 2008-01-30 11:01:34 UTC
Sorry, marking as enhancement, cf. issue 87023 desc 9. The current database schema model doesn't know that a column is
automatically generated.
Comment 5 Andrei Badea 2008-03-12 11:30:32 UTC
*** Issue 129870 has been marked as a duplicate of this issue. ***
Comment 6 jonathanvilalopez 2008-03-28 17:58:29 UTC
Is there any progress in this issue ? 

For those who use MS SQL Server and large databases is a very important stuff...... do you imagine modifying by hand
every entity with an Identity column ? and when regenerating the schema , begin to remodify again ?
Comment 7 Rochelle Raccah 2008-04-02 20:42:10 UTC
*** Issue 131805 has been marked as a duplicate of this issue. ***
Comment 8 Andrei Badea 2008-04-02 20:52:39 UTC
Not much, unfortunately. There wasn't much time to work on JPA in this release. 

Current idea is to use the IS_AUTOINCREMENT column added to DatabaseMetaData.getColumns() in JDBC 4. But I only tested
with the MySQL driver. No idea if other drivers already support it, so I don't know if it is feasible. If it proves to
be, the fix may appear in 6.5. Sorry, but it is definitely not for 6.1, which is already code-frozen.
Comment 9 Andrei Badea 2008-04-25 13:02:55 UTC
*** Issue 127234 has been marked as a duplicate of this issue. ***
Comment 10 David Vancouvering 2008-06-10 01:18:06 UTC
I am proposing adding a new method on the dbschema interface ColumnElement called isAutoIncrement.  I propose we use the
standard DatabaseMetaData getColumns support for autoIncrement.  If a driver doesn't support this (e.g. it throws an
exception), I'll return false.  Any comments?

I'll post the API review today or tomorrow.
Comment 11 David Vancouvering 2008-06-10 01:51:18 UTC
This is a change to the dbschema API.  It adds the method ColumnElement.isAutoIncrement.  Patch is being attached.

As you can see from the history the need for this is quite strong.  Please review and provide comments.
Comment 12 David Vancouvering 2008-06-10 01:53:27 UTC
Created attachment 62593 [details]
Patch file for changes to API
Comment 13 Dongmei Cao 2008-06-10 03:23:00 UTC
Sounds good - ColumnElement.isAutoIncrement() is what I need.
Comment 14 Andrei Badea 2008-06-10 10:10:07 UTC
AB01: why does ColumnElement.Memory.isAutoIncrement() throw an exception instead of a proper implementation? Also
ColumnElementImpl.

AB02: the relevant column in the result of DatabaseMetaData is IS_AUTOINCREMENT, not AUTO_INCREMENT. Not sure about the
comment about column 12. It looks IS_AUTOINCREMENT was added as the last column in JDBC4, so it is number 23 I think,
and it was not present in JDBC3 (not even as a REMARKS columns). Also note the column type is String, not boolean, and
its values are "YES" and "NO".
Comment 15 David Vancouvering 2008-06-10 18:49:13 UTC
AB01: why does ColumnElement.Memory.isAutoIncrement() throw an exception instead of a proper implementation? Also
ColumnElementImpl.

Thanks, fixed. 

AB02: the relevant column in the result of DatabaseMetaData is IS_AUTOINCREMENT, not AUTO_INCREMENT. Not sure about the
comment about column 12. It looks IS_AUTOINCREMENT was added as the last column in JDBC4, so it is number 23 I think,
and it was not present in JDBC3 (not even as a REMARKS columns). Also note the column type is String, not boolean, and
its values are "YES" and "NO".

I was looking at the wrong API in DatabaseMetadata.  It's a bummer this only works for Java 6, and who knows for which
database vendors.  I'll test with our main database vendors (MySQL, Oracle, PostgreSQL, Java DB, Microsoft) and maybe
there are db-specific workarounds I can do.
Comment 16 David Vancouvering 2008-06-27 07:18:39 UTC
I have something that correctly detects auto-increment for both Java DB and MySQL.

PostgreSQL and Oracle don't do auto-increment or identity, they do sequences. 

Looking at the JPA spec, there are two generation strategies for an @Id:

@GeneratedValue(strategy=GenerationType.SEQUENCE)
@GeneratedValue(strategy=GenerationType.IDENTITY)

where auto-increment belongs to the IDENTITY bucket

(see http://www.oracle.com/technology/products/ias/toplink/jpa/howto/id-generation.html)

If you're using an existing sequence, you need to know the name of the sequence and its allocation size:

@SequenceGenerator(name="InvSeq",sequenceName="INV_SEQ", allocationSize=5)

None of this information is available through JDBC and would need to be done by directly querying the metadata of a
given database.

It seems to me that supporting sequences is out of the scope for this issue and I will focus on support for IDENTITY.

So if ColumnDef.isAutoIncrement() returns true, this means the entity generator should generate

@GeneratedValue(strategy=GenerationType.IDENTITY)

We can open another issues, to be handled in a subsequent release, to support sequences.  I know this is important, but
one step at a time...

A new diff will be available for review soon.
Comment 17 David Vancouvering 2008-06-27 08:25:00 UTC
Created attachment 63578 [details]
Updated patch for API change
Comment 18 David Vancouvering 2008-06-27 08:27:25 UTC
Please review attached patch, it addresses Andrei's issues and also includes a unit test which passes for MySQL and Java
DB, two of the main databases that support identity/auto-increment (vs. sequence).  MS SQL Server is the other one, but
this seems sufficient to prove that, assuming the JDBC driver supports the query, the code will work.
Comment 19 Andrei Badea 2008-07-09 12:53:33 UTC
API looks fine.
Comment 20 David Vancouvering 2008-07-10 17:57:25 UTC
The API change is complete, now you can add support for automatically generated columns

http://hg.netbeans.org/main?cmd=changeset;node=a45560212d61
Comment 21 Dongmei Cao 2008-07-11 22:20:54 UTC
Fixed checked in changeset 8d0b6fb8bc59. 
With this fix, 

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

will be genereated for PKs that are auto incremented 
Comment 22 Dongmei Cao 2008-10-07 19:51:36 UTC
*** Issue 149321 has been marked as a duplicate of this issue. ***
Comment 23 jonathanvilalopez 2009-02-19 16:40:40 UTC
Sorry pals, but I dont understand.... this issue has been marked as resolved ? 

In NB 6.5 still seems not be fixed. I'm using this version : NetBeans IDE 6.5 (Build 200811100001)
Comment 24 gabehamilton 2009-02-19 16:59:05 UTC
It is working for me with NetBeans IDE 6.5 (Build 200811100001).  
I recently generated new 'Entity Classes from Database' against a MySQL 5.0.27 DB with auto_increment columns.
The generated id columns have the annotation @GeneratedValue(strategy = GenerationType.IDENTITY).
Comment 25 Sergey Petrov 2009-11-17 01:20:03 UTC
I also see this annotation.
jonathanvilalopez what table do you use, what db and what is your results?
Comment 26 David Konecny 2011-05-09 02:43:49 UTC
Updating to more appropriate state.