Okay, after few test I think I'm finally getting somewhere. Not ready to post the entire solution, though.
I've done as You advised (created the POJO for my primary key, corrected the mapping for my ExercisesSet) and my basic CRUD test went OK.
Now, I have a theoretical question (maybe a little off-topic). Since I am creating my PK as composite-id, I am now obliged to assign the necessary FKs myself, right?
What I mean is:
when I have added ExercisesSetPK (my PK class) to ExercisesSet as a private member and corrected the mapping, I received exceptions errors that members of ExercisesSetPK haven't been set. This makes sense, cause I didn't specify any separate mapping for my PK so Hibernate doesn't know were to fetch data from. I'm cool with that :-)
Hibernate also complained that my mapping should include
insert="false" update="false" properties, so I have added them as well to my
many-to-one tags. After that all worked fine.
Coming back to my previous thought... Since Hibernate asked me to set the values of ExercisesSetPK member before I save my ExerciseSet entity, the only place of doing that which occurred to me was inside my ExerciseSet constructor. That way I will be able to set all FKs when I create new man-to-many relationship. So I have modified my constructor:
Code:
public ExercisesSet(UserTraining training, Exercise exercise, Constant day) {
this.training = training;
this.exercise = exercise;
this.day = day;
// Setting primary keys properties
id = new ExercisesSetPK(training.getId(), exercise.getId(), day.getId());
}
This seems to work al right, but (finaly!) it made to think...
Since ExercisesSet is a base class for my
business logic, and a ExercisesSetPK is only being used by
Hibernate layer, doesn't that part of code bring those two separate layers too close together?
I mean, I try to make this project as flexible as possible. Maybe in two years from now I would like to get rid of Hibernate and use something else instead? But in this particular class I will have this unnecessary part of code which is related only to the Hibernate and probably I won't be needing that if I decide to jump over to some other ORM engine.
You catch my drift? (is that how You say it in English ?:-) )
Best regards,
Bat
P.S. Maybe this is totally irrelevant from the standpoint of regular Hibernate user, but I really try to learn this engine best, so I need to understand the things I'm doing.