dilix
dilix

Reputation: 3893

Eclipse + Hibernate + Spring question

I can't figure out how to organize classes in project...

I need to show all Workers with their history of the specialties (types).

I have 3 tables in DB: Workers: id, name, address Specialty: id, description Worker_Type: id_worker, id_specialty, hire_date

I have:

class Specialty
{
    ... 
}

class Worker
{
    private List<Specialty> history;
    ...
}

How can i map Specialty(Class) to two tables (Specialty and Worker_Specialty) to get all information about worker inculding history ?

Maybe i have to create one more class SpecialtyHistory and map it to Worker_Specialty ?

Upvotes: 0

Views: 185

Answers (1)

Courtney Faulkner
Courtney Faulkner

Reputation: 2080

What you are describing is called a ternary association, or in other words, a many-to-many relationship that includes adding information on the relationship itself. There are several approaches to mapping ternary associations, one of which being creating a SpecialtyHistory class as you mentioned, treating the relationship as a first class entity. If you did not need hire_date, you could just use a normal many-to-many mapping.

The Hibernate documentation has additional information on ternary associations.

Some previous StackOverflow questions: hibernate: ternary association mapping and Ternary (and n-ary) relationships in Hibernate.

Upvotes: 5

Related Questions