Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Hibernate, Spring & Struts Interview Questions You'll Most Likely Be Asked
Hibernate, Spring & Struts Interview Questions You'll Most Likely Be Asked
Hibernate, Spring & Struts Interview Questions You'll Most Likely Be Asked
Ebook221 pages2 hours

Hibernate, Spring & Struts Interview Questions You'll Most Likely Be Asked

Rating: 0 out of 5 stars

()

Read preview

About this ebook

 

  • 300 Hibernate, Spring & Struts Interview Questions
  • 78 HR Interview Questions 
  • Real life scenario based questions
  • Strategies to respond to interview questions
  • 2 Aptitude Tests
<
LanguageEnglish
Release dateDec 15, 2016
ISBN9781946383037
Hibernate, Spring & Struts Interview Questions You'll Most Likely Be Asked

Read more from Vibrant Publishers

Related to Hibernate, Spring & Struts Interview Questions You'll Most Likely Be Asked

Titles in the series (33)

View More

Related ebooks

Programming For You

View More

Related articles

Reviews for Hibernate, Spring & Struts Interview Questions You'll Most Likely Be Asked

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Hibernate, Spring & Struts Interview Questions You'll Most Likely Be Asked - Vibrant Publishers

    Hibernate Interfaces

    1: Explain Database Transaction management using Transaction Interface.

    Answer:

    In Hibernate, every transaction is handled by a session object. It controls the transactions and hides it from the outer world. To make sure the transaction completes successfully and is committed upon success and is rolled back upon failure, the entire transaction happens within a try – catch block. Within the try block, the session begins the transaction and continues with the processes and finally commits it. If anything goes wrong, the try block throws an error and under the exception handling part, a rollback is issued for the transaction. And finally, the session is closed. This is an efficient way of handling database transactions using Hibernate.

    2: What are the core interfaces available in hibernate?

    Answer:

    The core interfaces available in hibernate are:

    a) Session: used to store and retrieve the objects and they are not thread-safe

    b) SessionFactory: one object being created per application

    c) Criteria: used to provide a conditional search over resultset

    d) Query: obtained by invoking createQuery() method of Session

    e) Configuration: used to specify the hibernate mapping file’s location

    f) Transaction: used to perform several database operations

    3: What is SessionFactory? Is it thread-safe object?

    Answer:

    SessionFactory is an interface creating session instances. Threads servicing the client requests obtain the session instances from the SessionFactory. SessionFactory is created only once for each application. More than one thread can access the SessionFactory concurrently since it is thread-safe object.

    4: How will you create SessionFactory object?

    Answer:

    SessionFactory object can be created using buildSessionfactory() method.

    import org.hibernate.SessionFactory;

    import org.hibernate.cfg.Configuration;

    SessionFactory sf = new Configuration().configure().buildSessionFactory();

    5: What is Session? Is it a thread safe object?

    Answer:

    Session is obtained from SessionFactory and not thread-safe, meaning that it cannot be shared between threads. It is a single unit of work with the database. To avoid creating multiple sessions, ThreadLocal can be used. Get current session from the ThreadLocal object using get(). If no sessions are available, get a new session from SessionFactory object using openSession() method and set it in ThreadLocal.

    6: Explain about Criteria in hibernate.

    Answer:

    a) Criteria in hibernate are used to create dynamic queries to execute

    b) Criteria instances are obtained from session object using createQuery() method

    c) Criteria mycriteria = session1.createCriteria(Empl.class)

    *****

    Hibernate Configuration

    7: How do you configure hibernate?

    Answer:

    Hibernate could be configured in the following methods:

    a) Create hibernate configuration file hibernate.cfg.xml to provide hibernate configuration properties

    b) Create hibernate mapping file say emp.hbm.xml to provide hibernate mapping details i.e., mapping tables and java objects

    c) Configuration class uses these two files to create SessionFactory which in turn creates session instances

    8: What are the important tags of hibernate configuration file (hibernate.cfg.xml)?

    Answer:

    The important tags of hibernate configuration file are:

    ..> ...

    ...

    a) DTD: doctype

    b) Configuration of JDBC connection: driver class, url, username, password

    c) Dialect: specify the type of sql to be generated

    d) Size of Connection Pool

    e) Specify hbm2ddl.auto: automatic generation of database schema

    f) Map hbm.xml files: include Hibernate Mapping files

    9: Why column attribute is required if the property name is date?

    Answer:

    If the column attribute is not given hibernate will map the column -name as the property name. However if date is given as property name, column should be explicitly given since date is a keyword.

    date column=created_date/>

    10: How will you get hibernate statistics?

    Answer:

    Hibernate statistics could be obtained by:

    a) using getStatistics() method of SessionFactory

    b) SessionFactory.getStatistics();

    11: How will you make generated sql to be displayed in console?

    Answer:

    Generated sql could be displayed in console by setting show_sql property to true in the hibernate configuration file.

    show_sql > true

    12: How are the columns of the database mapped with the java class properties in hibernate?

    Answer:

    The columns of the database are mapped with the java class properties in hibernate as follows:

    a) Write POJO (getters and setters) class

    b) Create hibernate mapping file hibernate.cfg.xml where mapping between table columns and the class properties are specified

    empl table=empl_tabl>

    name column=empl_name>

    age column=empl_age/>

    dept cascade=all column=dept_Id/>

    13: If you want to insert data for few columns into a large table with hundreds of columns, hibernate will generate the insert sql query at run time containing all the table columns which will create performance issue. How will you make the hibernate to generate dynamically generated sql queries containing only the necessary columns? For instance, it should not include the null columns / property values.

    Answer:

    We can make the hibernate to generate dynamically generated sql queries using dynamic-insert=true attribute in the class mapping. Default option is false.

    .. table=UserDetails catalog=ks dynamic-insert=true >

    ...

    14: What is the flow of hibernate communication with database?

    Answer:

    The flow of hibernate communication with database is as follows:

    a) First, load hibernate configuration file and create configuration object. Using configuration object load all hibernate mapping files.

    b) Create SessionFactory from the Configuration object.

    c) Create Session from the SessionFactory object.

    d) Create the HQL query.

    e) Run the query which retrieves list of java objects.

    15: How will you configure Sequence generated primary key?

    Answer:

    We can configure sequence generated primary key by using tag.

    emp_Id name=empId type=java.lang.Long>

    sequence>

    myseq> emp_id_seq

    16: How will you change one relational database to another database without code changes?

    Answer:

    We can change a relational database to another using hibernate SQL dialect. Hibernate will generate sql queries based on the dialect defined in the configuration file.

    dialect> org.hibernate.dialect.MySQLDialect

    17: What is dynamic-insert and dynamic-update option in the class mapping?

    Answer:

    Dynamic-insert and dynamic-update option in the class-mapping are:

    a) dynamic-insert=true/false: Used to decide whether to include the columns having null properties, in the dynamically generated sql INSERT statement

    b) dynamic-update=true/false: Used to decide whether to include the null columns, in the dynamically generated sql UPDATE statement

    18: How will you configure Hibernate to access the instance variables directly without using setter method?

    Answer:

    We can configure hibernate to access the instance variables directly without using setter method as follows:

    a) Using access=field attribute in the tag (mapping file)

    b) uname access=field/>

    19: What is Automatic Dirty checking in hibernate?

    Answer:

    Automatic Dirty checking in hibernate:

    a) means that Hibernate persists the data (or updates the database) automatically if the state of the object is modified inside a transaction when the transaction is committed and the session is opened

    b) means no need for explicit update statements

    20: Write down a sample code for Automatic Dirty checking.

    Answer:

    Automatic Dirty checking Sample Code:

    Session session1 = sessionFactory1.openSession();

    Transaction tx1 = session1.beginTransaction();

    User user = (User)session1.get(User.class, 5L);

    user.setUname(Shobhana);

    tx1.commit(); //data is synchronized with database

    session1.close();

    21: How hibernate is database independent and what are the changes required?

    Answer:

    Hibernate is database independent as by changing the below properties, databases can be replaced without much changes.

    hibernate.dialect>

    org.hibernate.dialect.Oracle9Dialect

    hibernate.connection.driver_class>

    oracle.jdbc.driver.OracleDriver

    22: How will you include hibernate mapping file in the hibernate configuration file?

    Answer:

    We can include hibernate mapping file in the hibernate configuration file using tag and resource attribute.

    user.hbm.xml />

    addr.hbm.xml />

    *****

    Criteria Queries

    23: What are the ways in which object can be fetched from the database in hibernate?

    Answer:

    The ways in which object can be fetched from the database in hibernate are:

    a) using Criteria API

    b) using Standard SQL

    c) using HQL

    d) using identifier

    24: What is the use of Restrictions class?

    Answer:

    Restrictions class is used to implement comparison operations in criteria query. Using add() method of criteria query object, Restrictions can be added to the criteria query.

    Criteria mycriteria = session1.createCriteria(Student.class);

    mycriteria.add ( Restrictions.eq(age, 18) );

    25: How will you write criteria query to retrieve records having dept_name containing hr and emp_salary between 20000 and 30000?

    Answer:

    The required criteria query can be written as follows:

    List Employees = session1.createCriteria(Empl.class)

    .add( Restrictions.like(dept_name, hr%) )

    .add(Restrictions.between(emp_salary, 20000, 30000) )

    .list();

    26: How will you sort the employee class in descending order by employee salary using Criteria query?

    Answer:

    The employee class can be sorted in descending order by employee salary using Criteria query using org.hibernate.criterion.Order class of Criteria API.

    Criteria mycriteria = session1.createCriteria(Empl.class);

    mycriteria.addOrder(Order.desc ( emp_salary));

    27: How will you find out the maximum salary from Employee class?

    Answer:

    The maximum salary from employee class can be found using org.hibernate.criterion.Projections class which is used to fetch minimum, maximum, average, distinct count of the property values.

    Criteria mycriteria = session1.createCriteria(Empl.class);

    mycriteria.setProjection ( Projections.max(emp_salary) );

    28: What are the methods available in Projections class?

    Answer:

    The methods available in Projections class are:

    a) Projections.rowCount(): to fetch total row count of property values

    b) Projections.countDistinct(): to fetch distinct count

    c) Projections.avg(): to fetch average value

    d) Projections.max(): to fetch maximum value

    e) Projections.min(): to fetch minimum value

    f) Projections.sum(): to fetch sum of property values

    29: How will you implement pagination using criteria query?

    Answer:

    We could implement pagination using setFirstResults() and setMaxResults() methods. For instance, fetching records, starting from 20th record and retrieve the next 40 records from the database.

    Criteria mycriteria = session1.createCriteria(Empl.class);

    mycriteria.setMaxResults(20);

    mycriteria.setFirstResult(40);

    30: What are the disadvantages of Criteria query?

    Answer:

    The disadvantages of Criteria query are:

    a) Performance issue: No control over hibernate generated query. If the generated query is slow, it will be difficult to tune the query.

    b) Maintenance issue: Sql queries are written or scattered in java code. If any issues, we need to find out the problematic query in the application. Named queries which are stored in the hibernate mapping file are the best choice.

    31: How is the Primary Key created using Hibernate?

    Answer:

    The primary key in Hibernate is created using tag in hibernate mapping file.

    .. table =..>

    userId type=java.lang.String >

    user_Id length=20/>

    32: How do you create hibernate generated Primary Key?

    Answer:

    Hibernate generated Primary Key is created using tag which is used to specify how the primary key should be created in the database.

    .. table =..>

    addrId type=java.lang.Integer>

    addr_id />

    increment

    Enjoying the preview?
    Page 1 of 1