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.
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.
*** Issue 125787 has been marked as a duplicate of this issue. ***
I think the system knows if a column is of type IDENTITY ( when reversing )and has to generate by itself the GeneratedValue annotation.
*** Issue 87023 has been marked as a duplicate of this issue. ***
Sorry, marking as enhancement, cf. issue 87023 desc 9. The current database schema model doesn't know that a column is automatically generated.
*** Issue 129870 has been marked as a duplicate of this issue. ***
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 ?
*** Issue 131805 has been marked as a duplicate of this issue. ***
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.
*** Issue 127234 has been marked as a duplicate of this issue. ***
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.
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.
Created attachment 62593 [details] Patch file for changes to API
Sounds good - ColumnElement.isAutoIncrement() is what I need.
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".
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.
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.
Created attachment 63578 [details] Updated patch for API change
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.
API looks fine.
The API change is complete, now you can add support for automatically generated columns http://hg.netbeans.org/main?cmd=changeset;node=a45560212d61
Fixed checked in changeset 8d0b6fb8bc59. With this fix, @Id @GeneratedValue(strategy = GenerationType.IDENTITY) will be genereated for PKs that are auto incremented
*** Issue 149321 has been marked as a duplicate of this issue. ***
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)
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).
I also see this annotation. jonathanvilalopez what table do you use, what db and what is your results?
Updating to more appropriate state.