私は非常に基本的なNHibernate3.2タスクに取り組んでおり、既存のPostgresテーブルにレコードを挿入しています。私は非常に単純なオブジェクトを使用しているので、この質問に意味があります。
postgresテーブルは次のとおりです。
CREATE TABLE cat
(
id serial NOT NULL,
"name" character varying(50) NOT NULL,
sex_id integer NOT NULL,
CONSTRAINT cat_pkey PRIMARY KEY (id),
CONSTRAINT cat_sex_id_fkey FOREIGN KEY (sex_id)
REFERENCES sex (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
CREATE TABLE sex
(
id serial NOT NULL,
"name" character varying(10) NOT NULL,
CONSTRAINT sex_pkey PRIMARY KEY (id),
CONSTRAINT sex_name_key UNIQUE (name)
)
WITH (
OIDS=FALSE
);
私のマッピングクラスは次のとおりです。
public class CatMap : ClassMapping<Cat>
{
public CatMap()
{
Table("cat");
Id(x => x.Id, map =>
{
map.Column("id");
map.Generator(NHibernate.Mapping.ByCode.Generators.Native);
});
Property(x => x.Name, map =>
{
map.Column("name");
map.Length(50);
map.NotNullable(true);
});
ManyToOne(x => x.Sex, map =>
{
map.Column("Sex");
map.Unique(true);
map.ForeignKey("cat_sex_id_fkey");
});
}
}
public class SexMap : ClassMapping<Sex>
{
public SexMap()
{
Table("sex");
Id(x => x.Id, map =>
{
map.Column("id");
map.Generator(Generators.Native);
});
Property(x => x.Name, map =>
{
map.Column("name");
map.Unique(true);
map.Length(10);
map.NotNullable(true);
});
}
}
私のデータクラスは次のとおりです。
public class Sex
{
public Sex()
{
}
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class Cat
{
public Cat()
{
}
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Sex Sex { get; set; }
}
最後に、私が実際に上記のすべてを使用しようとしているコードを含むクラス。
public class Class1
{
public string DoSomething()
{
var sessionFactory = CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
Postgres.Tables.Sex sex1 = new Postgres.Tables.Sex() { Name = "Male" };
Postgres.Tables.Sex sex2 = new Postgres.Tables.Sex() { Name = "Female" };
Postgres.Tables.Cat cat1 = new Postgres.Tables.Cat() { Name = "Cat1" };
cat1.Sex = sex1;
Postgres.Tables.Cat cat2 = new Postgres.Tables.Cat() { Name = "Cat2" };
cat2.Sex = sex2;
session.SaveOrUpdate(sex1);
session.SaveOrUpdate(sex2);
session.SaveOrUpdate(cat1);
session.SaveOrUpdate(cat2);
transaction.Commit();
}
}
return "I created the cats.";
}
private static ISessionFactory CreateSessionFactory()
{
NHibernate.Mapping.ByCode.ModelMapper modelMapper = new NHibernate.Mapping.ByCode.ModelMapper();
System.Type[] mappingTypes = typeof(Postgres.Tables.Mappings.CatMap).Assembly.GetExportedTypes().Where(t => t.Name.EndsWith("Map")).ToArray();
modelMapper.AddMappings(mappingTypes);
Configuration cfg = new Configuration();
cfg.Proxy(p => p.ProxyFactoryFactory<NHibernate.Bytecode.DefaultProxyFactoryFactory>());
cfg.DataBaseIntegration(d =>
{
d.ConnectionString = "server=192.168.1.126;Port=5432;Database=simple;User Id=postgres;Password=postgres;";
d.Dialect<NHibernate.Dialect.PostgreSQL82Dialect>();
});
cfg.AddMapping(modelMapper.CompileMappingForAllExplicitlyAddedEntities());
return cfg.BuildSessionFactory();
}
}
「session.SaveOrUpdate(cat1)」でGenericADOExceptionが発生し、「挿入できませんでした:[DAL.Postgres.Tables.Cat] [SQL:INSERT INTO cat(name、Sex)VALUES(?、?); select lastval ()]」。InnerExceptionは"{"エラー:42703:リレーション\"cat\"の列\"sex\"が存在しません"}"です。
「sex1」を「cat1」に、「sex2」を「cat2」に適切に割り当てて、最初が男性、2番目が女性になるようにする方法に少し困惑しています。
ご入力ありがとうございます。