Every relational table has a "natural" primary key. However, it may be unrecognized and undefined. The best solution to your problem then is to determine what the primary key is and define it as such in your RDBMS.
Having said that, it is better to use a surrogate key for the primary key to protect against business semantics changes in the natural key. So why not use a generated id?
Edwin Keeton's suggestion is good. Using a surrogate makes perfect sense; fix the invalid data model so it truly represents relational data.
However, if you cannot change the data model, you can map the entire table as one big composite key. A horrible solution, but a work around which gives you time to fire the DBA who modelled your data this way, and hire a competent replacement.
Well, when you don't have a primary key and want to access a specific row, I suppose your query will contain all fields, right? So, as weird as it may seem, you could use a composite-key with all fields in it.
The problem might not be so simple to adress. Suppose I work with a legacy database and I have a readonly table (imported from a back-office) and I don't want to access individual rows in that table (I want just to list the records obtained form a join). So: I can't add surogate key (I can't change the database) and I don't have a natural (business) key. What about that?
Originally posted by Ionescu Victor: The problem might not be so simple to adress. Suppose I work with a legacy database and I have a readonly table (imported from a back-office) and I don't want to access individual rows in that table (I want just to list the records obtained form a join). So: I can't add surogate key (I can't change the database) and I don't have a natural (business) key. What about that?
See my earlier answer. That's the only work around you have available to you.
I tried using a big composite key. Hibernate generates a column on MySql.In development, may be I can live with that, but not in production. I'm no expert on Hibernate but this should be easy.
I'm no expert on Hibernate but this should be easy
An Object-Relational Mapping tool can only be expected to map relational data. Data without a primary key it is not relational, so it should be no surprise it doesn't work.
ORM is to map relations through primary and foreign key to database. If you are not having those on tables then you can use, criteria search with HQL or SQL and populated fetched data into related objects. These objects can be used for further. Example can be found at http://www.deepakgaikwad.net/?p=448