File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate Mapping problem because no primary key in database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Mapping problem because no primary key in database" Watch "Hibernate Mapping problem because no primary key in database" New topic
Author

Hibernate Mapping problem because no primary key in database

Ehrenfrids Murzello
Greenhorn

Joined: Oct 06, 2005
Posts: 7
I have a table which has no primary key. How to create the hibernate mapping xml without using the generator id
Edwin Keeton
Ranch Hand

Joined: Jul 10, 2002
Posts: 214

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?


SCJP, SCWCD
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"Ehrenfrids"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Originally posted by Ehrenfrids:
I have a table which has no primary key. How to create the hibernate mapping xml without using the generator id


How are you accessing records in that table when not using Hibernate? i.e. when you use SQL directly.
Leticia Barbalho
Ranch Hand

Joined: Sep 02, 2005
Posts: 37
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.
Leticia Barbalho
Ranch Hand

Joined: Sep 02, 2005
Posts: 37
Now, that's for a key not using the generator! For generating a value, you would have to create a field in the DB and use is as your PK.
Ionescu Victor
Greenhorn

Joined: Jan 07, 2004
Posts: 1
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?


Stupid is who stupid does.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.
Gagan Bajpai
Greenhorn

Joined: Aug 20, 2006
Posts: 2
Well, I've a legacy database. I don't care to have a IF/Primary key. is there a way? or Hibernate just takes away that option?
Gagan Bajpai
Greenhorn

Joined: Aug 20, 2006
Posts: 2
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.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


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.
Juan David Zuluaga
Greenhorn

Joined: Sep 10, 2009
Posts: 1
As the table has no primary key, it should not be mapped as an entity.

You could use a named query to insert data instead of declaring an entity, and perform the insert from the dao.


Deepak JavaEtc
Greenhorn

Joined: Apr 04, 2012
Posts: 2
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate Mapping problem because no primary key in database
 
Similar Threads
Two PKs
Hibernate Query Question
OJB - Mapping a table w/o Primary Key
Hibernate: is it necessary to have primary key in table
composite primary keys