Answers to my Hibernate/Struts 2 Questions

1. How is session factory created from the hibernate.cfg.xml?

    Session factory creates or opens a session to the database. Usually, an application has a single SessionFactory. Its behaviours are controlled by properties supplied at configuration time, through the hibernate.cfg.xml and is instantiated through this statement:

AnnotationConfiguration().configure().buildSessionFactory(), which is usually placed in a Hibernate util/helper class. SessionFactory is immutable, meaning, it is not subject or susceptible to change and is a thread safe object intended to be shared by all application threads.

2. Why should serializable be implemented by the model classes?
    According to Wikipedia, serialization is the process of converting a data structure or object state into a format that can be stored (ex., in a file or memory buffer, or transmitted across a network) and "resurrected" later in the same or another computer environment. When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object.
    Serializable means that the object can be converted to a byte stream and that byte stream can also be reverted back to a copy of that object (deserialization). According to the Java API, a Java object is serializable if its class or any of its superclasses implement either the interface or its subinterface,
    According to one of the posts on StackOverFlow, the model objects must be serializable so that Hibernate can pass it to the database layer.
3. What are the uses of Annotations? Why should I put annotations in the model class? @Entity and @GeneratedValue @Column?
    Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate. They can be applied to a program’s declarations of classes, fields, methods, and other program elements (from Java API).
@GeneratedValue – provides for the specification of generation strategies for the values of primary keys. – may be applied to a primary key property or field of an entity or mapped superclass in conjunction with the Id annotation.
    In my experience with experimenting with the working Hibernate(with MySQL) sample, it is important that the correct table name is specified for the @Entity annotation, (as well as the correct column names for the @Column annotation) so that Hibernate can make the proper connections/associations with the table/column – for and session.load() to work correctly.
4. Why do I have to create a serialVersionUID?
    According to, the serialVersionUID is a universal version identifier for a Serializable class. Deserialization uses this number to ensure that a loaded class corresponds exactly to a serialized object. The aforementioned site also gives guidelines for using and generating serialVersionUIDs.
5. What does session.load(class,identifier) do?
    session.load is a method in Hibernate’s API that retrieves a single instance of a class, identified by its unique identifier. It is similar to session.get(class, identifier). They can both use the cache in retrieving an object, avoiding database hit if the object is already in the cache. However, their difference is that if load() can’t find the object in the cache nor in the database, an exception is thrown whereas get() returns null if the object is not found.
6. How does session.createQuery(String) work?


   According to, there are actually 3 ways to create a query in Hibernate:

    a. session.createQuery() – uses HQL (Hibernate Query Language) syntax. To read more about HQL, refer to this link
        ex: Query query = session.createQuery("from Student s where like ‘k%’");

    b. session.createSQLQuery() – uses the native SQL syntax
        ex: Query query = session.createQuery("Select * from Student");

    c. session.createCriteria() – creates Criteria object for setting the query parameters, useful for those who don’t want to write the query by hand. You can specify  any type of complicated syntax using the Criteria API.
       ex: Criteria criteria = session.createCriteria(Student.class);    



Author: Mylene

Disclaimer: My postings reflect my own views and do not necessarily represent the views of my employer, Accenture. I'm proud to be Filipina and a Bisaya. I am dedicated, motivated, and results-driven. I am an Aries. Bullheaded. Stubborn. I am a leader. I am a software engineer. I am a child of God. I am living and loving life! :) Leave a comment and subscribe!

Leave a comment... :)

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s