• Post Reply Bookmark Topic Watch Topic
  • New Topic

Composite keys and annotations with Hibernate

 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

I've created three tables in my database

--Schools
idnum bigint unsigned NOT NULL PRIMARY KEY,
name varchar(30)
PRIMARY KEY (idnum)

--Districts
idnum bigint unsigned NOT NULL,
name varchar(30)
PRIMARY KEY (idnum)

--DistrictSchoolRelationship
idnum_district bigint unsigned NOT NULL,
idnum_school bigint unsigned NOT NULL,
PRIMARY KEY ( idnum_district, idnum_school)

(yes I know this is a classic one-to-many relationship and there's other ways to structure the tables but I like this method)

There's an example of *.hbm.xml files using the <composite-id> property in topic 1022460, but (of course) I'm using annotations in my classes instead of *.hbm.xml files.
I've successfully created corresponding classes School and District in my Eclipse/Java/Hibernate project but am having trouble creating class DistrictSchoolRelationship - how can I get Hibernate to understand about a concatenated key within DistrictSchoolRelationship entity?

TIA,

Still-learning Stuart
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've made some progress based on what googling turned up here:

http://boris.kirzner.info/blog/archives/2008/07/19/hibernate-annotations-the-many-to-many-association-with-composite-key/

The trick seems to be to create one additional class, an Embeddable class that describes the concatenated key.

But what must be done after the classes are created? Since my app makes use of DAOs do I need to make a DAO/DAOHibernate
for the embeddable class also?


Still-learning Stuart
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!