Interview Questions on Servlet, JSP, Hibernate For Fresher’s

  • By
  • October 29, 2021
  • JavaScript

1)What  is JSP?

JSP  is  java server Pages  used  for  creating   dynamic  web Application. JSP is a platform independent  way  of creating dynamic web Application. Using JSP you can create Simple to Complex dynamic webapplications. JSP  is a integral part of J2EE.

2)What is Hibernate?

Hibernate  is  a  framework  which  simplifies  the  database applications. It  is  a Open source , Light weight framework, ORM (Object Relational Mapping)tool, Hibernate   is  mainly  used  for  database applications. You  can use  Hibernate  instead of JDBC(java Database Connectivity).

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

3)What  is JPA?

JPA  is Java Persistence API (application programming Interface)  for data  persistence.JPA provides  some features  and  ORM(Object Relational Mapping) Tools. To  Map  a  Plain old  Java Objects into a table.

4)What is ORM Tool?

ORM(Object  Relational  Mapping)   this is  a tool that  makes  develop  job  much  simpler by creating automatic table, As a name suggest it  Maps  Object  to  storage.

5) what  is Package  for JPA?

javax.persistence  package that  contains JPA Classes  and Interfaces  for creating  applications using hibernate.

6) what    are  the  advantages  of  Hibernate?

Here  are  Some  of  advantages  of Hibernate.

1)Hibernate  is Open Source  and Lightweight

2)Fast Performance

3)Data Base Independent Query

4) Self Table creation.

7) What  are  the Layers  of Hibernate  Architecture?

  • Database Layer
  • Java Program Layer
  • Hibernate framework Layer
  • Back end Layer
  • Here  is a image Address.

8)What   are  the Objects  Used  By Hibernate?

Hibernate  framework  uses  many objects Like

Java API, JDBC,Java Transaction API,JNDI java Naming Directory API.

9) what  is  session Factory?

The Package or  API org.hibernate.SessionFactory   interface provides sessionFactory to   get  the  session object.It  is  a  an interface.

10)what  is  transaction?

Transaction   is  an Object  it  specifies  automatic  unit  of  work. IT provodes  the  method  for transaction management.

IT  is  available  in org.hibernate.Transaction

11)  Explain  the  Session  Object?  

Session object  is a   interface between   java application  and  database. IT  is  a wrapper   around  java data base connectivity (JDBC). org.hibernate.Session interface  provides  the  methods  for  save, update, delete, operation , For crud operation. It  also  includes  the  method  for Transaction, query, criteria.

12) What is  a  connection Provider?

Connection Provider   is  a  factory  of    Java Database  Connectivity(JDBC).It  is  a abstraction  Layer  for  Driver Manager  or  Data Source.

13) What   are   the  steps    for  creating  Hibernate  Application?

For  creating  the  hibernate  Application  we need  to  have  a  fallowing steps

  1. Create  a Plain Old  Java Objects (POJO).
  2. Create  a  Configuration file.
  3. Create a Persists class. OR Mapping Class.
  4. Load Jar files  of  hibernate  OR  Use Maven.
  5. Create a Class  to store  data,
  6. Last  You  run application .

Hibernate  will automatically  creates  a  table   from POJO class and Map  it  to  table.

14)  what   is  a  Mapping  File  name?What   are   the   elements  of  mapping file?

Hibernate.cfg.xml    is  a  Mapping  File  name.  IT  is  a  XML  file  it  will  map  to  a  table.

It Contains Many  elements  Like.

Hibernate-mapping  :  IT  is  a  root element. It contains  all  mapping elements.

Class : Here class  a  persists  class. It  is  a POJO (Plain Old Java Objects)class.

Id: IT is  a primary  key in  a  class. It  is  Compulsory   for Mapping.

Property : IT  specifies  property  for  a  class.

Hibernate  File Looks  Like  Here

<?xml version=“1.0” encoding=“utf-8”?>

<!DOCTYPE hibernate-configuration PUBLIC 

“-//Hibernate/Hibernate Configuration DTD 3.0//EN” 

“http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>

