• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate / Composite primary key / Annotations issue

 
bmpro
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Let's say you have a table NbItems with the columns year,month,nb.
There is NO primary key but using year/month, each row is unique.
So for example, if I want to know how many items I had for the year 2007 and month 4, "select * from NbItems where year='2007' and month='4'" returns ONLY one row.

My question is: Is there a way in Hibernate to map each row to an object?

(It is not possible for me to change the database and add a primary key)

Basically I don't want to use the @Id annotation when I define an entity bean. So far, the only thing I found out is that I need an primary key.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"bmpro", please check your private messages (click on My Profile) for an important message from me.
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hibernate does support composite primary keys, see in Hibernate reference.
For the annotation syntax, you can download the Java Persistence specification.
 
Bougnon Kipre
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you code or/and define a composite id in your hbm?

If you did did, you could code that in your POJO equals method.

However, I am not sure where that leaves the database primary key.

Another idea you might want to explore is to add year and month properties to

the current database primary key (in your code only, do not change the

database key specs) and code this as your new composite id in your hbm and

POJO equals.

This of course, if the new composite id satisfies your need.

[ September 14, 2007: Message edited by: Bougnon Kipre ]
[ September 14, 2007: Message edited by: Bougnon Kipre ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a composite key feature, you probably will need to use the Hibernate Annotations instead of JPA's Annotation, both are within the jar files that comes with Hibernate.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic