Showing posts with label Hibernate interview Question. Show all posts
Showing posts with label Hibernate interview Question. Show all posts

Sunday, November 24, 2019

Difference Betwixt Save(), Saveorupdate() Too Persist() Inwards Hibernate Session

Session interface inward Hibernate provides a yoke of methods to motion an object from novel or transient soil to persistent soil e.g. save(), saveOrUpdate() together with persist() are used to shop an object into the database, only at that spot are to a greater extent than or less pregnant differences betwixt them. The Session.save() method does an INSERT to shop the object into the database together with it too render the identifier generated past times the database. On the other hand, saveOrUpdate() tin survive used to reattach a detached object inward Hibernate Session i.e. it tin produce INSERT or UPDATE depending upon whether object exists inward database or not. The tertiary method, persist() is similar to Session.save() i.e. it is used to motion a transient object to the persistent soil past times storing it into the database only it doesn't render the database identifier. This was the basic departure betwixt save() vs saveOrUpdate() vs persist() method together with advise when to piece of occupation which method. It is too 1 of the oft asked Hibernate interview questions on Java Web developer labor interviews. Knowing this especial aid you lot to produce good on your interview. Btw, if you lot choose non read already hence you lot must read Java Persistence alongside Hibernate, best mass to larn these kinds of subtle details nearly Hibernate.



Session.save() vs Session.saveOrUpdate() vs Session.Persist

As I said, all iii methods save(), persist() together with saveOrUpdate belongs to Hibernate's Session interface. They all are used to convey a novel object into persistence context or a detached object, which was associated alongside a Hibernate Session inward past times only currently it's non associated into active persistence context. It is too 1 of the most popular Hibernate interview questions. Let's run into yoke of to a greater extent than differences betwixt save(), persist() together with saveOrUpdate() methods of Session interface.




1) The principal departure between save() together with saveOrUpdate() method is that save() method performs an INSERT performance to shop the object into the database, only INSERT volition neglect if the primary key is already persistent i.e. object already exists inward the database. This is why, you lot should alone telephone band save() alongside an absolutely novel object which doesn't choose whatsoever database identifier. Calling save() alongside the detached object volition fail. This is reverse of saveOrUpdate() method, which tin produce either INSERT or UPDATE SQL query depending upon whether an object exists inward the database or not. The saveOrUpdate() method kickoff executes a SELECT query to decide if it needs to produce an INSERT or UPDATE operation.


2) Another fundamental departure betwixt save() together with saveOrUpdate() method is that sometime is used to convey a transient object to persistent state only saveOurUpdate() tin convey both transient (new) together with detached (existing) object into persistent state. It is often used to re-attach a detached object into Session.

Here is an object's lifecycle together with soil transition diagram inward Hibernate which shows that save() together with saveOrUpdate() tin motion an object from transient to persistent state.

Serializable object only persist() method doesn't render anything. It's render type is void.

For example:
System.out.println(session.save(aCoin));

volition impress the generated primary key, only the next trouble volition throw a compile fourth dimension fault because persist()'s render type is void.

System.out.println(session.persist(aCoin));


That's all nearly difference betwixt save(), saveOrUpdate() together with persist() method of Hibernate Session interface. Now you lot know the basic departure betwixt them together with too learned when to piece of occupation the save(), saveOrUpdate() together with persist() method. Use the save() method to shop novel object into database together with when you lot demand the generated database identifier, otherwise piece of occupation persist() method. You tin piece of occupation saveOrUpdate() to reattach a detached object into Hibernate Session.

Further Learning
Introduction To Hibernate
Spring together with Hibernate for Beginners
Hibernate Interview Questions Preparation Course

Thanks for reading this article, if you lot similar this article together with interview enquiry hence delight percentage alongside your friends together with colleagues. If you lot choose whatsoever enquiry or feedback hence delight driblet a comment.

Friday, November 1, 2019

Top Xx Hibernate Interview Questions For Coffee J2ee Programmers

