Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7493179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:47:52+00:00 2026-05-29T16:47:52+00:00

I have a class defined like this: public class StateMachineMetadata<T> where T: struct {

  • 0

I have a class defined like this:

public class StateMachineMetadata<T> where T: struct
{
    public virtual int ID { get; protected set; }
    public virtual T State { get; set; }
    public virtual DateTime DateChanged { get; set; }
}

Where the State property is an enum:

public enum CarState
{
    Stopped = 1,
    Moving = 2
}
public enum OrderState
{
    Ordered = 1,
    Delivered = 2
}

Can I have two types:

StateMachineMetadata<CarState>, StateMachineMetadata<OrderState>

to be persisted in one table StateMachineMetadata using NHibernate?

=== update ===

As workaround generic part can be moved to interface:

public interface IStateMachineMetadata<T> where T: struct
{
    int ID { get; }
    T State { get; set; }
    DateTime DateChanged { get; set; }
}

public class StateMachineMetadataBase
{
    public virtual int ID { get; protected set; }
    public virtual DateTime DateChanged { get; set; }
}

public class CarStateMachineMetadata
    : StateMachineMetadataBase, IStateMachineMetadata<CarState>
{
    public virtual CarState State { get; set; }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T16:47:54+00:00Added an answer on May 29, 2026 at 4:47 pm

    No, and here’s why.

    Let’s assume we have two rows in the StateMachineMetadata table:

    ID | State | DateChanged
    1  |   1   |  2012-02-13
    2  |   1   |  2012-02-14
    

    If the State column is mapped both to CarState and OrderState, how would NHibernate determine which enum to deserialize to? One solution would be to add a discriminator column to the StateMachineMetadata table, which tells NHibernate which type it should map a specific row to.
    For example the Type discriminator column:

    ID | State | DateChanged | Type
    1  |   1   |  2012-02-13    1
    2  |   1   |  2012-02-14    2
    

    tells NHibernate that the first row represents a StateMachineMetadata<CarState> while the second is a StateMachineMetadata<OrderState>. This is called a Table per class hierarchy mapping in NHibernate. It requires both classes to inherit from a common base class, which in your case would naturally be StateMachineMetadata<T>.

    But here’s the catch: NHibernate doesn’t have a way to map a class with an open generic type. The reason is that it’s simply not possible.

    However, NHibernate does allow you to map classes with closed generics. You just can’t map them to the same table. Here’s how such configuration would look like:

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               assembly="MyAssembly"
               namespace="MyNamespace">
        <class name="StateMachineMetadata`1[MyNamespace.CarState]"
               table="StateMachineMetadata_CarState">
            <!-- property to column mappings -->
        </class>
    
        <class name="StateMachineMetadata`1[MyNamespace.OrderState]"
               table="StateMachineMetadata_OrderState">
            <!-- property to column mappings -->
        </class>
    </hibernate-mapping>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Foo defined like this: class Foo { public: Foo(int num);
I have a class defined like this: class MyClass { int x; public: MyClass(int
I have a State class defined like this: Public Class State Public Property StateId
I have a class here that is defined like this: struct USERFPOINT { POINTFLOAT
I have an abstract entity base class defined like this: public abstract class SessionItem
I have built myself a generic collection class which is defined like this. public
I have a ViewFlipper defined in a separate class. It looks like this: public
I have a custom listView defined like this public class DDListView extends ListView implements
I have an IEnumerable of an item class defined like this: public class Item
I have a class that defines its own enum like this: public class Test

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.