java 休眠刷新?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5442810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:09:26  来源:igfitidea点击:

Hibernate refresh?

javahibernatelazy-loadinghibernate-mapping

提问by marrithl2

I am working with hibernate and mysql to abstract a database for use in a java driven website. I got all my queries to work out just fine using hibernate, but couldn't seem to figure out how to do the updates, inserts, and deletes with it so I ended up making my own bean that does those functions using regular mySQL methods.

我正在使用 hibernate 和 mysql 来抽象一个数据库,以便在 java 驱动的网站中使用。我使用 hibernate 可以很好地解决所有查询,但似乎无法弄清楚如何使用它进行更新、插入和删除,所以我最终制作了自己的 bean,使用常规的 mySQL 方法来执行这些功能。

The issue I am running into now is that when I do an update or insert using regular mysql, the persisted hibernate objects from the database do not get updated, but the data stored in the database does. How can I resynchronize or "refresh" the hibernate information to match the mysql information?

我现在遇到的问题是,当我使用常规 mysql 进行更新或插入时,数据库中持久化的休眠对象不会更新,但存储在数据库中的数据会更新。如何重新同步或“刷新”休眠信息以匹配 mysql 信息?

Or if it would be easier, how do I save/update modified objects from hibernate into msql. (obviously I'm new to hibernate) Any help would be appreciated. Thanks ahead of time!

或者如果它更容易,我如何将修改后的对象从休眠状态保存/更新到 msql。(显然我是休眠的新手)任何帮助将不胜感激。提前致谢!

回答by Michael

Persisting the data back using standard SQL seems to somewhat defeat the purpose of using Hibernate. My recommendation would be to change things so that you can do both retrieval and saving of data through Hibernate. Depending on your setup it could be as easy as getting a session from the session factory and calling the save method.

使用标准 SQL 将数据保留回来似乎在某种程度上违背了使用 Hibernate 的目的。我的建议是改变一些事情,以便您可以通过 Hibernate 进行数据的检索和保存。根据您的设置,它可能就像从会话工厂获取会话并调用 save 方法一样简单。

i.e.

IE

  Session session = sessionFactory.getSession();
  session.save(hibernateObject);

In my experience the Hibernate documentation is quite good. Skimming through it would probably be a worthwhile endeavor. Particularly this section: Chapter 10: Working with Objects

根据我的经验,Hibernate 文档非常好。浏览它可能是一项值得的努力。特别是本节:第 10 章:使用对象