Hibernate is i of the most pop persistent frameworks inward the Java world. Hibernate offers an object to relational (ORM) solution which frees Java developers from writing tedious, hard to read in addition to cluttered JDBC code converting SQL columns into Object properties. Apart from freeing Java developer from writing JDBC in addition to database interaction code, Hibernate likewise offers the out-of-box solution on caching, proxying in addition to lazy loading which drastically improves the surgical operation of your Java Web application. Given it's of import inward Java Web application evolution patch Hibernate has acquire of i of the most sought-after science in addition to goes manus inward manus amongst Spring framework. That's why Spring in addition to Hibernate interview questions are likewise rattling pop inward Java interviews, especially for spider web evolution positions.

Earlier I direct hold shared to a greater extent than or less Spring MVC interview questions in addition to due to pop demand, I am straight off sharing some 20 oft asked Hibernate questions from diverse Java interviews.

These questions are rattling useful for both the telephone in addition to the face-to-face circular of interviews in addition to both beginners in addition to experienced Java developers upwards to 2 to five years tin do goodness from these questions.

If you lot are actively preparing for Java interviews than you lot tin likewise banking enterprise check out Java Programming Interview Exposed, it non entirely contains interview questions from Spring in addition to Hibernate but likewise other of import Java topics e.g. heart Java, information construction in addition to algorithms, Servlet, JSP, JSF, in addition to pattern patterns.

Since no listing is consummate without user involvement, thus if you lot direct hold seen whatever practiced Hibernate questions on your interviews, which is non on this listing in addition to then experience costless to part amongst us.

You tin likewise part your ain questions for which you lot are looking answers or you lot experience it's a practiced inquiry to live asked on Java JEE programming interviews.

Btw, I await that you lot direct hold to a greater extent than or less Hibernate experience in addition to cognition before you lot cash inward one's chips through this listing of questions otherwise it would live quite dorsum in addition to forth for. If you lot are a beginner to Hibernate, I propose you lot to showtime cash inward one's chips through a course of education like Spring in addition to Hibernate for Beginners which provides all the key cognition inward quick time.

After that you lot tin meliorate sympathise the points interviewers are looking in addition to these questions volition likewise brand feel to you.




20 Hibernate Questions from Java JEE Interviews

Here is my selected listing of xx Hibernate based questions for Java developers. It contains questions from Hibernate fundamentals, one-to-one, in addition to one-to-many mappings, caching, Hibernate vs JDBC comparison, pros in addition to cons of Hibernate, known problems amongst Hibernate, in addition to Performance improvement.


1. What is Hibernate?
Hibernate is an ORM (Object-relational Mapping) framework, which allows the developer to concentrate on concern logic past times taking attention of the persistence of information past times itself. Java developer tin write code using object in addition to Hibernate tin direct hold attention of creating those object from information loaded from the database in addition to saving update dorsum to the database.


2. What are the advantages of Hibernate over JDBC? (detailed answer)
Apart from Persistence i.e. saving in addition to loading information from Database, Hibernate likewise provides the next benefits
1) Caching
2) Lazy Loading
3) Relationship management in addition to provides code for mapping an object to the data
4) The developer is costless from writing code to load/store information into the database.


3. Difference betwixt get() vs load() method inward Hibernate? (detailed answer)
This is i of the most oft asked Hibernate interview questions, I direct hold seen it several times. The key deviation betwixt the get() in addition to load() method is that load() volition throw an exception if an object amongst id passed to them is non found, but get() volition render null.

Another of import deviation is that charge tin render proxy without hitting the database unless required (when you lot access whatever attribute other than id) but get() always cash inward one's chips to the database, thus sometimes using load() tin live faster than the get() method.

Use the load() method, if you lot know the object exists, and get() method if you lot are non certain close the object's existence. See detailed answer)
The principal deviation betwixt save() in addition to persist() method is that, salve returns a Serializable object spell render type of the persist() method is void, thus it doesn't render anything.

Here is a overnice diagram which explains the land transition inward Hibernate:

 Hibernate is i of the most pop persistent frameworks inward the Java globe Top xx Hibernate Interview Questions for Java J2EE Programmers



7. What is the requirement for a Java object to acquire a Hibernate entity object? (detailed answer)
It should non live concluding in addition to must furnish a default, no-argument constructor. See the detailed respond to acquire to a greater extent than close the special requirement for a Java object to acquire a Hibernate Entity.



8. What are dissimilar types of caches available inward Hibernate? (detailed answer)
This is to a greater extent than or less other mutual Hibernate interview question. Hibernate provides the out-of-box caching solution but at that spot are many caches e.g. showtime marking cache, instant marking cache, in addition to query cache.

