Droydn
Droydn

Reputation: 1067

NHibernate (Hibernate) Auto Increment On Property

Using nHibernate (or Hibernate as the mapping does not differ), I want to create an object with the following SQL:

CREATE TABLE `table` (
    `EntityId` binary(16) NOT NULL,
    `Number` int(11) NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (`EntityId`),
    Key(`Number`)
);

Ive tried using generated properties, additional id tags, specifying the sqltype in column tags under the property tags, and messing with defaults, insert, update, and not null attributes but nothing seems to work. Am I just not able to map this in Hibernate mapping syntax? How can I get a property that is not the primary key to auto increment?

I would post the mappings Ive got but none of them work.

Thanks in advance

Update: Using this mapping which I think is a good starting point but not quite what I want:

<class name="TheEntity" table="table">

  <id name="Id" 
     column="EntityId"
     generator="guid.comb" />

  <property name="Number" 
            generated="insert"
            insert="false" update="false" />
</class>

The schema in the database is generated as such:

CREATE TABLE `table` (
  `EntityId` binary(16) NOT NULL,
  `Number` int(11) DEFAULT NULL,
  PRIMARY KEY (`EntityId`)
);

which is not the desired behavior. Nor does nHibernate, through some means, insert a sequence into Number. Generated Properties, as shown above, from what Ive read rely on the database taking care of the insertion and updating entirely through something like a trigger. I do not want to use a trigger to do something as basic as auto_increment however since there is a built in facility already for this.

Any ideas?

Thanks again in advance.

Upvotes: 0

Views: 2462

Answers (2)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123861

Not really sure where is the problem, but the mapping could be like this:

<class name="TheEntity" table="table">

  <id name="Id" 
     column="EntityId"
     generator="guid.comb" />

  <property name="Number" 
            generated="insert"
            insert="false" update="false" />
</class>

The Number:

The generated value, which is not representing id could be marked as 5.5. Generated Properties. A cite:

generated="insert" - states that the given property value is generated on insert, but is not regenerated on subsequent updates.

We are also helping NHibernate with explicit declaration to NOT insert and upate.

The Guid - EntityId:

I am not using MySQL, so not sure if there is still some issue with guid mapping - check
Using Guid as Id column in NHibernate causes format exception when using MySQL

But in general, it could be like this, when NHibernate uses generator for us, to create new EntityId values. If the application creates the EntityId different value, just use generator="assigned"

The entity class in C# then should look like:

public class TheEntity 
{
    public virtual Guid Id { get; set; }
    public virtual int Number { get; set; }

Upvotes: 0

s7474
s7474

Reputation: 58

I use notation like this to generate id for postgres:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "financialAnalyst_seq_gen")
@SequenceGenerator(name = "financialAnalyst_seq_gen", sequenceName = "financialAnalyst_id_seq", allocationSize=1)

Upvotes: 1

Related Questions