Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Id field is a PK but is also a FK pointing towards a PK of a main table  RSS feed

 
Ken Loh
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gurus,

I'm a newbie to Entity Beans.

In my database, there are various tables which their ID columns derive their values from a main table's ID column. These ID columns are all primary keys to their respective tables. This also means that apart from the main table, all the ID columns are also foreign keys.

The ID column of the main table is auto-incremented. This relationship ensure that no records in the various non-main tables have the same IDs.

My problem is, how could I set up my associated entity beans of various classes to be created such that their IDs (which are PKs) are derived from the main table's auto-incremented ID column ?

Thanking you in advance,
Newbie to Entity Bean.
 
Balaji Akella
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constraints are implemented on the database side. Since a primay key - foreign key relationship is already defined on the database, any attempts to do an insert, with invalid data would result in CreateException being thrown.

However, if you are keen on implementing this feature from the bean code, then from the ejbCreate of the non-main table, invoke the find by primary key method on the main table - using the new key. If null is getting returned, it indicates that corresponding primary key is missing - CreateException can be thrown.

Swamy
 
Ken Loh
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I think I've not been successful in conveying what I mean to you.

A record in any non-main tables are created at the same time (or rather immediately in the same transaction right after) with the one in the main table. The ID of the new record in the non-main table, takes on the one (which is also new) in the main table. The ID of the new record in the main table is auto-incremented from the most previous one.

Originally posted by Swaminath Akella:
The constraints are implemented on the database side. Since a primay key - foreign key relationship is already defined on the database, any attempts to do an insert, with invalid data would result in CreateException being thrown.

However, if you are keen on implementing this feature from the bean code, then from the ejbCreate of the non-main table, invoke the find by primary key method on the main table - using the new key. If null is getting returned, it indicates that corresponding primary key is missing - CreateException can be thrown.

Swamy

[ February 18, 2005: Message edited by: Seibernaut ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!