• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic