-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Mapping many to many -- doesnt populate the table in SQL (!)
PostPosted: Sat May 23, 2009 6:27 pm 
Newbie

Joined: Tue May 19, 2009 7:28 pm
Posts: 5
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


Top
 Profile  
 
 Post subject: Re: Mapping many to many -- doesnt populate the table in SQL (!)
PostPosted: Sat May 23, 2009 7:01 pm 
Newbie

Joined: Tue May 19, 2009 7:28 pm
Posts: 5
by the way here's my test code:

Code:
      Transaction t = daoSD.getSession().beginTransaction();
   
      
   
      java.util.HashSet<Nationality> s = new java.util.HashSet<Nationality>() ;
   
      s.add(new Nationality("por"));
      s.add(new Nationality("eng"));

      
      
      StudentData ze = new StudentData("Milho","Ze",new Timestamp(milliseconds1),12562722,0);
      ze.setNationalities(s);
   
      daoSD.save(ze);

      
      t.commit();
      daoSD.getSession().close();
      System.out.println("Done");

   }


Top
 Profile  
 
 Post subject: Re: Mapping many to many -- doesnt populate the table in SQL (!)
PostPosted: Sun May 24, 2009 9:11 am 
Newbie

Joined: Tue May 19, 2009 7:28 pm
Posts: 5
Anyone???????????????.........


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.