JAVA 7 vs JAVA 8 vs JAVA 9 vs JAVA 10

Java 7- NEW IMPORTANT FEATURES 1- switch with string-  i.e- String a=”a”; switch(a){ case “a”:sop(“f”); break; case “b”:sop(“s”); break; case default: } 2-multi catch Before java 7- try{ }catch(ArithmeticException e){ }catch(Exception e){ } After java 7- try{ }catch(ArithmeticException | Exception e){ } 3- try with resources- Before java 7- try{ FileOutputStream fo=new FileOutputStream (); }catch(IOException […]

Step 5- AOP- Aspect Oriented Programming

AOP- Aspect Oriented Programming-  it provides extra functionality over method execution.
Aspect- It’s a class, which contains advices, join points, it can be configured using configuration file,  or spring AspectJ integratio.
Join point- It’s a point, where we can plugin aop aspect, spring only supports execution joint type.
Advice – It represents the methods to be executed at a particular join point.
Pointcut- It represents the expression which determines execution of advice.
Introduction – It represents adding new methods.
Target Object- It’s an object on which advices are applied, spring uses proxied object.
Weaving – It’s a process of linking aspects with other application type/object to create the advised proxy objects.
AOP advice types-
1- Before- 
2- After 
3- After-returning 
4- After-returning
5- Around

Step 4-  IOC(Inversion of control) and Dependency Injection(DI)

IOC(Inversion of control)- It’s a concept where we invert the control of creating object from new keyword to configuration files, In spring this concept is achieved by DI(Dependency injection).

Dependency Injection(DI)- In dependency injection, rather instantiating and initialing objects, they are described in configuration files that how they should be configured and assembled.
IOC containers- They are used to configure and instantiate objects.
Types of IOC containers- there are 2 types.
Type 1- BeanFactory
-Implementation of factory pattern that applies inversion of control to separate the configuration and dependencies from application.
A-XmlBeanFactory-
-Implementation of BeanFactory,
-it is used to provide basic configuration to application by loading required beans from metadata.
-uses lazy instantiation of beans, means bean is instatiated when getBean() is called.
-no support for annotation based DI.
Syntax – 
Resource resource = new ClassPathResource(“springConfigFile”);
BeanFactory beanFactory= new XmlBeanFactory(resource);
Type 2-ApplicationContext – 
-Extends the Beanfactory
-provides more specific functionalities to apllication.
-uses eager instantion, instantiate all beans at startup.
-supports annatation based DI(@Autowired, @PreDestroy)
A-ClassPathXmlApplicationContext- 
implements ApplicationContext interface, Beans loaded from classpth
B-FileSystemXmlApplicationContext- 
implements ApplicationContext interface, Beans loaded using full path of file.
C-WebXmlApplicationContext- 
implements ApplicationContext interface, Beans loaded using web application context.
Types of Dependency Injection-
1-Constructor based DI- Data members and objects are configured using constructor.
2-Setter method based DI- Data members and objects are configured using setter method of pojo files.
3-Interface based DI- Not supported in spring.
Autowiring – Wiring is spring shows relationship bw beans, and we have constructor and setter method based DI to establish relationships bw beans.
But in spring we also have autowire attribute to provide automation to wiring.

Types of autowiring- 
1- no- by default, no autowiring

2-
byName – wiring based on instance name

3
-byType – wiring based on datatype, there should be only one unique datatype of bean is declared.

4-
constructor – It’s similar to byType in constructor argument

5-
autoDetect- chooses between autowiring by constructor or bytype.

Step 3- Spring Bean

All the objects of application, which are managed by IOC containers are called beans.
Spring bean contains 3 things- 
– instantiation 
-life cycle details 
-dependency management

 

Spring bean scopes – there are 5 scopes

a- singleton – one instance per IOC container

b- prototype – many instance per IOC container

c- request – only for web application, instance per http request

d- session-only for web application, instance per http session

e- global session only for web application, instance per global http session

Spring bean life cycle –
there are 2 things,
A- Initialisation –
B- Destruction-
Detailed step by step bean lifecycle in spring framework-

Step1- spring container finds the bean definition from configuration file.
Step2- Bean is instantiated.
Step3- DI is applied from specified properties.
Step4- If Bean class implements BeanNameAware interface, setBeanName() is called by passing bean’s I’d. (optional)
Step5- If BeanClassLoaderAware is implemented, setBeanClassLoader() is called by passing ClassLoader object. (optional)
Step6- If BeanFactoryAware is implemented, setBeanFactory() is called by passing BeanFactory object. (optional)
Step7- If BeanPostProcessors object is in BeanFactory, postProcessBeforeInitialization() is called even before setting property for bean.(optional)
Step 8- If InitialisingBean is implemented, afterPropertiesSet () is called after setting bean properties.(optional)
Step9- If init-method is specified in configuration file, method is called for bean.(optional)
Step10- If BeanPostProcessors are present, postProcessAfterInitialization() is called. (optional)
Step11- If DisposableBean is implemented, destroy () is called.(optional)
Step12- If destroy-method is specified in configuration file, method is called for bean.(optional)
Spring callBack methods-

A callback method is called when an event occurs.

Spring uses 2 callback methods-
A- post-initialization callback methods
B- pre-destroy callback methods
1- Step 8 & 9 are post-initialization callback methods.
Step8 syntax-
Class Test implements InitialisingBean {
public void afterPropertiesSet(){
}}
Step 9 syntax-
Class Test{
public void init(){
}}
2- Step 11 & 12 are pre-destroy callback methods
Step11- Class Test implements DisposableBean {
public void destroy(){
}}
Step 12 syntax-
Class Test{
public void destroy(){
}}
Spring Bean Inheritance – Spring provides facility that child bean definition can inherits data from parent bean definition.
Syntax –
<bean id=”Pbean” class=”ParentBean”/>
<bean id=”Cbean” class=”ChildBean” parent=”Pbean”/>

Step 2- Spring Modules

Spring have 7 modules
1-Core container module- it includes fundamental features of spring framework, such as IOC and Di,

and BeanFactory, entire modules is built on this module.

2- Application context module – it extends the concept of BeanFactory, it provides a way to access any object, other than this it also provide support for other framework.

3- AOP module (Aspect Oriented Programming)- It provides facility to define method interceptors.

 It also includes AOP allience means we can plug other aop framework with spring.

4- JDBC abstraction and DAO modules- It includes clean styled code to perform jdbc operation, other than this it also handles jdbc and sql exceptions as unchecked exceptions.

5- O/R mapping integration modules – It provides integration of orm tools such as hibernate/ibatis into spring modules.
6- Web modules – It is built on application context module, it provides context specifically for web-based application. It also support file handling.
7- MVC framework modules- It is used to provide MVC framework integration into spring. But spring mvc is better option because it uses IOC to sepearate controller logic from business objects.

Generator class-

​Generator class is a sub element of id column.

Generator class is used to generate unique identifier

Hibernate supports 12 generators class currently

But most important are-

 (1)assigned

 (2)increment

6- Mapping in Hibernate

Mapping meaning in hibernate-

Connection between java class to database tables

Types of mapping in hibernate-

1- Inheritance Mapping-

When java classes are having inheritance, their tables can be created using 3 ways-

1- Table per class hierarchy –
-Only one table is created for all the classes.
-One Discriminator field is used.
xml element <subclass> is used.
-Gives maximum performance

2–Table per sub-class hierarchy-
-One table for each class is created.
-Foreign key is maintained between the tables.
-xml element <joined- subclass> is used.

3-Table per concrete class hierarchy-
-One table for each concrete class (subclass) is created but not of super classes.
-Foreign key is not maintained.
-xml element<union-subclass> is used.

 

2- Collection Mapping-

When java class is having collection object, they can be stored according to their type of collection –

1-list mapping
2-set mapping
3-map mapping

3- Association mapping-

When java class is having reference of another java class, their tables can be created using 3 ways-

1-One to one
2-One to many or many to one
3-Many to many

5- Caching in hibernate

Cache definition-

Cache is a temporary data which is used to speed up the performance.

Types of caching in hibernate-

1-Session Cache/First level cache in hibernate-

  • First level cache is enabled by default.
  • First level cache can’t be disabled.
  • Objects are retrieved from current session..
    i.e. for session object session1, Employee persist class.
    Session session1=sessionfactory.openSession();
    Employee emp1=(Employee)session1.load(Employee.class,1); //execute query
    Employee emp2=(Employee)session1.load(Employee.class,1); // will come from first level cache, query will not execute

2- SessionFactory Cache/Second level cache in hibernate-

  • Second level cache is disabled by default.
  • Second level cache is configured to use.
  • Objects can be retrieved from various sessions.
    i.e. 
    Session session1=sessionfactory.openSession();
    Employee emp1=(Employee)session1.load(Employee.class,1); //execute query
    session1.close();
    Session session2=sessionfactory.openSession();
    Employee emp2=(Employee)session2.load(Employee.class,1); //will come from second level cache, if manually enabled only.
    session2.close();
  • Stepes to enable Second level cache-(we are choosing ehCache vendor, because it is widely used)
    step1- Add cache usage in hbm file, for which you want to use second level cache
    i.e.<hibernate-mapping>
    <class name=”ClassName” table=”tableName”>
    <cache usage=”read-only” />
    i.e.- write-only, read-write only, restricted read-write only, read-only is widely used and famous.

    step2- Add these 3 lines in cfg file,
    i.e.
    <property name=”cache.provider_class”>org.hibernate.cache.EhCacheProvider</property>

    <property name=”hibernate.cache.use_second_level_cache”>true</property>

    <property name=”hibernate.cache.region.factory_class”>org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

    step3- Add 2 jars for hibernate second level cache. 
    hibernate-ehCache-x.x.x.jar,
    similar to hibernate-core version.
    ehCache-x.x.jar,
    latest version.

Extra-Query Level cache in hibernate-

  • Query Cache is used to cache the results of a query.
    i.e.
    Session session3=factory.openSession();
    Query query=session3.createQuery(“from Employee e where e.id=1”); //query will execute
    query.setCacheable(true);
    Employee em=(Employee)query.getSingleResult();

    Query query1=session3.createQuery(“from Employee e where e.id=1”); //will come from query cache, query will not run
    query1.setCacheable(true);
    Employee em1=(Employee)query1.getSingleResult();
  • Stepes to enable query level cache-
    step 1- Only add this line to cfg file.
    i.e.  <property name=”hibernate.cache.use_query_cache”>true</property>
    Done!!

4- States of objects in hibernate

Hibernate object states-

5

1-Transient state-

  • When object is just created and has no primary key, the state is called transient.

2-Persistent state-

  • When sessio is opened and object is just saved in database [using save, saveOrUpdate, persist, merge etc method]
  • or retrieved from the database [using get, load etc method]. This state is called persistent.

3-Detached state-

  • When the session was closed, the state changes to detached.
  • or evict() , clear() etc methods are applied, The object is detached from its session.

3-Hibernate Classes and Interfaces

1- Configuration class – includes 2 things

(i)Database connection- Using either .cfg or .properties file
(ii)mapping – connection between java classes to database tables using either .hbm files or annotations

2-Session Interface-

  • Object of Session is created each time when any operation is required.
  •  Session objects are used to create physical connection with database.
  • Session is thread-safe.
  • Session holds a mandatory (first-level) cache of saved java objects in database.
  • Session wraps JDBC connection for Transaction.

3-SessionFactory Interface-

  • Object of SessionFactory should be created only once per database.
  • SessionFactory is used to create session objects.

4- Transaction Interface- 

  • Transaction represents unit of work with database.
  • Transaction is associated with session.

5-Query-

  • Query is used to create manual queries.

6-Criteria.

  • Criteria is used to apply conditions on the result set.

2-Hibernate definition

Hibernate Definition

  • Hibernate is pure ORM tool.
  • ORM stands for Object Relational mapping.
  • ORM is a concept to save/manipulate java objects to database tables.
  • The main goal of hibernate to avoid old JDBC code and focus more on business logic.

Advantages of Hibernate

  • No need to maintain lengthy JDBC code
  • write less code
  • Lazy Loading
  • Caching

Hibernate Architecture

5