在休眠中插入大量记录的最佳方法

发布于 2024-10-10 11:35:45 字数 146 浏览 6 评论 0原文

我正在使用休眠+播放!工作中的框架,是否有使用 hibernate 插入大量记录的“最佳实践”?每个文本文件大约有 6,000 到 10,000 个,所以我不知道 Hibernate 是否会在工作中卡住或抛出异常。

任何建议请告诉我,如果我需要解释更多,请告诉我

I'm using hibernate + play! framework at work, is there a "best practice" on inserting a good amount of records using hibernate? They are around 6,000 to 10,000 per text file so I don't know if Hibernate is going to choke on the job or throw an exception.

Any suggestion let me know, let me know if I have to explain more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

踏月而来 2024-10-17 11:35:45

来自 *Java Persistence and Hibernate" (Manning) 并遵循 Pangea 的评论,使用无状态会话(没有持久性上下文缓存):

StatelessSession session = sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
    Item item = new Item(...);
    session.insert(item);
}
tx.commit();
session.close();

From *Java Persistence and Hibernate" (Manning) and following a comment from Pangea, use a stateless session (which doesn't have a persistence context cache) :

StatelessSession session = sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
    Item item = new Item(...);
    session.insert(item);
}
tx.commit();
session.close();
猫瑾少女 2024-10-17 11:35:45

只是对 Kartoch 的答案中的代码进行了一些更正。

根据 批处理,“插入StatelessSession 接口定义的 ()、update() 和 delete() 操作被认为是直接数据库行级操作,它们分别导致 SQL INSERT、UPDATE 或 DELETE 的立即执行。它们与保存具有不同的语义。 ()、saveOrUpdate() 和 delete() 操作由 Session 接口定义。”

StatelessSession 不再需要 save()、flush()、clear()。代码应该是这样的:

StatelessSession session = sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();

for ( int i=0; i<100000; i++ ) {
  Item item = new Item(.....);
  session.insert(item );
}    

tx.commit();
session.close();

最后,这里讨论一下普通批量插入和StatelessSession插入之间的区别:使用 StatelessSession 进行批处理

Just some corrections of the codes in the answer of Kartoch.

According to Batch Procession, "The insert(), update() and delete() operations defined by the StatelessSession interface are considered to be direct database row-level operations. They result in the immediate execution of a SQL INSERT, UPDATE or DELETE respectively. They have different semantics to the save(), saveOrUpdate() and delete() operations defined by the Session interface."

No more save(), flush(), clear() for StatelessSession. The code should be like this :

StatelessSession session = sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();

for ( int i=0; i<100000; i++ ) {
  Item item = new Item(.....);
  session.insert(item );
}    

tx.commit();
session.close();

Finally, Here is a discussion of the difference between the normal batch inserting and the StatelessSession insert : Using StatelessSession for Batch processing.

清引 2024-10-17 11:35:45

最好是使用 StatelessSessions。考虑以下示例 (http://docs .jboss.org/hibernate/orm/3.3/reference/en-US/html/batch.html):

StatelessSession session = sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();

ScrollableResults customers = session.getNamedQuery("GetCustomers")
    .scroll(ScrollMode.FORWARD_ONLY);
while ( customers.next() ) {
    Customer customer = (Customer) customers.get(0);
    customer.updateStuff(...);
    session.update(customer);
}

tx.commit();
session.close();

Best is to use StatelessSessions. Consider the following example from (http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/batch.html):

StatelessSession session = sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();

ScrollableResults customers = session.getNamedQuery("GetCustomers")
    .scroll(ScrollMode.FORWARD_ONLY);
while ( customers.next() ) {
    Customer customer = (Customer) customers.get(0);
    customer.updateStuff(...);
    session.update(customer);
}

tx.commit();
session.close();
总攻大人 2024-10-17 11:35:45

只需打开您的会话和交易即可。

添加会话保存中的所有元素。

然后提交事务。

//Remember to effective handler errors
public void saveAll(List<Object> list) throws Exception{
Session s = HibernateUtil.openSession();
Transaction tx = s.beginTransaction();
for(Object obj : list)
 s.save(obj);
tx.commit();
s.flush();
s.close();
}

Just open your session and transaction.

Add all elements in the save of the session.

Then commit the transaction.

//Remember to effective handler errors
public void saveAll(List<Object> list) throws Exception{
Session s = HibernateUtil.openSession();
Transaction tx = s.beginTransaction();
for(Object obj : list)
 s.save(obj);
tx.commit();
s.flush();
s.close();
}
绝不放开 2024-10-17 11:35:45

如果您想在休眠之外进行插入,您始终可以直接获取 Connection 对象。

Connection connection = DB.getConnection();

You can always get a Connection object directly in case you want to do the inserts outside of hibernate.

Connection connection = DB.getConnection();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文