Mapping question

View: New views
3 Messages — Rating Filter:   Alert me  

Mapping question

by ttomor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
This is my db schema which looks like this:


So a staff (which is a teacher) can teach a specific subject (curriculum) to a specific class (class_id).

As you can see 'teaches' is the table where all the tables meet. I've tried the Dali tool and it did a pretty good job, by producing something like this (The 'Staff' entity, not entirely quoted, just the parts which have to do with 'teaches'):

        //uni-directional many-to-many association to Curriculum
    @ManyToMany
        @JoinTable(
                        name="teaches"
                        , joinColumns={
                                @JoinColumn(name="staff_id", referencedColumnName="staff_id"),
                                }
                        , inverseJoinColumns={
                                @JoinColumn(name="class", referencedColumnName="class"),
                                @JoinColumn(name="subject_id", referencedColumnName="subject_id"),
                                @JoinColumn(name="year", referencedColumnName="year"),
                                }
                        )
        private Set<Curriculum> curriculums;

        //bi-directional many-to-many association to Class
    @ManyToMany
        @JoinTable(
                name="teaches"
                , joinColumns={
                        @JoinColumn(name="staff_id", referencedColumnName="staff_id"),
                        }
                , inverseJoinColumns={
                        @JoinColumn(name="class_id", referencedColumnName = "class_id"),
                        }
                )
        private Collection<Class> clazzs;


But this makes problem when I want to persist a new Staff object, which 'teaches' a specific subject (year, class, subject_id) to a specific class_id, because it doesn't know which class_id with which subject go together. Could you please help me with a better solution for this?

Help is appreciated

Sincerely yours,
tomor

Re: Mapping question

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You need to make an Entity for the Teaches table.  Some instead of a ManyToMany use a OneToMany,

see,

http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns

Re: Mapping question

by ttomor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you very much.

This seems to be the solution.

Tomor