The showtime marking cache is maintained at Session marking in addition to cannot live disabled but the instant marking cache is required to live configured amongst external cache provider similar EhCache.


9. What is the deviation betwixt the showtime in addition to instant marking cache inward Hibernate? (detailed answer)
This is in i trial to a greater extent than follow-up of previous Hibernate interview question. The showtime marking cache is maintained at Session marking spell the instant marking cache is maintained at a SessionFactory marking in addition to shared past times all sessions. You tin read these books to acquire to a greater extent than close caching inward Hibernate.


10. Does Hibernate Session interface thread-safe inward Java? (detailed answer)
No, Session object is non thread-safe inward Hibernate in addition to intended to live used with-in unmarried thread inward the application.



11. Does SessionFactory thread-safe inward Hibernate? (detailed answer)
SessionFactory is both Immutable in addition to thread-safe in addition to it has only i unmarried event inward Hibernate application. It is used to do a Session object in addition to it likewise provides caching past times storing SQL queries stored past times multiple session.

The instant marking cache is maintained at SessionFactory level. This tin live a hard in addition to tricky inquiry for less experienced Java developers who are non familiar amongst thread-safety in addition to Immutability.


12. What is dissimilar betwixt Session in addition to SessionFactory inward Hibernate? (detailed answer)
This is to a greater extent than or less other pop Hibernate interview question, generally at a telephonic circular of interviews.

The principal deviation betwixt Session in addition to SessionFactory is that the sometime is a single-threaded, short-lived object spell afterward is Immutable in addition to shared past times all Session.

It likewise lives until the Hibernate is running. Another deviation betwixt Session in addition to SessionFactory is that sometime provides showtime marking cache spell SessionFactory provides the Second marking cache.


13. What is touchstone query inward hibernate? (detailed answer)
Criteria is a simplified API for retrieving entities past times composing Criterion objects likewise known every bit Criterion query.

This is a rattling convenient approach for functionality similar "search" screens where you lot tin filter information on multiple weather condition every bit shown inward the next example:

List books = session.createCriteria(Book.class) .add(Restrictions.like("name", "java%") ) .add(Restrictions.like("published_year", "2015")) .addOrder(Order.asc("name") ) .list();
This tin live a tough inquiry if you lot are non using Hibernate on a daily basis, I direct hold interviewed several Java developers who direct hold used Hibernate but doesn't know close Criterion query or API. You tin banking enterprise check out this listing of 5 Hibernate in addition to JPA courses for Java Programmers to acquire to a greater extent than close JPA in addition to Hibernate.


14. What are other ORM frameworks? Any option of Hibernate?
This is a full general question, sometimes asked to start the conversation in addition to other times to complete the interview. EJB in addition to TopLink from Oracle are 2 of the most pop option to Hibernate framework.


15. What is the deviation betwixt save() in addition to saveOrUpdate() method of Hibernate? (detailed answer)
Though both save() in addition to saveOrUpdate() method is used to shop an object into Database, the key deviation betwixt them is that salve tin entirely INSERT records but saveOrUpdate() tin either INSERT or UPDATE records.



16. What is deviation betwixt getCurrentSession() in addition to openSession() inward Hibernate? (detailed answer)
An interesting Hibernate interview inquiry every bit you lot mightiness direct hold used both getCurrentSession() in addition to openSession() to obtain an event of the Session object. I direct hold left this inquiry unanswered for you lot to respond or discovery an respond based on your experience.


17. What is Hibernate Query Language (HQL)? (detailed answer)
Hibernate query language, HQL is an object-oriented extension to SQL. It allows you lot to query, store, update, in addition to yell back objects from a database without using SQL.

This inquiry is likewise similar to the before inquiry close Criterion query, Java developers who direct hold non used Hibernate extensively volition non know much close features similar HQL in addition to Criterion.


18. When do you lot occupation merge() in addition to update() inward Hibernate? (detailed answer)
This is i of the tricky Hibernate interview questions. You should occupation update() if you lot are certain that the Hibernate session does non comprise an already persistent event amongst the same id in addition to occupation merge() if you lot desire to merge your modifications at whatever fourth dimension without considering the land of the session. See Java Persistence amongst Hibernate for to a greater extent than details.

 Hibernate is i of the most pop persistent frameworks inward the Java globe Top xx Hibernate Interview Questions for Java J2EE Programmers