<hibernate-configuration>

    <session-factory>

        <property name=“hibernate.connection.driver_class”>com.mysql.jdbc.Driver</property>

        <property name=“hibernate.connection.url”>jdbc:mysql://localhost:3306/hibernatedemo</property>

        <property name=“hibernate.connection.password”>root</property>

        <property name=“hibernate.connection.username”>root</property>

       

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

        <property name=“hbm2ddl.auto”>create</property>

        <property name=“hibernate.dialect”>org.hibernate.dialect.MySQLInnoDBDialect</property>

        <mapping class=“Employee”></mapping>

    </session-factory>

</hibernate-configuration>

15) How  to  Store   data  in  a table Using Hibernate . Write Down the  Steps ?

Here  is  a  Code  for   Storing  the  data.

import javax.transaction.HeuristicMixedException;

import javax.transaction.HeuristicRollbackException;

import javax.transaction.RollbackException;

import javax.transaction.SystemException;

import javax.transaction.Transaction;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.boot.MetadataSources;

import org.hibernate.boot.registry.StandardServiceRegistry;

import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import org.osgi.service.blueprint.reflect.Metadata;

public class MainEntry

{

public static void main(String[] args)

{

// TODO Auto-generated method stub

StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure(“hibernate.cfg.xml”).build();

        Metadata meta = (Metadata) new MetadataSources(ssr).getMetadataBuilder().build();

        SessionFactory factory = ((org.hibernate.boot.Metadata) meta).getSessionFactoryBuilder().build();

        Session session = factory.openSession();

        Transaction t = (Transaction) session.beginTransaction();

        Student e1=new Student();

        e1.setId(101);

        e1.setName(“student”);

        e1.setCourse(“java”);

   session.save(e1);

   try {

t.commit();

} catch (SecurityException | RollbackException | HeuristicMixedException | HeuristicRollbackException

| SystemException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

   System.out.println(“successfully saved”);

    factory.close();

    session.close();

}

}

16) What is a persistence class?

Persistence class a   plain old java object. It contains getters and setters.

Code for Persistence class.

import javax.persistence.Entity;

import javax.persistence.Id;

@Entity

public class Employee

{

@Id

  private int eid;

  private String ename, dept, skill;

public int getEid() {

return eid;

}

public void setEid(int eid) {

this.eid = eid;

}

public String getEname() {

return ename;

}

public void setEname(String ename) {

this.ename = ename;

}

public String getDept() {

return dept;

}

public void setDept(String dept) {

this.dept = dept;

}

public String getSkill() {

return skill;

}

public void setSkill(String skill) {

this.skill = skill;

}

}

17) How to add mapping file in   a configuration file.

<mapping class=“Employee”></mapping>

We  add  above  code  in hibernate.cfg.xml  file.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

18) What    are  the  annotation  used  in hibernate application.

Annotation  are  meta keywords   and  only  compilers   will  understands with the help of Java Training in Pune. Hibernate  application can be created  using Annotations. Like

@Id => it  is  Used  for  creating  primary key.

@Entity => It  is  used   for  mapping  class  file  to  entity

@table => it  is  used  to  naming a  table .

@column => It is  used  for  making property  as a  column.

All hibernate Annotations are available in javax.persistence. Hibernate  Entity Manager implements  all the  interfaces and Life cycle  defined in JPA  specification.

19) Explain  SQL  Dialects  In Hibernate?

The  dialects  specifies the  databases like. Which  database  it  will  work  like   mysql, oracle, etc.

Based  on  Dialect  Hibernate    generate    the  SQL.   So  it  defined  in hibernate.cfg.xml file.

  <property name=“hibernate.dialect”>org.hibernate.dialect.MySQLInnoDBDialect</property>

All Dialects   are avaible in org.hibernate.dialect. Package.

20) What are Generators classes in hibernate?

Generator  class  is  used  for unique  id  for  objects. Generator  classes   are  defined  in hibernate  framework. All  generator   classes   are  exists  in org.hibernate.id.IdentifierGenerator Package.

Hibernate Framework  provides  built in classes. These   are  mentioned here

Increment,assigned,sequence,identity,uuid, guid,foreign.sequence-identity.

