Vinoth
Vinoth

Reputation: 21

Custom ID generator in NHibernate

HI all,

How to do functionality of generator class="assigned" as well as generator class="native" together.

in my case some situation i will have ID another case i dont know the ID.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Acurus.Capella.Core.DomainObjects" assembly="User">
  <class name="UserLookup, Core" table="User_lookup" lazy="true">
    <id name="Id" column="User_ID">
      <generator class="assigned" />
    </id>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Acurus.Capella.Core.DomainObjects" assembly="User">
  <class name="UserLookup, Core" table="User_lookup" lazy="true">
    <id name="Id" column="User_ID">
      <generator class="native" />
    </id>

Upvotes: 1

Views: 1669

Answers (1)

Diego Mijelshon
Diego Mijelshon

Reputation: 52745

You can write your own implementation of IIdentifierGenerator using code from both NHibernate.Id.Assigned and whatever concrete implementation of "native" corresponds to your DB, like NHibernate.Id.IdentityGenerator.

However, as Ids should be meaningless, I don't know why you would "have ID" before persisting an entity.

Upvotes: 2

Related Questions