19. The deviation betwixt sorted in addition to ordered collection inward Hibernate? (detailed answer)
The principal deviation betwixt sorted in addition to ordered collection is that sorted collection form the information inward JVM's heap retentivity using Java's collection framework sorting methods spell the ordered collection is sorted using gild past times clause inward the database itself.

Influenza A virus subtype H5N1 sorted collection is to a greater extent than suited for pocket-size dataset but for a large dataset, it's meliorate to occupation ordered collection to avoid OutOfMemoryError inward Java application.


20. How do you lot log SQL queries issued past times the Hibernate framework inward Java application?
You tin occupation the show_sql holding to log SQL queries issued past times the Hibernate framework, Just add together the next business inward your Hibernate configuration file:

<property name=”show_sql”> true </property>


21. What are the 3 states of a Hibernate Persistence object tin be? (detailed answer)
The Hibernate persistent or entity object tin alive inward the next 3 states:
1) transient
2) persistent
3) detached


22. What is the deviation betwixt the transient, persistent in addition to detached land inward Hibernate? (detailed answer)
New objects created inward Java programme but non associated amongst whatever hibernate Session are said to live inward the transient state.

On the other hand, an object which is associated amongst a Hibernate session is called Persistent object. While an object which was before associated amongst Hibernate session but currently it's non associate is known every bit a detached object.

You tin telephone band save() or persist() method to shop those object into the database in addition to convey them into the Persistent state. Similarly, you lot tin re-attach a detached object to hibernate sessions past times calling either update() or saveOrUpdate() method. See Spring in addition to Hibernate for Beginners to acquire to a greater extent than close persistence object's lifecycle inward Hibernate.


23. Which cache is used past times Session Object inward Hibernate? First marking or instant marking cache? (detailed answer)
Influenza A virus subtype H5N1 Session object uses the first-level cache. As I told before the instant marking cache is used at SessionFactory level. This is a practiced inquiry to banking enterprise check if the Candidate has been working inward hibernate or not. If he has non worked inward Hibernate from a long fourth dimension in addition to then he would acquire confused inward this question.


That's all inward this listing of Hibernate Interview inquiry for Java in addition to JEE developers. In this article, nosotros direct hold covered a lot of oft asked Hibernate inquiry for both beginners in addition to experienced Java developers from all of import topics of Hibernate framework e.g. Hibernate fundamentals, caching, collection mapping, surgical operation tuning, mutual issues in addition to Hibernate vs JDBC.

If you lot discovery that you lot lack cognition inward whatever exceptional area, I propose cash inward one's chips through next resources to fill upwards those gaps inward your knowledge.

Further Learning
Top xx Spring MVC Interview Questions 
Top xx Spring in addition to REST Interview Questions 
5 Courses to Learn Spring Boot in addition to Microservices
5 Hibernate in addition to JPA courses for Java Programmers
5 Free Courses to acquire Spring in addition to Spring Boot
Top five Spring in addition to Hibernate  Training courses
130+ Java Interview Questions from lastly five years
100+ Data Structure in addition to Algorithm Questions

Thanks for reading this article, if you lot similar this article in addition to interview inquiry in addition to then delight part amongst your friends in addition to colleagues. If you lot direct hold whatever inquiry or feedback in addition to then delight drib a comment.

Thursday, October 31, 2019

Difference Betwixt Showtime Degree Too Moment Degree Cache Inwards Hibernate

The primary difference betwixt outset degree in addition to bit degree cache inwards Hibernate is that the outset degree is maintained at the Session degree in addition to accessible entirely to the Session, spell the bit degree cache is maintained at the SessionFactory degree in addition to available to all Sessions. This means, you lot tin utilisation the outset degree cache to shop local data, i.e. the information which is needed past times the Session, in addition to you lot tin utilisation the second-level cache to shop global data, i.e. something which tin live shared across sessions. This is also i of the oftentimes asked Hibernate Interview questions in addition to accessible inwards both telephonic rounds every bit good every bit on confront to confront interview, inwards both fresher in addition to experienced degree interviews.

