0

Using Hibernate, I am trying to implement a one-to-one relationship between one fixed domain type A and another domain type B whose type be any type which implements a certain interface.

Situation:

The system has three distinct domain types; Country, Warehouse and Store, which implement an interface FlagSubscriber.

I would like to join FlagSubscribers with Flags. Each Flag can be paired with 0 or 1 FlagSubscribers.

Implementation:

In the database I have tables for Country, Warehouse, Store and Flag which each have an ID column and various other columns for their specific attributes. In order to pair Flags with FlagSubscribers I have created a link table which looks like this:

  • flag_id
  • subscriber_id
  • subscriber_type

The id fields are self-explanatory, the subscriber_type is a text field which determines whether the subscriber_id relates to a Country, Warehouse or Store.

Question:

Using Hibernate annotations, how do I tell Hibernate how to map FlagSubscribers in the Flag class?

I presume I need to use discriminators, but I am not exactly sure how to do this for my specific case, or even if it is possible.

Any pointers to examples, suggestions or ideas would be most welcome!

SlappyTheFish
  • 2,344
  • 3
  • 34
  • 41

2 Answers2

1

Perhaps it can be mapped using @Any annotation, though I'm not sure how would it play with a link table. However, since relationship between Flag and FlagSubscriber is one-to-one, you can map it without link table.

Also note that it wouldn't work since Hibernate 3.5 if Hibernate is used via EntityManager interface.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • Thanks - from the example this looks almost exactly what I want. The major difference in my case is that the type and ID of the linked object is stored in an external table, so I would haver to link that is somehow, but I am not sure how. – SlappyTheFish Apr 21 '11 at 12:30
  • @SlappyTheFish: Have you checked what happens if you add `@JoinTable` to the example? Besides that, you can always model link table as a separate entity. – axtavt Apr 21 '11 at 12:43
  • In the end, I merged the subscriber_id and subscriber_type columns into the Flag table and then dropped the FlagSubscribers table, so the example in the link you posted worked perfectly - thanks! With regards to the linking, I found this answer http://stackoverflow.com/questions/217831/how-to-use-hibernate-any-related-annotations/217880#217880 which looks as if it should work. – SlappyTheFish Apr 21 '11 at 15:01
0

I would go for another setup:

No interface FlagSubscriber but an (abstract) class with 3 subsclasses: Store, Warehouse and Country. All common fields may be moved to FlagSubscriber but I assume this might be limited to

@ManyToOne private Flag flag;

You could then opt to have a separate table FlagSubscriber which will contain the subscriber_id and a foreign key to the Table Flag. Each subclass of FlagSubscriber would also have it's own table with the same id. No need for discriminators then since hibernate will join with all subclasses to know which type (subclass) it actually is (on id it will only be able to success.

Stijn Geukens
  • 15,454
  • 8
  • 66
  • 101