Hibernate bit array to entity mapping
Posted
by teabot
on Stack Overflow
See other posts from Stack Overflow
or by teabot
Published on 2010-04-26T14:00:30Z
Indexed on
2010/04/26
14:03 UTC
Read the original article
Hit count: 413
I am trying to map a normalized Java model to a legacy database schema using Hibernate 3.5. One particular table encodes a foreign keys in a one-to-many relationship as a bit array column.
Consider tables 'person' and 'clubs' that describes people's affiliations to 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 is clear that person.id
is used as the bit index in the bit array club.members
. In this example the members
column tells us that:
- no one is a member of Cricket,
- Bob/Sue -> Tennis,
- Bob/Sue/Joe -> Cooking and
- Sue -> Golf.
In my Java domain I'd like to declare this with entities like so:
class Person {
private int id;
private String name;
...
}
class Club {
private Set<Person> members;
private int id;
private String name;
...
}
I am assuming that I must use a UserType
implementation but have been unable to find any examples where the items described by the user type are references to entities - not literal field values - or composites thereof. Additionally I am aware that I'll have to consider how the person
entities are fetched when a club
instance is loaded.
Can anyone tell me how I can tame this legacy schema with Hibernate?
© Stack Overflow or respective owner