Primary Key in Entity Bean

Discussions

EJB programming & troubleshooting: Primary Key in Entity Bean

  1. Primary Key in Entity Bean (8 messages)

    is it must to have a findByPrimaryKey in all EntityBean.

    Threaded Messages (8)

  2. Yes. It might be possible that you, directly never use it, but the container will use it.
  3. What if the table does not have one ?
  4. Yep. You would still have one. The container uses it to help to uniquely identify your bean. An entity bean has to have a primary key (even if it doesn't map to a primary key in your database). Since your entity has to have a primary key the container has to have a way of returning it - and thus the requirement of findByPrimaryKey().
  5. Thanks Larry.

    I understand what you say. But if there is a table which has 'n' no. of columns in it, and neither of them is a primary key and, for that matter, not even with 'NOT-NULL' constraint. What you would be the correct ( or would anything be 'correct' ) logic for the findByPrimaryKey in a Bean-managed Entity Bean.

    I appreciate your help.
  6. Primary Key in Entity Bean[ Go to top ]

    Well if a database table doesnot have a primarykey...it can be because of two reasons.. either it is a weak entity set... in which case it would sport a foreign key or it is a reference set, in which case entitybean-per-row is not an advisable approcah... model the eb on a relationship... rather than a row on the table... i could give a relevant example if you hint the nature or use of this table

    cheers
    Adi
  7. Yeah, Anand made some good points.

    The Bean's primary key should protect against duplicating your bean. If your bean maps to a row in your table then you don't want your bean duplicating that row. Even without a primary-key on your table, there should still be something about your table that makes a row unique. Maybe it's a set of fields or all feilds. If this is the case then perhaps you need to implement your bean with a compound primary key. Your bean's primary key does not have to map to one particular field. Should it map to one field, that field does not have to be marked as the primary-key on the table - but your bean will act as if it was..

    Yeah, I'd be curious to know how you're using this table. Should your entity map to a particular row? Are you trying to do some kind of composite or complex entity that holds a collection of rows in another table?
  8. I think I have got what I want to know.

    Now, here is the situation.
    The table contains historical data, the unique value is
    albumid and timestamp. but for reason unknown, it is created without any constraint.

    So, here is what I am going to do. I would have a compound pk and my Pk class would have albumid and timestamp. which I may not be using at anytime, but will be used by the container.. am I right.

    Larry & Anand, Thanks for your help.
  9. Yes, you would be correct.