Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

SPRING:-

Through Java UI cannot be created.


Transactions are not present in Java.
Security of application not present through Java;
Bulk processing not present in Java.
Batch processing not ready made in Java.
Nothing like JSON data in Java.(only java objects present)

Spring has all the things that an application needs.


VMWare manages the Spring.

Maven tool is needed.


dependencies added in pom.xml file.

Java objects are not created here, ApplicationContext is used.(Java objects


here are called Beans)

3 types of Configuration:-
1)xml
2)Annotations
3)Java Configuration.

Maven project:-
1) Right click in the project explorer tab and new->maven->Maven-project
2) Select the default workspace and continue.
3) Enter the
Group id
Artifact id
Name (same as artifact id)
Continue.
4) in pom.xml file add dependencies ie spring-core and spring-context (take content
from net search mvnrepository->1st link->search Spring->select version->copy
content from net and paste under dependencies in pom.xml file.

Along with dependency, indirect dependency also created by maven automatically.


It downloads dependency on the local repository.
From net it will download and save in local repository.
In users,M2 folder local repository present
Dependency managemnet automated by Maven.

pulgin are additionals documents needed for Configuration.

In spring project first, application Context is needed for creation of objects.

//this will read the specified file,no errors should be present in beans.xml
otherwise ApplicationContext will be created successfully.
ApplicationContext context=new
ClassPathXmlApplicationContext("classpath:beans.xml");
A a = context.getBean(A.class);

Dependency injection is done through xml and annotations.


create beans.xml file in src/main/resources.
in src/main/resources files are present in classpath.

Fetching bean by class


A a = context.getBean(A.class);
a.execute();
Fetching bean by id
A app=(A)context.getBean("a");
app.execute();

If parameters are optional then use settors,if mandatory then use constructor.

Annotations:-
Annotations used mostly here as compared to xml.
@Component or @Service
@Autowired (does wiring by type)

(in beans.xml)
<context:component-scan base-package="com.ibm"></context:component-scan>

Spring-test dependency needed for test case of JUnit.


JUint and spring-test automatically creates the ApplicationContext.
NO need to main method if test cases are created .

In one test case file, various test cases can be made and their order is not fixed.

Maven install will build and place it into the local repository.
Maven deploy will build and place it into the server where it woould be deployed.

***********************************************************************************
******************
JDBC used for database connectivity.
it does not provide full database independency.As all DB has different syntax.

ORM (object to relation mapping) ie hybernate is layer before JDBC that provides
full data independence.

hybernate is build on JDBC.

5 steps for JDBC:-


1) Load relevant driver implementation class
2)User drivermanager to get connection.
3)Create and fire sql statement
4)Fetch result using result set
5)Close connection

JDBC throws different types of EXceptions thus hybernate is used.

HYBERNATE:-
Everything related to connections should always be present in the xml file
(hybernate.config.xml)

In xml file, 1 session factory for each database required.

Session is made to create physical connection with the DB.

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();


Session session = sessionFactory.openSession();

SessionFactory represets an instance of one database.

Transaction is made only with updates or inserts are needed.i.e when DB is


changing.
When data is fetched we do not need Transaction to be created.
update() function of session is not used to update the Db but it is used to move
from detached to the persistent state.
Query always follows the Class name not the table name.(if they are different).

DDL auto genearted by HYBERNATE.


Also object-relation mapping

Coloumn name can be different from the field in class using the @Colounm
annotations

create,update,insert and delete operations all are run through functions of session
object.
If not function then createQuery(), this type is called named query.

Hybernate
1)update
2)fetch all employees
3)Max salary
4)acc to name fetch details
5)count total no of emp

EXAM (MCQ)

1) return type diff then is overloadng?


2) JVM JDK and JRE (compilation is where byte code is generated..so who does the
compilation?)
3) byte and short

***********************************************************************************
********************************
Integrating SPRING and HYBERNATE together

Initially do all the steps required for the Spring. i.e Employee class, beans.xml
etc.
JPA is the set of interfaces.
real classes are present in Hibernate.

JPA can use different types of ORM, here we are using Hibernate.
SessionFactory is called EntitYManagerFactory
Session is called EntitYManager
Hibernate-config.xml is called here as persistence.xml
Right click on project Configure->Convert to JPA project
or
Create a folder in resources name META-INF, a file named persistence.xml is
created.

in beans.xml, add other tags required.

1) pom.xml has all dependencies of spring as well as Hibernate additional,spring-


orm dependency.
2) persistence.xml
2)bean.xml data source,EntityManagerFactory (1 DB 1 EntitYManagerFactory),eetc
3) special annotation of @persistenceContext, in this already session available
4)@Transaction annotation at the top of method.Before start of session it
automatically
does start session and at the end closes the sessionAlso add tag<> to the bean.xml
fro the tx.
whenever an exception is thrown by this method the Transaction is automatically
rolled back.
Only Rumtime exception will give Transaction rollback, checked exception does not
do the Transaction rollback.

***********************************************************************************
**************************

dell->eclipse-workspace1->SpringHiberanteDemo2
(User is associated with course. One to one and one to many relationship present.)

Mapping from Employee to Address. Annotation of @OnetoOne is used.


@OnetoOne(cascade=CascadeType.ALL) if any operations on employee then address is as
a shadow of it.
@OneToMany(cascade = CascadeType.ALL,fetch=FetchType.EAGER)

When we fetch an employee, the address is lazyly fetched, we need to change to


eager.(one to many, if employee is fetched then all its address we want then eager
fetch)
be default it is lazy.

Cascade used to create the address first and then mapped to employee.

NamedQuery:- query has a name and can be used anywhere using this logical name. It
is used for code Reusability.
HQL queries in Hibernate.
JPQL(java persistence query language)

Dynamic query is not possible through NamedQuery.


Criteria queries is an API used for Dynamic queries.
Dynamic query is slower than HQL.

Inheritance in JPA:-
1)Single Table strategy.But normalization notb present.
2)Table per class.Redundancy present.
3)Joined. Whatever type of emp, for id and name it will go in the emp
table,Foreign key needed.

Single table strategy:-Discriminator values is present.

You might also like