bitbonk
bitbonk

Reputation: 49609

Make an Entity Write-Only in NHibernate

I use NHibernate with XML mapping and an Oracle DB. And I have this simple mapping:

  <class name="ProjectInfo" table="T_PROJECT">
    <id name="Id" column="IDENT" />
    <property name="Customer" />
    <join table="T_PROJECTRUN" optional="true">
      <key column="PRIDENT" />
      <property name="Remarks" column="Remarks" />
      <property name="RemarksTimestamp" column="Remarks_TS" />
    </join>
  </class>

and the entity:

public class ProjectInfo : EntityBase<ProjectInfo, int>
{
    public string Customer { get; set; }
    public string Remarks { get; set; }
    public DateTime RemarksTimestamp { get; set; }
}

ProjectInfo is supposed to act as a read-only (from DB) view of the more complex Project entity. Which has a similiar mapping but does not have the two Remarks properties.

How can I make NHibernate never try to write the ProjectInfo entity to the database but only read it from the database? So from NHibernate this would be Write-Only I guess.

Currently whenever I try to update a Project entity using this.Session.Update(entity) I get the following exception:

NHibernate.Exceptions.GenericADOException occurred
  HResult=-2146232832
  Message=could not insert: [MyNamspace.Model.ProjectInfo#1][SQL: INSERT INTO T_PROJECTRUN (Remarks, Remarks_TS, PRIDENT) VALUES (?, ?, ?)]
  Source=NHibernate
  SqlString=INSERT INTO T_PROJECTRUN (Remarks, Remarks_TS, PRIDENT) VALUES (?, ?, ?)
  StackTrace:
       at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
       at NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
       at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, Object rowId, ISessionImplementor session)
       at NHibernate.Action.EntityUpdateAction.Execute()
       at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
       at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
       at NHibernate.Engine.ActionQueue.ExecuteActions()
       at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
       at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
       at NHibernate.Impl.SessionImpl.Flush()
       at NHibernate.Transaction.AdoTransaction.Commit()
       at MyNamspace.RepositoryBase`2.ExecuteAsTransaction(Action action)

Upvotes: 2

Views: 636

Answers (1)

Tomasz Jaskuλa
Tomasz Jaskuλa

Reputation: 16013

Have you tried something like this ?

<class name="ProjectInfo" table="T_PROJECT" mutable="false">

I think it exists in NHibernate 3.2 and after but don't know since when.

Upvotes: 3

Related Questions