2011-12-02 4 views
0

Nhibernate 3.2 GA를 사용 중이며 코드로 매핑하고 있습니다.NHibernate 구성 요소가 저장되지 않습니까?

public class Organization : EntityBase 
{ 
    public Organization() 
    { 
     Users = new List<User>(); 
     BankAccounts = new List<BankAccount>(); 
    } 

    [Required] 
    public virtual Guid ExternalID { set; get; } 

    [Required] 
    public virtual Int64 Number { set; get; } 

    [Required] 
    public virtual string Name { set; get; } 

    public virtual Person Contact { set; get; } 

    public virtual Address Address { set; get; } 

    [Required] 
    [DataType(DataType.EmailAddress)] 
    public virtual string Email { set; get; } 

    [DataType(DataType.Url)] 
    public virtual string Website { set; get; } 

    public virtual IList<BankAccount> BankAccounts { set; get; } 

    public virtual IList<User> Users { set; get; } 

} 

이 부품없이 만들어지는 경우 개체가 성공적으로 저장됩니다

나는 2 구성 요소와 간단한 클래스가 있습니다.

그러나 엔티티를 검색하고 구성 요소를 업데이트하려고하면 아무 일도 일어나지 않습니다!

다음
var mapper = new ConventionModelMapper(); 

var entityBaseType = typeof(EntityBase); 

// define root entity and ignore base from being mapped. 
mapper.IsEntity((t, declared) => entityBaseType.IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract); 

mapper.IsRootEntity((t, declared) => entityBaseType.Equals(t.BaseType) 
            || t.BaseType == typeof(InvoiceBase) 
            || t.BaseType == typeof(InvoiceItemBase)); 

mapper.IsComponent((t, declared) => t.Namespace.EndsWith("Components")); 

// column naming and cascading configuration 
mapper.BeforeMapManyToOne += (insp, prop, map) => map.Column(prop.LocalMember.GetPropertyOrFieldType().Name + "Id"); 
mapper.BeforeMapManyToOne += (insp, prop, map) => map.Cascade(Cascade.Persist); 
mapper.BeforeMapBag += (insp, prop, map) => map.Key(km => km.Column(prop.GetContainerEntity(insp).Name + "Id")); 
mapper.BeforeMapBag += (insp, prop, map) => map.Cascade(Cascade.All); 

// enitity class mapping 
mapper.Class<EntityBase>(map => { 

    map.Id(e => e.ID, m => m.Generator(Generators.Identity)); 

    map.Property(e => e.CreatedOn); 
    map.Property(e => e.DeletedOn); 
    map.Property(e => e.UpdatedOn); 

    map.ManyToOne<User>(e => e.CreatedBy, m => { m.Column("CreatedBy"); }); 
    map.ManyToOne<User>(e => e.DeletedBy, m => { m.Column("DeletedBy"); }); 
    map.ManyToOne<User>(e => e.UpdateBy, m => { m.Column("UpdateBy"); }); 

}); 


//define components 
mapper.Component<Person>(map => { 

    map.Property(p => p.Email, m => m.Column("Preson_Email")); 
    map.Property(p => p.Name, m => m.Column("Preson_Name")); 
    map.Property(p => p.Surname, m => m.Column("Preson_Surname")); 

    map.Insert(true); 
    map.Update(true); 

}); 

mapper.Component<Address>(map => { 

    map.Property(a => a.City, m => m.Column("Address_City")); 
    map.Property(a => a.Street, m => m.Column("Address_Street")); 
    map.Property(a => a.Zipcode, m => m.Column("Address_Zipcode")); 

    map.Insert(true); 
    map.Update(true); 

}); 

//TODO: move to different class (cleaner) 
//all entities mapping 
mapper.Class<User>(map => { 
    map.ManyToOne(u => u.Organization); 
}); 

mapper.Class<Organization>(map => { 

    map.Component<Person>(o => o.Contact); 
    map.Component<Address>(o => o.Address); 

    map.Bag<User>(o => o.Users, cm => cm.Inverse(true), r => { }); 
}); 

var mapping = mapper.CompileMappingFor(entityBaseType.Assembly.GetExportedTypes().Where(t => t.Namespace.EndsWith("Domain"))); 

내가 데이터베이스에 저장하는 방법입니다 : 여기

는 매핑 코드

// get user domain object 
       var user = UserService.GetById((int)membershipUser.ProviderUserKey); 
       // get the organization to continue the profile 
       var organization = OrganizationService.GetByExternalId(model.ExternalID); 

       organization.Address = new Address 
        { 
         City = model.OrganizationAddressCity, 
         Street = model.OrganizationAddressStreet, 
         Zipcode = model.OrganizationAddressZip 
        }; 

       organization.Contact = new Person 
        { 
         Email = model.OrganizationContactEmail, 
         Name = model.OrganizationContactName, 
         Surname = model.OrganizationContactSurname 
        }; 

        //organization.CreatedBy = user; 
        organization.UpdatedOn = DateTime.Now; 

       // add the user to the organization 
        organization.Users.Add(user); 

       //save organization profile 
       OrganizationService.Update(organization); 

Spring.Net 트랜잭션 관리를 사용하여 조직 저장소 :

 [Transaction] 
    public virtual void Update(T entity) 
    { 
     entity.UpdatedOn = DateTime.Now; 
     Session.Update(entity); 
    } 

을 참고 : Spring.Net 트랜잭션 관리로 장식 된 메서드를 사용하므로 Commit을 명시 적으로 호출하지 않습니다.

+1

이 업데이트를 수행하는 코드를보기 :

해결 방법은 명시 적으로 호출하는 것입니다. – jasonp

+0

@jasonp : 질문을 업데이트했습니다. –

답변

0

괜찮습니다. 매핑과는 아무런 관련이 없습니다. 매핑이 맞습니다!

스프링 트랜잭션 결함으로, Spring.Net SessionFactory를 구성하는 것이 실수인지 확실하지 않습니다.

Session.Flush(); 
관련 문제