Hibernate version:
Hibernate Core 3.2.CR2
Annotations 3.2.CR1
EntityManager 3.2 CR1
Hi guys,
this is more of a principal question on how to design my mapping. I've did the same scenario using plain old JDBC already and wanted to use this as a first test of the EJB3 stuff.
I want to model a group containing contacts.
I've got three DB tables:
- Contact (contact_id, firstname, lastname,...)
- Group(group_id, name, ...);
- Members(group_id,contact_id)
The contact and group tables contain a lot more attributes of course. For this reason, I created two subset classes of Contact and Group:
- ContactPartial
- GroupPartial
They both contain only a small subset of the information needed. Only if I need more, I want to retrieve the whole Contact or Group object from the DB.
Only the Contact and Group classes are entities. Both partial classes are retrieved whenever I need only a subset of the information.
My GroupPartial object manages a list of ContactPartial objects.
@OneToMany(fetch = FetchType.LAZY, targetEntity = ContactPartial.class)
@JoinTable(
name="MEMBERS",
joinColumns = { @JoinColumn( name="GROUP_ID") },
inverseJoinColumns = @JoinColumn( name="CONTACT_ID")
)
private List<IContactPartial> members = new ArrayList<IContactPartial>();
Problem is of course, that this is not possible as long the ContactPartial is not an entity of itself.
But, this is not as I want it! I want the ContactPartial "reuse" the data from the DB table of Contact.
I tried to make them use the same table and column names (using @Table(name="CONTACT")), but this throws me error messages.
Is there a way of mapping this behaviour? I would rather prefer do define an elegant mapping, instead of handcoding my queries.
Many thanks in advance,
-Frederik
|