开发者

NHibernate Mapping 1 Object To 2 Tables

We want to use NHibernate as our persistence layer in our application. We are also using Fluent NHibernate for the mappings.

We get Person data from a 3rd party and we need to save the data to our database. It works better in the code to have all properties on one object, but it makes more sense to save the data to 2 tables in our database.

Our object looks like this:

public class Person
{
    public virtual long VersionNumber { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string IdentificationNumber { get; set; }
}

Our database tables look like this:

CREATE TABLE PersonVersion (
    [PK] VersionNumber bigint NOT NULL,
    [FK] PersonDemographicsId int NOT NULL
)
CREATE TABLE PersonDemographics (
    [PK] PersonDemographicsId int NOT NULL,
    IdentificationNumber nvarchar(9) NOT NULL,
    FirstName nvarchar(100) NOT NULL,
    LastName nvarchar(100) NOT NULL
)

When we receive new data, the version number can change, but the rest of the demographics could be the same. What we need NHibernate to do is save a new record to the PersonVersion table which links to the existing PersonDemographics record. If the demographics have changed, then we will create a new record in both tables. But most of the time, once we've downloaded the initial data, the demographics won't change as often as the version number. We need to keep track of all version numbers so that's why it's necessary to create a new PersonVersion record.

How would we accomplish this using NHibernate and mappings using Fluent NHibernate?

Also, as you can see, our Person object currently does not have a开发者_如何学JAVA PersonDemographicsId because it is not needed by our application at all; it is just an ID for the table relationship which is needed in the database. In order to properly map this in NHibernate, do we have to add a PersonDemographicsId property on our Person object?

Thanks for the help!


This article http://ayende.com/blog/2327/multi-table-entities-in-nhibernate explains a way to map a single class to two tables in the database.


just an idea, maybe has to be tweaked

public class Person
{
    public virtual int Id { get; set; }

    internal protected virtual IList<long> VersionNumbers { get; set; }
    public virtual long VersionNumber {
       get { return VersionNumbers[VersionNumbers.Count - 1]; }
       set { VersionNumbers.Add(value); }
    }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string IdentificationNumber { get; set; }
}

public class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Table("PersonDemographics");
        Id(p => p.Id, "PersonDemographicsId").GeneratedBy.Assigned();
        Map(p => p.FirstName);
        Map(p => p.LastName);
        Map(p => p.IdentificationNumber);

        HasMany(p => p.VersionRecord)
            .Table("PersonVersion")
            .KeyColumn("PersonDemographicsId")
            .Element("VersionNumber")
            .OrderBy("VersionNumber")
            .Cascade.All();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