tags:

views:

808

answers:

1

I have two related tables, MANY and ONE. An integer ONE_ID column on the MANY table formalises a many-to-one relationship.

I want to map to Many and One domain objects and to have Hibernate generate the DDL including a foreign key constraint on MANY.ONE_ID. But I want a simple property getter and setter pair ("int getOneId() / void setOneId(int)") on the Many domain object rather than the association methods that Hibernate would ordinarily expect me to provide ("One getOne() / void setOne(One)"). Is this possible?

I'll try and explain why I want this: I actually have hundreds of such many to one associations for which I want the FK constraint but that I'll never need to navigate. The hibernate mapping file and the domain objects are to be created by code generation. All I need to do at runtime is to be able to read the FK property from the 'many' side object and to set it; I never need to actually navigate the many to one association. Because of the code generation issues it would greatly simplify things if I could have simple property based accessors for the FK field.

A: 

AFAIK, you have two options:

  • Declare the mapping normally, and make the association lazy. This way you won't pay for what you don't use. You'll also want to carefully choose the owner of the association. Addittionaly, you specify a Long property and map it to the FK.
  • Don't rely on hibernate to generate the ddl/schema. Map the tables as you'd originally like (no association, just long types).
Miguel Ping
Thanks. I'm going with the second option, and using a Hibernate <database-object> element to add an FK constraint to each table. Since the HBM file is generated this is easy enough to do.
David Easley