That's why it's of import for a Java Hibernate Developer to empathise how Caching industrial plant inwards Hibernate in addition to what is the role of unlike caches provided past times Hibernate. In this article, I'll explicate nearly the First in addition to Second degree caches, how they work, in addition to what are telephone commutation differences betwixt them.

Caching is i of the powerful features of Hibernate in addition to in all likelihood i of the most substantial reasons to utilisation the Hibernate framework. It allows developers to cook a to a greater extent than responsive spider web application past times minimizing the let on of database transactions.

Hibernate maintains unlike caches for the unlike purpose, e.g. outset degree cache at Session level, Second degree cache at the SessionFactory degree in addition to Query Cache to cache query in addition to its results (see Spring in addition to Hibernate for Beginners course on Udemy).




What is First Leven Cache inwards Hibernate?

As the mention suggests, the first-level cache is the outset cache hibernate consults earlier loading an object from the database. It is maintained at Session level, in addition to it's past times default enabled.

If you lot know, hibernate in addition to then you lot know that Session is the interface betwixt Hibernate in addition to Database. You charge objects using Session, e.g. past times calling get() or load() method or past times executing queries.

When you lot inquire charge method to render a Person (an object stored inwards database) with Id=1, outset fourth dimension it's loaded from database, that likewise lazily, when whatever method of that object is called other than getId(), side past times side fourth dimension if you lot charge the same object in addition to then Hibernate doesn't acquire to Database, instead it returns the object from outset degree cache maintained at Session level.

Similarly, when you lot update a particular object, Hibernate defer the database telephone telephone to combine multiple database transactions into one, this way, Hibernate improves the functioning of your spider web application.

The information inwards the outset degree cache is maintained every bit long every bit Session is open, every bit shortly every bit you lot closed the Session, all information is lost. Next fourth dimension fifty-fifty if you lot charge the same object, e.g. Person alongside the same id, Hibernate volition i time again acquire to Database to charge that object, provided Second-level Cache is disabled (see Java Persistence alongside Hibernate book).

difference betwixt outset degree in addition to bit degree cache inwards Hibernate Difference betwixt outset degree in addition to bit degree cache inwards Hibernate


Btw, Caching at Session degree has around retentiveness implication, specially if you lot are loading lots of large objects. Long-lived sessions alongside several large objects volition accept to a greater extent than retentiveness in addition to tin crusade out of retentiveness errors into your spider web application.


What is Second-Level Cache inwards Hibernate?

Now, let's come upward to the second-level cache. This is an optional Cache which Hibernate provides. Unlike outset degree cache which is accessible entirely to the session who maintains it, Second-level Cache is accessible to all Sessions.

This way if i Session loads an object, similar Person alongside id=1 in addition to Second session also loads the same object, entirely i database telephone telephone volition live made. Next session volition acquire the information from the Second-level Cache.

The Second Level cache is past times default disabled. Hibernate also doesn't furnish whatever caching implementation for that. Instead, it gives CacheProvider interface in addition to whatever 3rd political party Cache which implements CacheProvider interface tin live hooked every bit Second degree cache, e.g. EHCache or NCache.

You also demand to define Concurrency Strategy to live used along alongside Second degree cache, e.g. Transactions, read-write, nonstrict-read-write, or read-only.

If you lot are interested inwards implementation details, I propose you lot depository fiscal establishment tally out courses)
  • Difference betwixt First in addition to Second degree cache inwards Hibernate? (answer)
  • Difference betwixt get() in addition to load() method inwards Hibernate? (answer)
  • 5 Spring in addition to Hibernate Training Courses for Java developers (list)
  • 2 Books to Learn Hibernate inwards 2019 (books)
  • 5 Online Courses to larn Spring Boot inwards 2019 (courses)
  • 5 Books to Learn Spring Framework inwards 2019 (books)
  • Why Hibernate Entity shape should non live lastly inwards Java? (answer)
  • 10 Hibernate Questions from Java Interviews (list)
  • Top fifteen Spring Boot Interview Questions for Java Programmers (questions)
  • Top five Courses to larn Spring Framework in-depth (courses)
  • Thanks for reading this article. If you lot similar this Hibernate interview interrogation in addition to my explanation, in addition to then delight portion alongside your friends in addition to colleagues. If you lot conduct hold whatever feedback or questions, in addition to then delight driblet a note.