aspose file tools
The moose likes Object Relational Mapping and the fly likes Hibernate supports for PK Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Hibernate supports for PK" Watch "Hibernate supports for PK" New topic
Author

Hibernate supports for PK

Charu Sama
Greenhorn

Joined: Aug 03, 2006
Posts: 12
I am working on a Hibernate Project and some of my database tables does not have Primary Key. I looked into Hibernate reference and could found how to handle a table which does not have PK on it. So, I am wondering if Hibernate supports table mapping without PK ???
Has anyone come across this situation ???
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Data in a database defined as an entity without a primary key is not relational data. So you will have problems using Hibernate (or any other ORM) with such data. The only fix is to correct your invalid entity relational model so everything has a primary key, ideally by adding surrogate keys.

If changing the ER model is not possible, the only workaround is to map your entities without primary keys so they have composite keys containing every attribute in the entity.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 504

Originally posted by Paul Sturrock:
Data in a database defined as an entity without a primary key is not relational data. So you will have problems using Hibernate (or any other ORM) with such data. The only fix is to correct your invalid entity relational model so everything has a primary key, ideally by adding surrogate keys.

If changing the ER model is not possible, the only workaround is to map your entities without primary keys so they have composite keys containing every attribute in the entity.



Adding to it,
If your table(Table I) has a one-to-one mapping with another table(Table II), probably you can map the use the primary key's value of Table I as a foreign(but mapped like a primary key) in table II.

Eg;
Consider Table I and Table II.

Table I is an unique entity and can stand on its own. Table II is also unique but it always has a one-to-one relationship with table I. Table II is never related to any other model, apart from a business rule which says if table II is accessed it is always accessed with Table I.

Here it is upto you to decide whether you need to have a surrogate key for Table II, which doesn't have any business/Front end significance and just for facilitating the use of hibernate.

On the other side,
you can define something like this on your Table II.hbm file,





But, each of these approaches has it own advantages and disadvantages. You need to decide which suits your business best.


If you are not laughing at yourself, then you just didn't get the joke.
 
 
subject: Hibernate supports for PK
 
Threads others viewed
how to insert a row with FK ?
hibernate table mapping
Two PKs
multiple composit key
OJB - Mapping a table w/o Primary Key
IntelliJ Java IDE