This week's book giveaway is in the Agile/Processes forum.
We're giving away four copies of Building Green Software: A Sustainable Approach to Software Development and Operations and have Anne Currie, Sarah Hsu , Sara Bergman on-line!
See this thread for details.
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

entity bean for a table with No Primary key : Please Help,,,, URGENT

 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have to create a CMP for a table with no primary key but which has a foreign key. Has any one done this ?
I am using Websphere and WSAD.. If anyone has done this ... can someone help me
Thanks
[ October 03, 2002: Message edited by: maya menon ]
 
Ranch Hand
Posts: 2713
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I won't go into how it is bad design to not have primary keys on database tables...
Regardless, an Entity Bean must have a primary key. That is not to say that an Entity must correspond to exactly one row in a table (although with CMP this is the case). If there is no possible PK for those rows than you really have no choice but BMP (PK'ed on the FK) or a Session Bean.
 
Shreya Menon
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chris,
I dont agree with the fact that you cant create an EJB with No primary Key..
Please find these, I got these from JGURU.
But I am not sure how to create a bean without a primary key for websphere..
If any one has done it pols let me know

--------------------------------------------------
Entity Beans with no primary keys Is it possible to create an Entity Bean(CMP) based on table that does not have a primary key.
Derived from A question posed by Pankaj Jha
Topics Java:API:EJB:EntityBean:CMP, Java:API:EJB:EntityBean rimary Keys
Author Shirish Poddar
Created Jun 3, 2001

Answer
Yes, you can create CMP Entity Beans without primary keys. But you must be aware of the implications:
Duplicate records may be entered in the table
The findByPrimaryKey() method may return varying rows
In essence, CMP was not designed to not have primary keys, although it can work. The development process is the same as when you create CMP which has primary keys.
Is this item helpful? yes no Previous votes Yes: 0 No: 0


Comments and alternative answers

Entity Beans with no primary keys Is it possible to create an Entity Bean(CMP) based on table that does not have a primary key.
Allen Fogleson, Jun 3, 2001 [replies:1]
You can create a custom Primary key class that contains all the columns in the table. (assuming that would be fairly unique) The only hassles there would be in implementing the hashcode method.
Is this item helpful? yes no Previous votes Yes: 0 No: 0



Re: Entity Beans with no primary keys Is it possible to create an Entity Bean(CMP) based on table that does not have a primary key.
xxxxx yyyyy, Feb 14, 2002
I have created an entity bean for a table which does not have a primary key and it does not work. There is nothing stops you creating an entity bean(CMP) for a table which does not have a primary key but the result return by the finder method is wrong. Though the table does not have primary key, the CMP bean randomly picks a column(most likey the first column) out of a table and mkes it a primary key (which is absolutely wrong). Conclusion: A CMP entity bean for a table which does not have a primary key is not going to work. You must have a primary key to work properly.
Is this item helpful? yes no Previous votes Yes: 0 No: 0



No Primary Key Class in Entity Beans
Shirish Poddar, Jun 9, 2001 [replies:1]
Generally we create a separate primary key class in a Enity Bean. We can do without this creation also. What we can do is we have to write in the EJB-Jar.XML the primary key class name as Java predefined class, such as java.lang.string,any wrapper classes. <prim-key-class>java.lang.String</prim-key-class> <primkey-field>accountId</primkey-field> In this case Duplicate recrods will not be allowed.
Is this item helpful? yes no Previous votes Yes: 0 No: 0



Re: No Primary Key Class in Entity Beans
Allen Fogleson, Jun 14, 2001
But in this case you are saying that the acountID is the primary key... So effectively you have made the table have a "primary key". I think in general we have to be aware that although primary keys may not be required, the detriment to not having at least one unique field is that finders may not operate as expected.
Is this item helpful? yes no Previous votes Yes: 0 No: 0



Yes it is possible
prashant jani PREMIUM, Jan 18, 2002 [replies:1]
in the cmp xml file. Do not provide CMp mapping for the primary key.
Thats all
but u will have to create a sequence at the database level for the primary key.
this will avoid duplicate keys problem

------------------------------------------------
Thanks
 
Chris Mathews
Ranch Hand
Posts: 2713
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point is that you must still define a Primary Key for the Entity Bean. There is no way to get around that. If this PK is not unique in the database then you are just asking for problems.
I stand by my assertion.
 
Shreya Menon
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok,
But our data model is existing for years and now there is no way it will be changed...
Has any body done it ? Please help me
Thanksweb page
 
Chris Mathews
Ranch Hand
Posts: 2713
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is to not use an Entity Bean because it is not a logical fit for your situation. Use a Session Bean instead.
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A fundamental thing about Entity Beans is that they have an identity. That identity is represented by the Primary Key class.
A Primary Key should uniquely identify one individual Entity Bean.
The Entity Beans primary key is usually the same as the primary key used by the database. But that is not required. So long as you can construct an object that uniquely identifies the entity and can be used to reliably find the entity, then it can be the PK.
For example, your database table for USER might have a generated sequence number as the PK column, but that doesn't make sense as an Object. So maybe you would use the "USERNAME" column or "FIRSTNAME" + "LASTNAME" or "EMPLOYEE_NUMBER" as the Entity Bean's PK.
If you do this, there should be some constraint in the database (or in the business logic) to ensure that the things you are using for the Entity PK are unique. In a database, you would usually do this with some uniqueness constraint. But the Entity Bean does not really care - as long as you are assured that the primary key is unique by some means.
Note that an Entity Bean is not required to be backed by a database at all. You could have an Entity Bean backed by LDAP or PeopleSoft or calculated data or random numbers. But whatever it is it will still need an identity.
If your data does not meet this identity requirement, then you will probably want to consider somthing like Stateless Session Bean for data access. But if you can construct a SQL query that will never return more than ONE row from the database, then the things in that query will make up your entity's PK.
[ October 03, 2002: Message edited by: Dave Landers ]
 
Shreya Menon
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave,
Thanks for the respone.
Can you give me an example of this ??
Maya
 
reply
    Bookmark Topic Watch Topic
  • New Topic