Spring and Hibernate Interview Qns

Spring MVC Execution Flow Diagram


Spring MVC 3.2 Execution Flow

Step 1: First request will be received by DispatcherServlet
Step 2DispatcherServlet will take the help of HandlerMapping and get to know the Controller class name associated with the given request
Step 3: So request transfer to the Controller, and then controller will process the request by executing appropriate methods and returns ModeAndView object (contains Model data and View name) back to the DispatcherServlet
Step 4: Now DispatcherServlet send the model object to the ViewResolver to get the actual view page
Step 5: Finally DispatcherServlet will pass the Model object to the View page to display the result
That’s it
You Might Also Like

Spring Interview Questions

What is Spring?

It is a lightweight, loosely coupled and integrated framework for developing enterprise applications in java.

What are the advantages of spring framework?

  1. Predefined Templates
  2. Loose Coupling
  3. Easy to test
  4. Lightweight
  5. Fast Development
  6. Powerful Abstraction
  7. Declarative support
More details...

What are the modules of spring framework?

  1. Test
  2. Spring Core Container
  3. AOP, Aspects and Instrumentation
  4. Data Access/Integration
  5. Web
More details...


What is IOC and DI?

IOC (Inversion of Control) and DI (Dependency Injection) is a design pattern to provide loose coupling. It removes the dependency from the program.

  1. public class Employee{  
  2. Address address;  
  3. Employee(){  
  4. address=new Address();//creating instance  
  5. }  

Now, there is dependency between Employee and Address because Employee is forced to use the same address instance.

Let's write the IOC or DI code.

  1. public class Employee{  
  2. Address address;  
  3. Employee(Address address){  
  4. this.address=address;//not creating instance  
  5. }  
  6. }  

Now, there is no dependency between Employee and Address because Employee is not forced to use the same address instance. It can use any address instance.

What is the role of IOC container in spring?

IOC container is responsible to:
  • create the instance
  • configure the instance, and
  • assemble the dependencies

What are the types of IOC container in spring?

There are two types of IOC containers in spring framework.
  1. BeanFactory
  2. ApplicationContext
More details...

What is the difference between BeanFactory and ApplicationContext?

BeanFactory is the basic container whereas ApplicationContext is the advanced container. ApplicationContext extends the BeanFactory interface. ApplicationContext provides more facilities than BeanFactory such as integration with spring AOP, message resource handling for i18n etc.

What is the difference between constructor injection and setter injection?
No.
Constructor Injection
Setter Injection
1)
No Partial Injection
Partial Injection
2)
Desn't override the setter property
Overrides the constructor property if both are defined.
3)
Creates new instance if any modification occurs
Doesn't create new instance if you change the property value
4)
Better for too many properties
Better for few properties.

What is autowiring in spring? What are the autowiring modes?

Autowiring enables the programmer to inject the bean automatically. We don't need to write explicit injection logic.
Let's see the code to inject bean using dependency injection.
  1. <bean id="emp" class="com.javatpoint.Employee" autowire="byName" />  
The autowiring modes are given below:

No.
Mode
Description
1)
no
this is the default mode, it means autowiring is not enabled.
2)
byName
injects the bean based on the property name. It uses setter method.
3)
byType
injects the bean based on the property type. It uses setter method.
4)
constructor
It injects the bean using constructor
The "autodetect" mode is deprecated since spring 3.




What are the different bean scopes in spring?

There are 5 bean scopes in spring framework.
No.
Scope
Description
1)
singleton
The bean instance will be only once and same instance will be returned by the IOC container. It is the default scope.
2)
prototype
The bean instance will be created each time when requested.
3)
request
The bean instance will be created per HTTP request.
4)
session
The bean instance will be created per HTTP session.
5)
globalsession
The bean instance will be created per HTTP global session. It can be used in portlet context only.

In which scenario, you will use singleton and prototype scope?

Singleton scope should be used with EJB stateless session bean and prototype scope with EJB stateful session bean.

What are the transaction management supports provided by spring?

Spring framework provides two type of transaction management supports:
  1. Programmatic Transaction Management: should be used for few transaction operations.
  2. Declarative Transaction Management: should be used for many transaction operations.

What are the advantages of JdbcTemplate in spring?

