Question

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";
Was it helpful?

Solution

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top