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 156726 - Use @IdClass annotation instead of @EmbeddedId annotation, which can cause major foreign key problems
Summary: Use @IdClass annotation instead of @EmbeddedId annotation, which can cause ma...
Status: RESOLVED WONTFIX
Alias: None
Product: javaee
Classification: Unclassified
Component: Persistence (show other bugs)
Version: 6.x
Hardware: All Windows XP
: P4 blocker (vote)
Assignee: Dongmei Cao
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-01-13 22:28 UTC by kidvid
Modified: 2016-07-07 08:56 UTC (History)
0 users

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments
create table ddl (4.29 KB, text/plain)
2009-01-14 01:58 UTC, kidvid
Details
incorrectly generated class using an EBJ Project (6.89 KB, text/plain)
2009-01-14 01:59 UTC, kidvid
Details
156726: Create Table DDL (4.70 KB, text/plain)
2009-01-14 02:14 UTC, kidvid
Details
156726: Generated Java files using an EJB Project (13.46 KB, application/x-compressed)
2009-01-14 02:18 UTC, kidvid
Details

Note You need to log in before you can comment on or make changes to this bug.
Description kidvid 2009-01-13 22:28:47 UTC
The Netbeans EJB generator uses the @EmbeddedId annotation if you have a composite primary key.

We are using MS SQL Server 2008, with the free JDBC driver.

The parent table in question has two primary key columns, both mapped to Java Longs:  KeyNumbaOne, and KeyNumbaTwo.
The underlying columns in the table are called "key_numba_one" and "key_numba_two."

When I generated this bean, I also highlighted it's child table.

The child table has a one to many relationship with the parent table.  Put another way, the parent table has a many to
one relationship with the child table.  They join only on KeyNumbaOne.

The Generator puts in a @JoinColumn annotation that looks like this:
@JoinColumn(name = "key_numba_one", referencedColumn = "key_numba_one", updateable=false, insertable=false)
@ManyToOne
private ChildTableBean childTableBean;

Unfortunately, JBOSS 4.22 cannot deploy this.
Based on a lot of research I've done, a lot of people are having problems getting the @EmbeddedID annotation to work
with foreign keys that are made up of only part of the primary key and not all columns in the primary key.

What other people in Hibernate forums, as well as my own project have been doing to work around the NetBeans generated
code is to change how the primary keys work and use the @IdClass annotation instead.

When you do it that way, you have a lot more flexibility with joining to child tables that only join one part of the
primary table's composite key.

Please change how the primary key is generated in Netbeans to use the most flexible approach, so we don't have to spend
hours re-configuring our generated beans, or at least give the user the option to use either the @IdClass or @EmbeddedId
annotation.
Comment 1 kidvid 2009-01-13 23:50:28 UTC
Correction:  Beans generated as in the example below will deploy without a problem.

However, if you try to retrieve one using an EntityManager.find operation, you'll get an exception.

If you comment out all foreign key relation annotations in the parent and child class and recompile, the bean will work 
properly.

If you reconfigure the bean and use an @IdClass instead, it's possible to get the foreign key annotations to work.
Comment 2 kidvid 2009-01-14 01:58:22 UTC
Created attachment 75794 [details]
create table ddl
Comment 3 kidvid 2009-01-14 01:59:13 UTC
Created attachment 75795 [details]
incorrectly generated class using an EBJ Project
Comment 4 kidvid 2009-01-14 02:01:59 UTC
Please ignore the attachments.  They belong to another ticket.
Comment 5 kidvid 2009-01-14 02:14:10 UTC
I'm attaching two files that specifically pertain to this ticket:  issue 156726, for the @IdClass & @EmbeddedId problem 
mentioned previously.  These new file names will be preceded with this ticket number as to differentiate them from the 
other files that do not pertain to this ticket, and were incorrectly attached.

One file contains Postgres DDL.  The problem can be seen with the relationship between the prime table and the table 
called "sub9."

Comment 6 kidvid 2009-01-14 02:14:50 UTC
Created attachment 75798 [details]
156726:  Create Table DDL
Comment 7 kidvid 2009-01-14 02:18:26 UTC
Created attachment 75799 [details]
156726:  Generated Java files using an EJB Project
Comment 8 kidvid 2009-01-14 02:21:17 UTC
Additional comment for 156726.  The example files also display a problem that was mentioned in ticket 156307, which 
pertains to the EJB generator not picking up on all of the columns when creating the get methods.

Observe in the attached zip file that even though the EBJ Generator attempted to insert the relationships, the getters 
for these foreign keys are not present event though the "include related tables" checkbox was checked, and all tables 
in the schema were highlighted.
Comment 9 kidvid 2009-01-15 15:44:35 UTC
I did some more testing with this, and the reason that I couldn't do a find operation on the bean didn't have to do with
the @EmbeddedId annotation.  It had to do with a bad data mapping created by the Entity class generator.  I'll create a
separate bug ticket for that problem.

So, I'm recommending we treat this ticket as an enhancement request.

When generating Entity Classes from database teams, give the user the option to use the "@IdClass" annotation style or
the "@EmbeddedId" annotation style.  Developers seem to have strong preferences one way or another when it comes to this.


Comment 10 Rochelle Raccah 2009-01-15 21:04:34 UTC
FYI - EmbeddedId is the recommended approach by most folks (according to a colleague - including Gavin King), so the
most to do here would be to introduce a choice for the user and leave the default as is.

The spec says something like 'you can also use IdClass if you are moving from CMP'.  There may be a bug in Hibernate
itself causing this...
Comment 11 Martin Balin 2016-07-07 08:56:09 UTC
This old bug may not be relevant anymore. If you can still reproduce it in 8.2 development builds please reopen this issue.

Thanks for your cooperation,
NetBeans IDE 8.2 Release Boss