Less code: By using the JdbcTemplate class, you don't need to create connection,statement,start transaction,commit transaction and close connection to execute different queries. You can execute the query directly.
More details...

What are classes for spring JDBC API?

  1. JdbcTemplate
  2. SimpleJdbcTemplate
  3. NamedParameterJdbcTemplate
  4. SimpleJdbcInsert
  5. SimpleJdbcCall
More details...

How can you fetch records by spring JdbcTemplate?

You can fetch records from the database by the query method of JdbcTemplate. There are two interfaces to do this:
  1. ResultSetExtractor
  2. RowMapper

What is the advantage of NamedParameterJdbcTemplate?

NamedParameterJdbcTemplate class is used to pass value to the named parameter. A named parameter is better than ? (question mark of PreparedStatement).
It is better to remember.

What is the advantage of SimpleJdbcTemplate?

The SimpleJdbcTemplate supports the feature of var-args and autoboxing.

What is AOP?

AOP is an acronym for Aspect Oriented Programming. It is a methodology that divides the program logic into pieces or parts or concerns.
It increases the modularity and the key unit is Aspect.

What are the advantages of spring AOP?

AOP enables you to dynamically add or remove concern before or after the business logic. It is pluggable and easy to maintain.

What are the AOP terminology?

AOP terminologies or concepts are as follows:
  • JoinPoint
  • Advice
  • Pointcut
  • Aspect
  • Introduction
  • Target Object
  • Interceptor
  • AOP Proxy
  • Weaving

What is JoinPoint?

JoinPoint is any point in your program such as field access, method execution, exception handling etc.

Does spring framework support all JoinPoints?

No, spring framework supports method execution joinpoint only.

What is Advice?

Advice represents action taken by aspect.

What are the types of advice in AOP?

There are 5 types of advices in spring AOP.
  1. Before Advice
  2. After Advice
  3. After Returning Advice
  4. Throws Advice
  5. Around Advice

What is Pointcut?

Pointcut is expression language of Spring AOP.

What is Aspect?

Aspect is a class in spring AOP that contains advices and joinpoints.

What is Introduction?

Introduction represents introduction of new fields and methods for a type.

What is target object?

Target Object is a proxy object that is advised by one or more aspects.

What is interceptor?

Interceptor is a class like aspect that contains one advice only.

What is weaving?

Weaving is a process of linking aspect with other application.

Does spring perform weaving at compile time?

No, spring framework performs weaving at runtime.

What are the AOP implementation?

There are 3 AOP implementation.
  1. Spring AOP
  2. Apache AspectJ
  3. JBoss AOP

What is the front controller class of Spring MVC? See this

The DispatcherServlet class works as the front controller in Spring MVC.
More details...

What does @Controller annotation?

The @Controller annotation marks the class as controller class. It is applied on the class.

What does @RequestMapping annotation?

The @RequestMapping annotation maps the request with the method. It is applied on the method.

What does the ViewResolver class?

The View Resolver class resolves the view component to be invoked for the request. It defines prefix and suffix properties to resolve the view component.

Which ViewResolver class is widely used?

The org.springframework.web.servlet.view.InternalResourceViewResolver class is widely used.

Does spring MVC provide validation support?

Yes.


Hibernate Interview Questions

What is hibernate?

Hibernate is an open-source and lightweight ORM tool that is used to store, manipulate, and retrieve data from the database.

What is ORM?

ORM is an acronym for Object/Relational mapping. It is a programming strategy to map object with the data stored in the database. It simplifies data creation, data manipulation, and data access.

Explain hibernate architecture?

Hibernate architecture comprises of many interfaces such as Configuration, SessionFactory, Session, Transaction, etc.

Explain Carefully.

What are the core interfaces of Hibernate?

The core interfaces of Hibernate framework are:
  • Configuration
  • SessionFactory
  • Session
  • Query
  • Criteria
  • Transaction

Mention some of the advantages of using ORM over JDBC.

ORM has the following advantages over JDBC:
  • Application development is fast.
  • Management of transaction.
  • Generates key automatically.
  • Details of SQL queries are hidden.

Define criteria in terms of Hibernate.

The objects of criteria are used for the creation and execution of the object-oriented criteria queries.

List some of the databases supported by Hibernate.

