filmov
tv
Lesson - 62 : Hibernate - Many To Many Using idbag tag using XML and Annotation based Config

Показать описание
Hibernate – Assotations/Relationship : <idbag> Tag In Hibernate :
1. While working with many-to-many relationship a join table of relationship contains foreign keys. The foreign keys in the table contains duplicate values.
2. In order to maintain the data easily in a join table there is a need of primary key also for the join table.
3. If a primary key column is also added for a join table then we need to inform about primary key column to the hibernate by configuring <idbag> tag in the mapping file or hbm file of the hibernate.
5. If a primary key column is added for a join table then the value to that primary key column should be generated by hibernate only. A developer can not assign the primary key values for join table.
6. Under <idbag> tag, we configure <collection-id> tag to configure primary key column of join table.
7. Under <collection-id> tag the generator class should be other than assign.
Course Table :
<set name="students" cascade="all" table="STUDENTS_COURSES">
<key column = "CID" />
</set>
Student Table:
<idbag name="course" cascade="all" table="STUDENTS_COURSES">
<collection-id type="int" column="s-c-id">
<generator class="increment"/>
</collection-id>
<key column = "STUID" />
</idbag>
1. While working with many-to-many relationship a join table of relationship contains foreign keys. The foreign keys in the table contains duplicate values.
2. In order to maintain the data easily in a join table there is a need of primary key also for the join table.
3. If a primary key column is also added for a join table then we need to inform about primary key column to the hibernate by configuring <idbag> tag in the mapping file or hbm file of the hibernate.
5. If a primary key column is added for a join table then the value to that primary key column should be generated by hibernate only. A developer can not assign the primary key values for join table.
6. Under <idbag> tag, we configure <collection-id> tag to configure primary key column of join table.
7. Under <collection-id> tag the generator class should be other than assign.
Course Table :
<set name="students" cascade="all" table="STUDENTS_COURSES">
<key column = "CID" />
</set>
Student Table:
<idbag name="course" cascade="all" table="STUDENTS_COURSES">
<collection-id type="int" column="s-c-id">
<generator class="increment"/>
</collection-id>
<key column = "STUID" />
</idbag>