问题描述
我有5个MySQL InnoDB表: Test,InputInvoice,InputLine,OutputInvoice,OutputLine ,并且每个映射在Hibernate中运行。我玩过使用StatelessSession / Session和JDBC批处理大小。我已经删除了任何生成器类来让MySQL处理这个id代 - 但它仍然表现得很慢。
这些表中的每一个都在java类中表示,并相应地映射到休眠状态。目前,当需要写出数据时,我循环遍历这些对象并执行 session.save(Object)或 session.insert(Object )如果我使用StatelessSession。当我的行数达到最大jdbc批量大小(50)时,我也会执行清空和清除操作(使用Session时)。
- 如果我在一个拥有这些对象的'父类'类中执行了 session.save(master)而不是每个对象,那么速度会更快?
- 如果我将它们放在主/容器类中,我将如何映射hibernate以反映关系?容器类实际上并不是它自己的表,而是一个基于两个索引run_id(int)和line(int)的关系。另一个方向是:How do我得到Hibernate做一个多行插入?
以voetsjoeba的回应作为跳板点。
我的hibernate配置使用以下选项:
hibernate.order_inserts = true
hibernate.order_updates = true
-
我使用 Session to
StatelessSession -
重新订购
Java代码一次处理一个表中的所有元素
。因此,所有
的表x,然后是表y等等。 删除< generator> 来自每个 -
创建逻辑允许我确定是否只设置了
一个id并且不要将
'空'行写入数据库中。 最后,我打开 dynamic-insert
类。 Java现在创建它并且
将它分配给对象
为我的类在他们的hibernate
定义中是这样的:< class name =com.my.classtable =MY_TABLEdynamic-insert =true >
I have 5 MySQL InnoDB tables: Test,InputInvoice,InputLine,OutputInvoice,OutputLine and each is mapped and functioning in Hibernate. I have played with using StatelessSession/Session, and JDBC batch size. I have removed any generator classes to let MySQL handle the id generation- but it is still performing quite slow.Each of those tables is represented in a java class, and mapped in hibernate accordingly. Currently when it comes time to write the data out, I loop through the objects and do a session.save(Object) or session.insert(Object) if I'm using StatelessSession. I also do a flush and clear (when using Session) when my line count reaches the max jdbc batch size (50).
- Would it be faster if I had these in a 'parent' class that held the objects and did a session.save(master) instead of each one?
- If I had them in a master/container class, how would I map that in hibernate to reflect the relationship? The container class wouldn't actually be a table of it's own, but a relationship all based on two indexes run_id (int) and line (int).
- Another direction would be: How do I get Hibernate to do a multi-row insert?
The final solution for me was to use voetsjoeba's response as a jumping off point.My hibernate config uses the following options:
hibernate.order_inserts = true hibernate.order_updates = true
I changed from using Session toStatelessSession
Re-ordered theJava code to process all the elementsin a batch a table at a time. So allof table x, then table y, etc.
Removed the <generator> from eachclass. Java now creates it andassigns it to the object
Created logic that allowed me to determine if justan id was being set and not write'empty' lines to the database
Finally, I turned on dynamic-insertfor my classes in their hibernatedefinitions like so: <class name="com.my.class" table="MY_TABLE" dynamic-insert="true">
这篇关于休眠关系映射/加快批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!