How is hibernate_unique_key table created in a new

2020-06-29 08:16发布

I'm trying to create my first test application with NHibernate following example in NHibernate 2.0 Beginners Guide. The example is using HiLo POID generator in mappings. I understand that this algorithm requires special database table to store current Hi value. But I don't understand how is this table created? Do I need to create table manually (I haven't found SQL script for this table) or should it be crated automatically by the session?

My current code is throwing exception with message: Invalid object name 'hibernate_unique_key' and table doesn't exist in my database.

2条回答
乱世女痞
2楼-- · 2020-06-29 08:56

The simpliest option how to generate database schema is to use the tool hbm2ddl: it is just the matter of configuring session factory:

<property name="hbm2ddl.auto">create</property>

Another option which I found recently is running shema export from the code. Here are details in another thread.

查看更多
在下西门庆
3楼-- · 2020-06-29 09:01

I suggest a hilo per table strategy, to do this you will need to create the table manually and then add some rows of data e.g.

CREATE TABLE hibernate_unique_key (
  TableName varchar(25) NOT NULL,
  NextHi bigint NOT NULL
)

then add a row into the database for every table you wish to use the hilo for: e.g.

CmsLogin,10
Address, 10

Your mappings would then contain the following:-

    <id name="Id" column="Id" unsaved-value="0">
        <generator class="hilo">
            <param name="column">NextHi</param>
            <param name="where">TableName='CmsLogin'</param>
            <param name="max_lo">100</param>
        </generator>
    </id>

and viola!

查看更多
登录 后发表回答