21) How To map  inheritance Mapping  in  hibernate?

There   are  three  inheritance mapping  in   hibernate like

Table Per Hiearchy,Table Per concrete Class,Table Per sub class.

22) Which annotation is used  for One-to-one  Mapping?

@OneToOne(targetEntity=Address.class,cascade=CascadeType.ALL)

@OneToOne(targetEntity=Employee.class)

23) what  is pom.xml  file in maven?

In Maven   pom.xml   file  is  project  object  Model  that   is  used   for   adding  dependency  files that require  for   creating the  application .

Here  is  a  dependency  for  mysql, and dependency  for  hibernate

<dependency>

    <groupId>org.hibernate</groupId>

    <artifactId>hibernate-core</artifactId>

    <version>5.4.16.Final</version>

</dependency>

    <!– https://mvnrepository.com/artifact/mysql/mysql-connector-java –>

<dependency>

    <groupId>mysql</groupId>

    <artifactId>mysql-connector-java</artifactId>

    <version>5.1.26</version>

</dependency>

24)Explain  Bidirectional  Association  in hibernate.

It  helps   to  fetch   data   from  dependent  object.if  Java Course in Pune apply  one-to-one, one to  many  mapping relationship for  mapping  classes   and  files  is  called  Bidirectional  Association.

For  example we can  fetch  data  from  both  tables  like dependent  and independent tables.

25)Explain  Lazy  Collection  in  hibernate.

In Lazy collections  hibernate  loads the  child  objects  on  demand. So Application  performance  gets  improved. In hibernate  3.0  by  default  lazy collections  is  enabled.

Lazy  Collection is  by  default  True.

26) Explain caching  in hibernate?

Caching  improves  the  performances  of  a  application   by pooling  object  into  cache. Cache  is useful  when we need  to  fetch  the  same data  repeatedly.

In hibernate  there   are  two  Types  of  caching

First level cache

Second level cache.

In first level cache    session  object  stores   first level  cache.  Session object  holds first level cache. By default  it is  enabled.

Second level  cache Java Classes in Pune need to enable  it  explicitly. So  cache  is  available  for entire application.

Session Factory object holds  second level cache.

27) Explain Core Interfaces  of  Hibernate.

The  core  interfaces  in hibernate   are-

  • Configuration
  • Session Factory
  • Session
  • Query
  • Criteria
  • Transaction

The  core interfaces  are  also   called  key components  in hibernate.

28)  How   to  create  SQL  in hibernate?

Session.CreateSQLQuery()  method  is  used  for  SQL  in  hibernate.

29) What  is  HQL?

Hibernate  Query  Language   is  called  HQL . That  is  used   like  a  sql. If  anybody from  development  background  which  have  very less  knowledge  about  sql can  use  HQL. HQL  interacts   with a class .

Persists  class it  will  map.

Here    are  few   advantages   of HQL

  1. if  you  have  very less  knowledge  about  sql . you can go for HQL. No need to  learn SQL to work  with a  database.
  2. HQL  is  database  independent.
  3. HQL  is  very  simple  way  to  write.

29) What  are  different   kinds  of mapping  in hibernate?

One  to  one mapping, one  to many mapping, many to  one mapping, many to  many mapping.

30)  What  are  the  states  of  objects  in hibernate?

There  are  3  states  of  object  in hibernate . Transient, Persistent, Detached.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

31) what  is session.persist()?.

Syntax  of session. persist()  method.

public void persist(Object o)

it  returns  the  void  it returns  nothing.

32)  What   are  the advantages  of  ORM?

  • Application development is fast.
  • Generates  key  automatically.
  • Automatic  mapping to a class.
  • No need  to  have a knowledge  of  SQL.

33)  What  is   difference between  update()  and  merge()  method ?

Update   will  update the table . it  will  edit  the table.

Merge  will  combine the tables.

Update  is   used   inside a session   only. Once  session  is  closed   it  will  throw  exception.

For  merge  no  need to   of  session object.

Bhagyashri Sangam

Call the Trainer and Book your free demo Class  Call now!!!
| SevenMentor Pvt Ltd.

© Copyright 2021 | Sevenmentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*