0

I have two tables and I wand to create a junction table. I have this mapping.

public UsertMap()
    {
        Table("Users");
        Id(x => x.Id, map => map.Generator(Generators.Assigned));
        Property(x => x.FirstName, map => map.NotNullable(true));
        Property(x => x.LastName, map => map.NotNullable(true));
        Property(x => x.Role, map => map.NotNullable(true));
        Bag(x => x.Projects, map =>
        {
            map.Table("UsersInProject");
            map.Cascade(Cascade.None);
            map.Access(Accessor.Field);
            map.Key(u => u.Column("UserId")); 
            }, a => a.ManyToMany(x => x.Column("ProjectId")));
    }

and

public ProjectMap()
    {
        Table("Projects");
        Id(x => x.Id, map => map.Generator(Generators.Assigned));
        Property(x => x.SubmissionDate, map =>map.NotNullable(true));
        Property(x => x.QuotesubmissionDate, map => map.NotNullable(true));
        Bag(x => x.ProjectUsers, map =>
        {
            map.Table("UsersInProject");
            map.Cascade(Cascade.None);
            map.Access(Accessor.Field);
            map.Key(u => u.Column("ProjectId"));
        }, a => a.ManyToMany(x => x.Column("UserId")));
    }

It created the junction table but without setting as primary key*, the composite key {ProjectId , UserId}

What am I doing wrong?

Thanx in advance.

*It was strange for me to understand it also when i was told of this about me code. The created table is created by this sql code:

CREATE TABLE "UsersInProject"
(
  "ProjectId" uuid NOT NULL,
  "UserId" uuid NOT NULL,
  CONSTRAINT fk28998796db607aa2 FOREIGN KEY ("UserId")
      REFERENCES "Users" ("Id") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk28998796f9a8d344 FOREIGN KEY ("ProjectId")
      REFERENCES "Projects" ("Id") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE "UsersInProject"
  OWNER TO test;

I would expece also a line like the next in there:

CONSTRAINT pk PRIMARY KEY ("ProjectId" , "UserId" ),

edit2: (for a better view)

drop table if exists "UsersInProject" cascade;
create table "UsersInProject" (
        "ProjectId" uuid not null,
       "UserId" uuid not null
    );
    alter table "UsersInProject" 
        add constraint FK28998796DB607AA2 
        foreign key ("UserId") 
        references "Users";

    alter table "UsersInProject" 
        add constraint FK28998796F9A8D344 
        foreign key ("ProjectId") 
        references "Projects";
Turambar
  • 293
  • 1
  • 3
  • 8
  • What does this mean: "but without setting as primary key, the composite key {ProjectId , UserId}"? – Stefan Steinegger Dec 05 '12 at 10:58
  • I edited to answer your question. – Turambar Dec 05 '12 at 13:09
  • And what's the question? – Stefan Steinegger Dec 05 '12 at 14:38
  • The create table script you show here is not created by NHibernate. It is most probably a hand written SQL script. – Stefan Steinegger Dec 05 '12 at 14:54
  • Yeah of course it's not. The above code does this: drop table if exists "UsersInProject" cascade; create table "UsersInProject" ( "ProjectId" uuid not null, "UserId" uuid not null ); alter table "UsersInProject" add constraint FK28998796DB607AA2 foreign key ("UserId") references "Users"; alter table "UsersInProject" add constraint FK28998796F9A8D344 foreign key ("ProjectId") references "Projects"; – Turambar Dec 05 '12 at 15:18
  • And *what exactly* is the question now? – Stefan Steinegger Dec 06 '12 at 07:02
  • How the nHibernate will set the primary Key to the wanted Composite Key. – Turambar Dec 06 '12 at 08:06
  • But you write the script yourself - so just create the primary key if you want it. Why do you need NH to do anything? – Stefan Steinegger Dec 06 '12 at 12:25
  • If it is not possible by mapping by code of Conform, it's another story, I'll figure out what to do then. Add hoc editing the map-created schema doesn't seem the best solution to me, even though it does seem easy and obvious. But after my research i doubt that you can create any composite primary key in Conform anyway... – Turambar Dec 06 '12 at 14:44

1 Answers1

0

I came to the conclusion that in order to have a composite primary key in nHibernate you'll have to use another column to be used for the update of the row in the database by the nHibernate.

More of this here: http://devlicio.us/blogs/anne_epstein/archive/2009/11/20/nhibernate-and-composite-keys.aspx

I believe that because of that, you must add more columns (1 column to be precice) in the junction table to achieve what i wanted.

Enjoy!

Turambar
  • 293
  • 1
  • 3
  • 8