Hi !
So here's the deal:
I have 3 tables in SQL .... Nationality , Student and Nationality_Student.
In nationality table i have 3 records like (italian , french , german ).
When i insert a student info i set his nationaliaties through a HashSet in java.
The problem is that the student is inserted correctly (like his first name , last name , date of birth , etc ) except his nationalities!!! The table Nationality_Student remains empty!! So no relation is made........!! That table only has two keys "Id" (Identity field in student) and "Code" (varchar(3) pk in nationality).
Here's my SQL code:
Code:
SET IDENTITY_INSERT dbo.StudentData_Nationality_AR ON
if object_id('Nationality') is not null
drop table Nationality
create table Nationality(
Code varchar(3) not null,
primary key(code),
);
if object_id('StudentData') is not null
drop table StudentData
create table StudentData(
Id int NOT NULL IDENTITY ,
FamilyNames varchar(40) not null,
GivenNames varchar(40) not null ,
DateOfBirth datetime not null,
IdentificationNumber int,
PassportNumber int,
check(IdentificationNumber>0 Or IdentificationNumber>0),
primary key (id),
);
if object_id('StudentData_Nationality_AR') is not null
drop table StudentData_Nationality_AR
create table StudentData_Nationality_AR(
Id int not null IDENTITY ,
Code varchar(3) not null,
primary key(Id,code),
foreign key(id) references StudentData(Id),
foreign key(code) references Nationality(code),
);
And here's the mapping of Student in hibernate:
Code:
<class name="Components_src.StudentData" table="StudentData" schema="dbo" catalog="BAS">
<id name="id" type="java.lang.Integer">
<column name="Id" />
<generator class="identity" />
</id>
(...)
<set name="nationalities" cascade="all" inverse="true" table="StudentData_Nationality_AR" schema="dbo" catalog="BAS">
<key>
<column name="Id" not-null="true" />
</key>
<many-to-many entity-name="Components_src.Nationality" >
<column name="Code" length="3" not-null="true" />
</many-to-many>
</set>
And here's the Nationality mapping
Code:
<class name="Components_src.Nationality" table="Nationality" schema="dbo" catalog="BAS">
<id name="code" type="java.lang.String">
<column name="Code" length="3" />
<generator class="assigned"/>
</id>
<set name="studentDatas" cascade="all" inverse="true" table="StudentData_Nationality_AR" schema="dbo" catalog="BAS">
<key>
<column name="Code" length="3" not-null="true" />
</key>
<many-to-many entity-name="Components_src.StudentData" >
<column name="Id" not-null="true" />
</many-to-many>
</set>
</class>
I am going crazy over this....just cant understand it :(
Any help would be very appreciated!
Thank you!
Jorge