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 87023 - Perstitence class generation does not recognize automatically generated columns
Summary: Perstitence class generation does not recognize automatically generated columns
Status: RESOLVED DUPLICATE of bug 76357
Alias: None
Product: javaee
Classification: Unclassified
Component: Persistence (show other bugs)
Version: 5.x
Hardware: All Windows XP
: P3 blocker (vote)
Assignee: Andrei Badea
URL:
Keywords:
: 114315 118275 121407 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-10-12 19:30 UTC by jfurmankiewicz
Modified: 2008-01-30 10:57 UTC (History)
3 users (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments
Sequence samples (37.99 KB, image/png)
2007-02-15 15:36 UTC, jfurmankiewicz
Details

Note You need to log in before you can comment on or make changes to this bug.
Description jfurmankiewicz 2006-10-12 19:30:10 UTC
In PostgreSQL, the "serial" and "bigserial" column datatypes are used for
auto-incremented columns (using sequences, like in Oracle).

If I have a table like:

CREATE TABLE entity
(
  entity_id serial NOT NULL, -- Entity ID
  first_name varchar(255) NOT NULL,
  last_name varchar(255) NOT NULL,
  middle_name varchar(255),
  date_of_birth date,
  CONSTRAINT "PK_entity" PRIMARY KEY (entity_id)
) 
WITHOUT OIDS;
ALTER TABLE entity OWNER TO postgres;
GRANT ALL ON TABLE entity TO postgres;
GRANT ALL ON TABLE entity TO public;
COMMENT ON TABLE entity IS 'Entity';
COMMENT ON COLUMN entity.entity_id IS 'Entity ID';

The entity class that gets generated has a property:

public class Entity implements Serializable {

    @Id
    @Column(name = "entity_id", nullable = false)
    private Integer entityId;

However, this is wrong. It is totally missing the @GeneratedValue annotation for
this column.

Instead, for PostgreSQL the proper code that works is this:

public class Entity implements Serializable {

    @Id
    @Column(name = "entity_id", nullable = false)
   
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="entity_entity_id_seq")
    private Integer entityId;

When generating entity classes via the NB wizard, all of them would need to be
modified by hand accordingly to work properly, otherwise they all fail when
trying to use them.

I'm on NB 5.5RC1, JDK 6.0b101, WinXP SP2, PostgreSQL 8.1, Glassfish V2. I used
the JDBC3 driver from PostgreSQL for the persistence datasource (using Toplink).
Comment 1 Jan Horvath 2006-10-13 09:29:08 UTC
changing priority to P3 because PostgreSQL is not supported database.
Comment 2 Andrei Badea 2007-02-15 15:10:44 UTC
Generated column values aren't supported for any database, since there is almost
no support for it in JDBC. 

How could one infer the "entity_entity_id_seq" sequence name? Is it always the
table name, column name and "seq" concatenated and separated by underscores
(when the column type is serial/bigserial)? Or is there a way to query the
PostgreSQL JDBC driver for the sequence name?
Comment 3 jfurmankiewicz 2007-02-15 15:35:26 UTC
Honestly, I am not sure. I understand though that PostgreSql automatically
creates a sequence for you when defining a "serial" column, using the format:

(table_name)_(column_name)_seq

so, for "entity.entity_id" we would get a sequence called "entity_entity_id_seq".

I am not sure if you can get this via JDBC or not. If at the very least NB could
generated the @GeneratedValue annotation using this strategy, we would probably
cover 99% of the use cases for PostgreSQL.

See attached screenshot.
Comment 4 jfurmankiewicz 2007-02-15 15:36:20 UTC
Created attachment 38553 [details]
Sequence samples
Comment 5 Andrei Badea 2007-09-04 08:16:55 UTC
*** Issue 114315 has been marked as a duplicate of this issue. ***
Comment 6 Andrei Badea 2007-10-10 11:17:18 UTC
*** Issue 118275 has been marked as a duplicate of this issue. ***
Comment 7 Erno Mononen 2007-11-07 19:24:25 UTC
*** Issue 121407 has been marked as a duplicate of this issue. ***
Comment 8 Andrei Badea 2008-01-15 14:48:43 UTC
Changing to enhancement. I know this is dissapointing, but supporting generated columns was never in the scope of the
Entity Classes from DB wizard when it was introduced in 5.0. The needed infrastructure is missing from the database
support, so there is also no point in pretending that this can be fixed for 6.1.
Comment 9 jfurmankiewicz 2008-01-15 14:55:03 UTC
Sorry to hear that. This would be very useful if we have to generate entity classes for let's say 200 tables in one shot.

Without support for this we would need to open 200 Java classes by hand after they were generated and modify them manually.

Maybe a good solution would be for the wizard to prompt for an optional sequence format (e.g. "<TABLE_NAME>_SEQ") that
would be autogenerated when creating entity classes ... most DBs have some common naming schema they follow for sequence
names.

This would probably be fairly simple, would not entail any major changes to the DB support (since you are relying on the
programmer to tell you what the sequence naming schema should be instead of trying to figure it out yourself).
Comment 10 Andrei Badea 2008-01-30 10:57:42 UTC
Closing this as a duplicate of issue 76357. Although this issue has many duplicates, I prefer to leave 76357 open in
order to avoid losing its votes.

jfurmankiewicz: that would be quite a crude hack, and a PostgreSQL-oriented one. MySQL and MSSQL Server have identity
columns instead of sequences.


*** This issue has been marked as a duplicate of 76357 ***