当我第一次调用一个对象时,跨数据库对象得到了完美的水合;但是,当我保存一个对象时,然后获取 CreateUser 和 UpdateUser 为空。OriginalNote 参考一直有效(它的 CreateUser 不为空)。我试图在保存后获取新 ID 以重新填充整个对象图。原始笔记的第一个 Get 效果很好,但填充刚刚保存的对象的那个不起作用。
映射:
public sealed class DocumentNoteMap : EntityMapBase<DocumentNote>
{
public DocumentNoteMap()
{
Table("DocDataNote");
Id(x => x.DocDataNoteID).GeneratedBy.Native().UnsavedValue(null);
Map(x => x.DocDataID);
Map(x => x.DocDataEventID);
Map(x => x.NoteText).Column("Note");
References(x => x.OriginalNote).Class<DocumentNote>().Column("ParentDocDataNoteID")
.Not.LazyLoad()
.Not.Update();
References(x => x.UpdateUser).Class<CasUser>().Column("UpdateBy")
.ReadOnly()
.Not.LazyLoad()
.Not.Update();
References(x => x.CreateUser).Class<CasUser>().Column("CreateBy")
.ReadOnly()
.Not.LazyLoad()
.Not.Update();
}
public class CasUserMap : ClassMap<CasUser>
{
public CasUserMap()
{
Schema("CAS.dbo");
Table("CAS_User");
Id(x => x.CASUserID);
Map(x => x.FirstName);
Map(x => x.LastName);
}
}
文档注释存储库:
[Repository]
public class DocumentNoteRepository : HibernateRepositoryBase<DocumentNote,int?>, IDocumentNoteRepository
{
public DocumentNoteRepository(ISessionFactory sessionFactory)
:base(sessionFactory)
{
}
public int? Save(DocumentNote entity, bool autoFlush)
{
var persisted = (DocumentNote)CurrentSession.Merge(entity);
entity.DocDataNoteID = persisted.DocDataNoteID;
if (autoFlush) { CurrentSession.Flush(); }
return entity.DocDataNoteID;
}
}
get 方法在基类中:
/// <summary>
/// Base class for data access operations.
/// </summary>
public abstract class HibernateRepositoryBase<TEntity, TId> : IRepository<TEntity, TId> where TEntity : class
{
private ISessionFactory _sessionFactory;
public IQueryable<TEntity> Query()
{
return CurrentSession.Query<TEntity>();
}
protected HibernateRepositoryBase(ISessionFactory sessionFactory)
{
_sessionFactory = sessionFactory;
}
/// <summary>
/// Session factory for sub-classes.
/// </summary>
protected ISessionFactory SessionFactory
{
get { return _sessionFactory; }
private set { _sessionFactory = value; }
}
/// <summary>
/// Get's the current active session. Will retrieve session as managed by the
/// Open Session In View module if enabled.
/// </summary>
protected ISession CurrentSession
{
get
{
var session = _sessionFactory.GetCurrentSession();
_sessionFactory.GetCurrentSession().EnableFilter("TenantFilter").SetParameter("TenantID", User.Current.OrganizationId);
return session;
}
}
[Transaction(ReadOnly = true)]
public TEntity Get(TId id)
{
var entity=CurrentSession.Get<TEntity>(id);
if (entity == null)
{
throw new DataException("Could not find entity of Type{0} and Primary Key {1}".FormatWith(typeof(TEntity), id.ToString()));
}
// evict so changes don't get inadvertently persisted
//CurrentSession.Evict(entity);
return entity;
}
[Transaction(ReadOnly = true)]
public IList<TEntity> GetAll()
{
var listOfEntities = CurrentSession.CreateCriteria<TEntity>().List<TEntity>();
// evict so changes don't get inadvertently persisted - can change when OSIV is removed
listOfEntities.ToList().ForEach(e => CurrentSession.Evict(e));
return listOfEntities;
}
}
编辑:
我尝试过的事情:
1.尝试在保存后(在获取之前)强制刷新。
2. 保存后驱逐保存的对象(获取前)