内容简介:在PostgreSQL中使用GenerationType.IDENTITY会失效批处理能力。因此使用其(BIG)SERIAL,它的作用类似MySQL的 AUTO_INCREMENT。这里使用GenerationType.SEQUENCE激活批插入处理,通过hi/lo算法优化它。第一步,使用GenerationType.SEQUENCE替代GenerationType.IDENTITY:
在PostgreSQL中使用GenerationType.IDENTITY会失效批处理能力。因此使用其(BIG)SERIAL,它的作用类似 MySQL 的 AUTO_INCREMENT。
这里使用GenerationType.SEQUENCE激活批插入处理,通过hi/lo算法优化它。
第一步,使用GenerationType.SEQUENCE替代GenerationType.IDENTITY:
@Entity
<b>public</b> <b>class</b> TennisPlayer implements Serializable {
<b>private</b> <b>static</b> <b>final</b> <b>long</b> serialVersionUID = 1L;
@Id
<font><i>// This will disable insert batching - AVOID IT! 会失效批处理</i></font><font>
</font><font><i>// @GeneratedValue(strategy = GenerationType.IDENTITY)</i></font><font>
</font><font><i>// This will work, but better use the below solution 性能不佳</i></font><font>
</font><font><i>// @GeneratedValue(strategy = GenerationType.AUTO)</i></font><font>
</font><font><i>// This will allow insert batching and optimizes the identifiers</i></font><font>
</font><font><i>// generation via the hi/lo algorithm</i></font><font>
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = </font><font>"hilo"</font><font>)
@GenericGenerator(name = </font><font>"hilo"</font><font>,
strategy = </font><font>"org.hibernate.id.enhanced.SequenceStyleGenerator"</font><font>,
parameters = {
@Parameter(name = </font><font>"sequence_name"</font><font>, value = </font><font>"sequence"</font><font>),
@Parameter(name = </font><font>"initial_value"</font><font>, value = </font><font>"1"</font><font>),
@Parameter(name = </font><font>"increment_size"</font><font>, value = </font><font>"10"</font><font>),
@Parameter(name = </font><font>"optimizer"</font><font>, value = </font><font>"hilo"</font><font>)
})
<b>private</b> Long id;
</font>
这里依靠hi/lo算法在一次数据库往返中获取多个标识符:
源代码可以在 这里 找到 。
以上所述就是小编给大家介绍的《Hibernate/JPA批插入中使用PostgreSQL的(BIG)SERIAL自增主键》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- InnoDB 并发插入,居然使用意向锁?
- Python MySQLdb 使用utf-8 编码插入中文数据问题
- java – 如何使用hibernate将多行插入数据库?
- SpringBoot高级篇JdbcTemplate之数据插入使用姿势详解
- 彬哥笔记 --18 Go语言 游戏服务器使用插入排序实现经验换算等级
- HashMap为何从头插入改为尾插入
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Learn Python 3 the Hard Way
Zed A. Shaw / Addison / 2017-7-7 / USD 30.74
You Will Learn Python 3! Zed Shaw has perfected the world’s best system for learning Python 3. Follow it and you will succeed—just like the millions of beginners Zed has taught to date! You bring t......一起来看看 《Learn Python 3 the Hard Way》 这本书的介绍吧!