Some of the databases supported by Hibernate are:
  • DB2
  • MySQL
  • Oracle
  • Sybase SQL Server
  • Informix Dynamic Server
  • HSQL
  • PostgreSQL
  • FrontBase

List the key components of Hibernate.

Key components of Hibernate are:
  • Configuration
  • Session
  • SessionFactory
  • Criteria
  • Query
  • Transaction

Mention two components of Hibernate configuration object.

Database Connection
Class Mapping Setup

How is SQL query created in Hibernate?

The SQL query is created with the help of the following syntax:
Session.createSQLQuery

What does HQL stand for?

Hibernate Query Language

How is HQL query created?

The HQL query is created with the help of the following syntax:
Session.createQuery

How can we add criteria to a SQL query?

A criterion is added to a SQL query by using the Session.createCriteria.

Define persistent classes.

Classes whose objects are stored in a database table are called as persistent classes.

What is SessionFactory?

SessionFactory provides the instance of Session. It is a factory of Session. It holds the data of second level cache that is not enabled by default.

Is SessionFactory a thread-safe object?

Yes, SessionFactory is a thread-safe object, many threads cannot access it simultaneously.

What is Session?

It maintains a connection between the hibernate application and database.
It provides methods to store, update, delete or fetch data from the database such as persist(), update(), delete(), load(), get() etc.
It is a factory of Query, Criteria and Transaction i.e. it provides factory methods to return these instances.

Is Session a thread-safe object?

No, Session is not a thread-safe object, many threads can access it simultaneously. In other words, you can share it between threads.

What are the states of the object in hibernate?

There are 3 states of the object (instance) in hibernate.
  1. Transient: The object is in a transient state if it is just created but has no primary key (identifier) and not associated with a session.
  2. Persistent: The object is in a persistent state if a session is open, and you just saved the instance in the database or retrieved the instance from the database.
  3. Detached: The object is in a detached state if a session is closed. After detached state, the object comes to persistent state if you call lock() or update() method.

What are the inheritance mapping strategies?

There are 3 ways of inheritance mapping in hibernate.
  1. Table per hierarchy
  2. Table per concrete class
  3. Table per subclass
more details...

How to make an immutable class in hibernate?

If you mark a class as mutable="false", the class will be treated as an immutable class. By default, it is mutable="true".

What is automatic dirty checking in hibernate?

The automatic dirty checking feature of Hibernate, calls update statement automatically on the objects that are modified in a transaction.

What is automatic dirty checking in hibernate?

The automatic dirty checking feature of Hibernate, calls update statement automatically on the objects that are modified in a transaction.
Let's understand it by the example given below:
  1. ...  
  2. SessionFactory factory = cfg.buildSessionFactory();  
  3. Session session1 = factory.openSession();  
  4. Transaction tx=session2.beginTransaction();  
  5.    
  6. Employee e1 = (Employee) session1.get(Employee.class, Integer.valueOf(101));  
  7.    
  8. e1.setSalary(70000);  
  9.    
  10. tx.commit();  
  11. session1.close();  
Here, after getting employee instance e1 and we are changing the state of e1.
After changing the state, we are committing the transaction. In such a case, the state will be updated automatically. This is known as dirty checking in hibernate.

How many types of association mapping are possible in hibernate?

There can be 4 types of association mapping in hibernate.
  1. One to One
  2. One to Many
  3. Many to One
  4. Many to Many

Is it possible to perform collection mapping with One-to-One and Many-to-One?

No, collection mapping can only be performed with One-to-Many and Many-to-Many.

What is lazy loading in hibernate?

Lazy loading in hibernate improves the performance. It loads the child objects on demand.
Since Hibernate 3, lazy loading is enabled by default, and you don't need to do lazy="true". It means not to load the child objects when the parent is loaded.

What is HQL (Hibernate Query Language)?

Hibernate Query Language is known as an object-oriented query language. It is like a structured query language (SQL).
The main advantage of HQL over SQL is:
  1. You don't need to learn SQL
  2. Database independent
  3. Simple to write a query

What is the difference between first level cache and second level cache?
No.
First Level Cache
Second Level Cache
1)
First Level Cache is associated with Session.
Second Level Cache is associated with SessionFactory.
2)
It is enabled by default.
It is not enabled by default.





















No comments:

Post a Comment