JavaNinja's Profile
Ninja
100000380
Points

Questions
185

Answers
186

  • Ninja Asked on 18th September 2018 in Hibernate.

    A sorted collection is sorting a collection by utilizing the sorting methods provided by the Java collections. The sorting is done in the memory of JVM that is running hibernate after the data being read from database using Java comparator. This is efficient way to sort when collection is small.

    An ordered collection is sorting a collection by specifying order-by clause or using OrderBy annotation when retrieval. This is more efficient when the collection is very large. With ordered collection you are saving process time allowing RDBMS sorting data in a fast-way, rather than ordering data in Java once received. Furthermore order-by clause or OrderBy annotation does not force you to use SortedSet or SortedMap collection. You can use any collection like HashMap, HashSet, or even a Bag, because hibernate will use internally a LinkedHashMap, LinkedHashSet or ArrayList respectively.

    This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.

    • 2738 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 17th September 2018 in Hibernate.

    Criteria is a simplified API for retrieving entities by composing Criterion objects. This is a very convenient approach for functionality like “search” where there is a variable number of conditions to be placed upon the result set. Complex Hibernate queries can be generated on-the-fly using Criteria API.

    A Criteria object is created using the createCriteria() method in the Hibernate session object:

    Criteria criteria = session.createCriteria(Student.class);
    criteria.setMaxResults(10);
    List students = criteria.list();
    

    Once created, Criterion instances are usually obtained via the factory methods on Restrictions.

    List students = session.createCriteria(Student.class).add(Restrictions.like("studentName", "James%")).list();
    

    This answer accepted by JavaNinja. on 17th September 2018 Earned 15 points.

    • 1399 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 17th September 2018 in Hibernate.

    The update() method is used to save the object when the session object does not contain a persistent instance with the same identifier. The method will throw an exception if there is already a persistence instance associated with the session. The merge() method is used to merge the modifications without considering about the state of the session. It will just update without considering the session state.

    This is explained in the code snippet below:

    // In the first session
    Student student = (Student) firstSession.load(Student.class, 1001);
    student.setStudentGrade("A+");
    .........
    session.close();
    // student is detached
    Student studentTwo = (Student) secondSession.load(Student.class, 1001);
    studentTwo.setStudentGrade("B");
    ..........
    secondSession.update(student);
    // Update here will it will throw an exception like org.hibernate.NonUniqueObjectException
    secondSession.merge(student);
    // Merge will simply update without concern about the session</p>
    

    Use update() if you are certain that the session does not contain an already persistent instance with the same identifier. Use merge() if you want to merge your modifications at any time without consideration of the state of the session.

    This answer accepted by JavaNinja. on 17th September 2018 Earned 15 points.

    • 1352 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 17th September 2018 in Hibernate.

    An object is hibernate can exist in one of the three states of the life cycle – transient, persistent and detached.

    When an object is instantiated, it is not associated with a session and it is not persisted directly in the database and it is said to be in transient state. When an object is associated with a session or when the transient object is made persistent, the object has a representation in the database and an identifier value and is said to be in persistent state.

    When the session object is closed, the association is lost with the persistence manager and the object is said to be in detached state.

    Student student = new Student();
    student.setStudentId(1234);
    student.setStudentName("Nitin");
    // student object is in a transient state
    Long id = (Long) session.save(student);
    // session is an object of Hibernate Session
    // student object is now in persistent state
    
    ....
    session.close();
    // student is now in detached state
    

    This answer accepted by JavaNinja. on 17th September 2018 Earned 15 points.

    • 2130 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 17th September 2018 in Hibernate.

    Hibernate supports two main levels of cache:

    1. First-level Cache – This is the default cache associated with the Session object. Hibernate uses this cache on a per-transaction basis. Mainly it reduces the number of SQL queries it needs to generate within a given transaction. Instead of updating after every modification in the transaction, it updates only at the end of the transaction.

    2. Second-level Cache – This cache is associated with the SessionFactory object. To reduce database traffic, second level cache keeps loaded objects with the SessionFactory between transactions. These objects are not bound to a single user, available at the application level. Since the objects are available at the cache, each time a query returns an object, one or more database transactions can be avoided.

    There is one more cache – Query level Cache is supported and this is more for caching query results which can be used along with second level cache for improved performance.

    This answer accepted by JavaNinja. on 17th September 2018 Earned 15 points.

    • 1316 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 17th September 2018 in Hibernate.

    The load() method may return a proxy instead of a real persistent instance. A proxy is a placeholder that triggers the loading of the real object when it’s accessed for the first time; On the other hand, get() never returns a proxy.

    Choosing between get() and load() is easy: If you’re certain the persistent object exists, and nonexistence would be considered exceptional, load() is a good option. If you aren’t certain there is a persistent instance with the given identifier, use get() and test the return value to see if it’s null. Using load() has a further implication: The application may retrieve a valid reference (a proxy) to a persistent instance without hitting the database to retrieve its persistent state. So load() might not throw an exception when it doesn’t find the persistent object in the cache or database; the exception would be thrown later, when the proxy is accessed.

    This answer accepted by JavaNinja. on 17th September 2018 Earned 15 points.

    • 1346 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 17th September 2018 in Hibernate.

    Hibernate session’s get() method is used to retrieve an object from the database using a unique Id. The method returns null if the object is not found in the database. If the object is not available in cache, but exists on database, it may involve several data access calls to database returning a completely initialized object.

    Hibernate session’s load() method is also used to retrieve an object from the database using a unique Id. However this method throws an exception if the object is not available in the database. If the object is not available in cache, but exists on database, load() method can return proxy in place providing lazy initialization which results in better performance.

    This answer accepted by JavaNinja. on 17th September 2018 Earned 15 points.

    • 1235 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 17th September 2018 in Hibernate.

    Session object represents a unit of work in Hibernate that represents conversation between application and database. It is used to maintain connection with database. Session object is referred to as persistence manager since it is responsible for storing and retrieving object to and from database.

    Session object is single threaded and hence it is not thread-safe. It can’t be shared between multiple threads.

    Session object is created with the SessionFactory object:

    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();

    This answer accepted by JavaNinja. on 17th September 2018 Earned 15 points.

    • 1408 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 17th September 2018 in Hibernate.

    SessionFactory in Hibernate is a factory object used to create a session instance. It is part of org.hibernate package. It is a multithreaded object and hence it is thread-safe. Many threads can access it concurrently.

    SessionFactory can be created through the hibernate configuration file (hibernate.cfg.xml) which determines the hibernate mapping file (.hbm) to be loaded.  These files contain the properties required for configuring the database and the tables.

    SessionFactory can be created as follows:

    Configuration cfg=new Configuration();
    cfg=cfg.configure();
    

    // When you invoke the configure() method, the framework looks for hibernate.cfg.xml and  for hibernate mapping file (.hbm file).

    SessionFactory sc=cfg.buildSessionFactory();
    

    // When you invoke the buildSessionFactory() method on the configuration object, the SessionFactory object is created.

    This answer accepted by JavaNinja. on 17th September 2018 Earned 15 points.

    • 1438 views
    • 1 answers
    • 0 votes
  • Ninja Asked on 17th September 2018 in Hibernate.

    Hibernate is a simple, flexible and powerful ORM framework to map Java classes to database tables. Hibernate itself takes care of this mapping using XML files so developer does not need to write code for this.

    Benefits of Hibernate are:

    1. Supports POJO based persistence
    2. Is database Independent
    3. Supports powerful query language Hibernate Query Language (HQL) which is database independent. This is expressed in a familiar SQL like syntax and includes full support for polymorphic queries. Hibernate also supports native SQL statements.
    4. Supports Inheritance, Association and Relationships
    5. Provides powerful Hibernate Cache which helps in increasing the application performance
    6. Provides improved Performance and maintainability

    This answer accepted by JavaNinja. on 17th September 2018 Earned 15 points.

    • 1342 views
    • 1 answers
    • 0 votes