Hibernate bit for entity mapping

I am trying to map the Java normalized model to the legacy database schema using Hibernate 3.5. One particular table encodes foreign keys on a one-to-many basis as a column of a bit array.

Consider the tables person

and club

that describe how people join clubs:

person: .----.------.    club: .----.---------.---------------------------.
        | id | name |          | id |   name  | members | binary(members) |
        |----+------|          |----+---------|---------+-----------------|
        |  1 | Bob  |          | 10 | Cricket |       0 |             000 |
        |  2 | Joe  |          | 11 | Tennis  |       5 |             101 | 
        |  3 | Sue  |          | 12 | Cooking |       7 |             111 |   
        '----'------'          | 13 | Golf    |       3 |             100 |
                               '----'---------'---------'-----------------'

      

So hopefully it's clear what is person.id

used as the bit index in the bit array club.members

:

.---.---.---.
| S | J | B |
| u | o | o |
| e | e | b |
|---+---+---|
| 1 | 0 | 1 |
'---'---'---'

      

In this example, the column members

tells us that:

  • Nobody enters Cricket --- no flags set
  • Bob / Sue → Tennis --- flags in positions 1 and 3 are set
  • Bob / Sue / Joe → Cooking --- flags at positions 1, 2 and 3 are set
  • Sue → Golf --- flag in position 3 is set

Now, a join table could be used for this example, which would simplify things and avoid many potential problems - for example: a maximum range members

that sets an upper bound on the number of rows people

. However, I am stuck with this circuit and it seems that there are factors in favor of using a bit array column back when.

In my Java domain, I would like to simulate this schema with objects like this:

class Person {
    private int id;
    private String name;
    ...
}

class Club {
    private Set<Person> members;
    private int id;
    private String name;
    ...
}

      

I am guessing that I should use the implementation UserType

, but could not find any examples where the items described by the user type are entity references rather than literal field values ​​or their composites. Also, I know I will need to consider how the objects person

are retrieved when the instance is loaded club

.

Can anyone tell me how I can tame this legacy schema using Hibernate?

Update

I recently had to revisit this display type in an old database. This time, it became apparent that our table equivalent members

was actually a static set and could be hard-coded as Enum

. With this simplification, it was quite easy to implement Hibernate UserType

converting between a bitmap and a set of enums.

+2


a source to share


1 answer


I have never encountered such a situation, but I think you need to implement a custom one UserCollectionType

(see chapter 5.2.3. Types in the Hibernate Core documentation), UserCollectionType

this is an extension point that can be used to support any damn collection and collection semantics that you like.

I'm not sure how well they are supported by annotations though (as per HHH-4417 , you may have to use a hack). Using hbm.xml

for this would be a good idea here IMO.



Some more pointers / discussions:

+1


a source







All Articles