您能解释一下 generatedBy.HiLo(...) 的参数的作用吗?
有一些重载:
GeneratedBy.HiLo(string maxLo);
GeneratedBy.HiLo(string maxLo, Action<ParamBuilder> paramValues);
GeneratedBy.HiLo(string table, string column, string maxLo);
GeneratedBy.HiLo(string table, string column, string maxLo, string where);
GeneratedBy.HiLo(string table, string column, string maxLo, Action<ParamBuilder> paramValues);
我从概念上理解高/低算法的作用。但这些参数有什么作用呢?我在 Hibernate 中找不到有关它们实际用途的文档。
maxLo
应该是一个名称,还是一个数字? paramValues 是什么? table
和 column
的用途是什么?
基于 NHibernate 入门教程的示例用法:
public class StoreMap : ClassMap<Store>
{
public StoreMap()
{
Id(x => x.Id).GeneratedBy.HiLo("something");
Map(x => x.Name);
HasMany(x => x.Staff)
.Inverse()
.Cascade.All();
HasManyToMany(x => x.Products)
.Cascade.All()
.Table("StoreProduct");
}
}
There are a few overloads:
GeneratedBy.HiLo(string maxLo);
GeneratedBy.HiLo(string maxLo, Action<ParamBuilder> paramValues);
GeneratedBy.HiLo(string table, string column, string maxLo);
GeneratedBy.HiLo(string table, string column, string maxLo, string where);
GeneratedBy.HiLo(string table, string column, string maxLo, Action<ParamBuilder> paramValues);
I understand conceptually what the hi/lo algorithm does. But what do these parameters do? I can't find the documentation in Hibernate on what they actually do.
Is maxLo
supposed to be a name, or a number? What are the paramValues? What are the purposes of table
and column
?
Example usage based on the NHibernate getting started tutorial:
public class StoreMap : ClassMap<Store>
{
public StoreMap()
{
Id(x => x.Id).GeneratedBy.HiLo("something");
Map(x => x.Name);
HasMany(x => x.Staff)
.Inverse()
.Cascade.All();
HasManyToMany(x => x.Products)
.Cascade.All()
.Table("StoreProduct");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
maxLo 是 HiLo 的低值。
根据插入表的频率,您应该将该值指定为 10、100 或 1000。如果将该值指定为 10,NHibernate 将从数据库中获取 Hi 值并使用低值生成 HiLo。
如果您经常插入,那么您应该指定一个高 Lo(例如 100 或 1000),如果您插入得较少,那么您应该指定一个低 Lo。这是因为每次创建会话工厂时都会从数据库中提取一个新的 Hi 值。如果您当前的 Hi 为 5,Lo 为 100,并且已插入 100 个项目中的 13 个,并且您创建了一个新的会话工厂(通过重新启动网站或应用程序),那么它将获得新的 Hi 为 6,并且您会有 513 到 600 之间的差距
因此,如果您的 Hi 当前为 3,那么它将生成 31 到 40 之间的值。同样,如果您的 Lo 为 1000,它将生成从3001 到 4000。
附加参数允许您指定存储 Hi 值的表。默认情况下,如果 NHibernate 配置为创建/更新数据库,NHibernate 将为您生成此表,但是参数允许您自己设置表。
我不久前写了一篇博客文章,其中显示了每个 Hi 的列和每个 Hi 的行。
http://www.philliphaydon.com/2010/10 /24/using-hilo-with- Fluentnhibernate/
编辑:Lo 例如 7 的行为
HiLo 被计算为数字范围,对于例如,如果我们的 Hi 为 3,Lo 为 10,则范围介于:
3*10 = 30 到 3*10+10 = 40
之间工厂的范围是 30 到 40,当你请求一个对象的 Id 时,它会获取当前值 (30) 并添加 1。因此 Id = 31。下一个对象将采用31+1 = 32。这种情况直到达到 40 为止,此时它会进入数据库并请求下一个 Hi。
如果您有两个 Web 服务器,第二个 Web 服务器可能会请求下一个 Hi,即 4,这意味着第一个 Web 服务器将获得 5。
因此您的下一个范围是 50 到 60。
如果 Lo 是 7,我们将得到:
0*7 = 0 / 0*7+7 = 7 = 1-7
1*7 = 7 / 1*7+7 = 14 = 8-14
2*7 = 14 / 2*7+7 = 21 = 15-21
所以它仍然有效。
但我个人的观点是使用整数值 10、100、1000 更容易。
maxLo is the Lo-value of the HiLo.
Depending on the frequency of inserts into the table you should specify the value as 10, 100, or 1000. If you specify the value as 10, NHibernate will grab the Hi value from the database and use the low value to generate your HiLo.
If you insert often then you should specify a high Lo (such as 100 or 1000), if you insert less often then you should specify a low Lo. This is because a new Hi value is pulled from the database every time the session factory is created. If you have a current Hi of 5 with a Lo of 100, and have inserted 13 of 100 items, and you create a new Session Factory (from restarting the website, or app) then it will get a new Hi of 6, and you will have gaps from 513 to 600
So if your Hi is currently 3, then it will generate values from 31 to 40. Likewise if your Lo is 1000, it will generates values from 3001 to 4000.
The additional parameters allow you to specify a table where the Hi value is stored. By default NHibernate will generate this table for you if it's configured to create/update the database, however the parameters allow you to setup the tables yourself.
I wrote a blog post a while ago which shows Column per Hi, and Row per Hi.
http://www.philliphaydon.com/2010/10/24/using-hilo-with-fluentnhibernate/
Edit: Behaviour of a Lo such as 7
HiLo is calculated as a range of numbers, for example if we have a Hi of 3, and a Lo of 10, the range is between:
3*10 = 30 to 3*10+10 = 40
So the session factory will have a range of 30 to 40, when you ask for an Id for an object it grabs it's current value (30) and add's 1. So the Id = 31. The next object will take 31+1 = 32. And this occurs until it reaches 40, at which point it goes to the database and asks for the next Hi.
If you had two web servers, the second Web Server may have asked for the next Hi which was 4, which means the first Web Server will get 5.
So your next range is 50 to 60.
If the Lo was 7, we would get:
0*7 = 0 / 0*7+7 = 7 = 1-7
1*7 = 7 / 1*7+7 = 14 = 8-14
2*7 = 14 / 2*7+7 = 21 = 15-21
So it still works.
But my personal opinion is it's easier to work with whole values, 10, 100, 1000.