ust a summary from the article here: http://www.techfaq360.com/hibernate_interview_question
Difference between session.save() , session.saveOrUpdate() and session.persist()?
- session.save() Save does an insert and will fail if the primary key is already persistent.
- session.saveOrUpdate() : saveOrUpdate does a select first to determine if it needs to do an insert or an update.
session.persist() : Does the same like session.save().
But session.save() return Serializable object(primary key), but session.persist() return void.
What are the general considerations
or best practices for defining your Hibernate persistent classes?
- You must have a default no-argument constructor for your persistent classes and there should be getXXX() (i.e accessor/getter) and setXXX( i.e. mutator/setter) methods for all your persistable instance variables.
- You should implement the equals() and hashCode() methods based on your business key and it is important not to use the id field in your equals() and hashCode() definition if the id field is a surrogate key (i.e. Hibernate managed identifier). This is because the Hibernate only generates and sets the field when saving the object.
- It is recommended to implement the Serializable interface. This is potentially useful if you want to migrate around a multi-processor cluster.
- The persistent class should not be final because if it is final then lazy loading cannot be used by creating proxy objects.
Difference between session.update() and session.lock() in Hibernate ?
- Both of these methods and saveOrUpdate() method are intended for reattaching a detached object.
- The session.lock() method simply reattaches the object to the session without checking or updating the database on the assumption that the database in sync with the detached object.
It is the best practice to use either session.update(..) or session.saveOrUpdate().
Use session.lock() only if you are absolutely sure that the
detached object is in sync with your detached object or if it does not matter because
you will be overwriting all the columns that would have changed later on within the same transaction.
saveOrUpdate vs merge
1. If you retrieve an object and close session (the object is detached),
2. Then you open session and retrieve again( the second copy is managed by session)
3. try to save the first copy of the same object, hibernate will throw exception (not NonUniqueObjectExceptin)
4. Because 2 object is the same thing but one is manged, the other is not.
In this case, we can use merge to copy the value from object 1 -> object 2
http://www.stevideter.com/2008/12/07/saveorupdate-versus-merge-in-hibernate/
SaveOrUpdate() vs merge()
-
if the object is already persistent in this session, do nothing
if another object associated with the session has the same identifier, throw an exception
if the object has no identifier property, save() it
if the object's identifier has the value assigned to a newly instantiated object, save() it
if the object is versioned (by a <version> or <timestamp>), and the version property value is the same value assigned to a newly instantiated object, save() it, otherwise update() the object
-
if there is a persistent instance with the same identifier currently associated with the session, copy the state
of the given object onto the persistent instance
if there is no persistent instance currently associated with the session, try to load it from the database, or
create a new persistent instance the persistent instance is returned, the given instance does not become associated with the session, it remains detached
Difference between list() and iterate() i9n Hibernate?
-
If instances are already be in the session or second-level cache iterate() will give better performance.
If they are not already cached, iterate() will be slower
than list() and might require many database hits for a simple query.
Difference between load() and get()
-
load() will throw an unrecoverable exception if there is no matching database row.
get() will return null if there is no matching database row.
当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为refresh是从hibernate的session中去重新取object,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下 (copy from here: http://chenying.blog.51cto.com/614874/134702
)
分享到:
相关推荐
### J2EE面试问题:核心Java、EJB、JSP、Servlets、Struts、Hibernate、JDBC及相关信息 #### 一、核心Java - **基础知识**:熟悉面向对象编程的基本概念如封装、继承和多态;掌握类与对象的概念;了解接口和抽象类...
"java_interview_questions.zip"这个压缩包中包含了一份名为"java_interview_questions.pdf"的文件,很可能是一个详细的Java面试问题大全。这份文档通常会涵盖各种Java核心技术、框架、设计模式以及最佳实践等主题。...
对于互联网行业的面试,面试官可能还会关注Java在Web开发中的应用,如Servlet、JSP、Spring框架、Hibernate等ORM工具,以及微服务、分布式系统和云计算相关的技术,比如Docker、Kubernetes和AWS服务的使用。...
7. **Interview Questions**(面试问题): 这个部分可能包含各种技术面试问题,涵盖了上述所有领域,旨在测试候选人的理论知识、实践经验和问题解决能力。面试问题可能包括算法题、Java编程题、Spring Boot和...
同花顺java面试笔试题Java、J2EE、JSP、Servlet、Hibernate 面试题 如果您喜欢该项目,请单击。 拉取请求受到高度赞赏。 目录 问:异常的类型有哪些? 解释 Java Exception 类的层次结构? 异常是一种错误事件,它...
Java面试问题和答案 提起收藏 Java Sprint Boot 数据库(包括SQL / PostgresSQL / MongoDB,JPA,Hibernate等) 最受欢迎的Java面试问答 最受欢迎的Spring Boot面试问答 最受欢迎的数据库面试问答
同花顺java面试笔试题Java、J2EE、JSP、Servlet、Hibernate 面试题 如果您喜欢该项目,请单击。 拉取请求受到高度赞赏。 目录 问:异常的类型有哪些? 解释 Java Exception 类的层次结构? 异常是一种错误事件,它...
java面试题笔试题Java、J2EE、JSP、Servlet、Hibernate 面试题 如果您喜欢该项目,请单击。 拉取请求受到高度赞赏。 目录 问:异常的类型有哪些? 解释 Java Exception 类的层次结构? 异常是一种错误事件,它可能在...
java中异常的笔试题Java、J2EE、JSP、Servlet、Hibernate 面试问答 如果您喜欢该项目,请单击。 拉取请求受到高度赞赏。 目录 问:异常的类型有哪些? 解释 Java Exception 类的层次结构? 异常是一种错误事件,它...
接下来,"Great summary of the interview questions.pdf"可能包含以下类型的面试题目: 1. **基础概念**:如Java的历史、特点、与C++的区别等。 2. **数据结构与算法**:面试中常见的问题,如数组、链表、排序和...
Chapter 6: Java Interview Questions – lastly, I have provided you with a comprehensive list of questions that you could be asked in an interview for a Java programming job. I also provide the answers...
关于项目最初,它是来自Java访谈问题的存储库。 当前,它更像是具有有用链接的知识库。目录核心 馆藏 并发弹簧 Servlet 冬眠 吉特 Maven Gradle微服务 JavaScript Linux 模式 DDD 建筑休息 SQL测试 交易次数 兔子MQ ...
文件名“IT+Interview+Questions.chm”表明这可能是一个帮助文件,包含了各种IT面试问题,可能覆盖了多个技术领域,比如网络,操作系统,数据库等。而“java面试题大全(j2se-_jsp-_ssh-_oracle).doc[1].pdf”则明确...
从压缩包子文件的文件名称“IT+Interview+Questions.chm”来看,这是一个CHM(Compiled Help Manual)文件,它是微软的一种帮助文件格式,通常包含组织良好的文本、图像和超链接,方便用户查阅。这个文件很可能将...
- Hibernate:ORM框架,对象关系映射原理。 这些知识点是Java开发者面试中常见的问题,通过深入理解和实践,可以提高面试成功率,并为日后的开发工作打下坚实的基础。在准备面试时,不仅要掌握理论知识,还要有...