How do I get automatic versioning in immutable subclass?

6 views
Skip to first unread message

Mikael Henriksson

unread,
Nov 19, 2009, 8:48:47 AM11/19/09
to nhu...@googlegroups.com
Ok the following works like a fucking charm (pardon the language) except that since I only do inserts it does not update the version automatically and I'd rather not leave that to whoever is writing the persistence service ;)
Any suggestions to how I can intercept and automatically increment the version where contact_id = ? and contact_version_id = ?. Do I need to use the event subsystem for that? Or can I solve it further in the mapping?
Note: ( ReadOnly() means "mutable=false" which prohibits deletes and updates.)

public class ParentMap : ClassMap<Parent>
{
    public ParentMap()
    {
        Table("parent_def");      
        Id(x => x.Id,"parent_id");
        Map(x => x.UID, "parent_uid").CustomSqlType("varchar").Length(40);
        Map(x => x.DeletedAt ,"dte_deleted").Nullable();
        Map(x => x.ModifiedAt, "dte_modified").Nullable();
        Map(x => x.CreatedAt, "dte_created").Nullable();
        Map(x => x.AutoUpdate, "auto_update").CustomSqlType("bit");
       
        HasMany(x => x.ParentVersions)
            .KeyColumn("parent_id")
            .Inverse()
            .Cascade.All();
    }
}
 
public class ParentVersionMap : ClassMap<ParentVersion>
{
    public ParentVersionMap()
    {
        Table("parent_version");
        ReadOnly();
 
        Id(x => x.Id  ,"parent_version_id");
        Version(x => x.Version)
            .Column("parent_version")
            .Generated.Never();
        Map(x => x.ParentHash ,"parent_hash")
            .CustomSqlType("varchar")
            .Length(40);
        Map(x => x.DateCreated ,"dte_created");
        Map(x => x.UpdateAvailable, "update_available");
 
        References(x => x.Parent, "parent_id")
            .ForeignKey("parent_id");
    }
}

Fabio Maulo

unread,
Nov 19, 2009, 8:55:37 AM11/19/09
to nhu...@googlegroups.com
What mean "Version" (that is for optimistic-lock) in a no mutable entity ?

2009/11/19 Mikael Henriksson <mik...@zoolutions.se>

--

You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=.



--
Fabio Maulo

Mikael Henriksson

unread,
Nov 19, 2009, 9:07:40 AM11/19/09
to nhu...@googlegroups.com
Ok I am terrible at explaining but here goes: You know how you have counters id columns (incremental identity). The version would be the incremental identity for the specific primary-key and foreign-key. So for every insert where the parent_id and the parent_version_id already exists it should autoincrement so this time around it would be more of a conditional counter I suppose. 

Mikael Henriksson

unread,
Nov 19, 2009, 9:08:51 AM11/19/09
to nhu...@googlegroups.com
Pardon the primary key has nothing to do with it, only the forreign key. The primary key is just an identity.

Mikael Henriksson

unread,
Nov 19, 2009, 4:11:38 PM11/19/09
to nhu...@googlegroups.com
One way of doing it is creating a preinsertevent like below but it feels wrong and it is an extra select for evert insert which feels wrong. Any other suggestions?


public bool OnPreInsert(PreInsertEvent @event)
{
    var entity = @event.Entity as ParentVersion;
    if (entity == null)
    {
        return false;
    }
 
    var currentMaxVersion = @event.Session.CreateCriteria<ParentVersion>("pv")
        .Add(Restrictions.Eq("pv.Parent.Id", entity.Parent.Id))
        .SetProjection(Projections.Max("pv.Version"))
        .UniqueResult<int>();
 
    Set(@event.Persister, @event.State, "ParentVersion", currentMaxVersion + 1);
 
    return false;
}
Reply all
Reply to author
Forward
0 new messages