Spring B JPA

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

Mr.

Raghu sir ( Spring JPA )

Q.51 what is ORM?


 ORM stands for Object Relational mapping. We have to perform every operation
in object oriented manner.
 This ORM is also called a technique connects our application, that is object-
oriented program will be connected to relational data base.
 [ It is technique used in creating bridge between object-oriented program and
relational database]
 [ Normal java object convert in relational database ]
 ORM is mainly used for database Operation Every Operation you perform save,
insert.delete, select that should be in object

Q.52 what is JPA?


 Stands for Java Persistance API, It is one of ORM specification. ( orm is theoretical
concept, where JPA is programmatically concept but it is Not complete one.)
 Sun micro system is given some set of interfaces and classes. Yes it is following
ORM.
 It is a Java specification used to do Persist data (persistency operation) between
data base connectivity Operations.
 mainly for relational database between Java objects & Java application.
 actually it connects Java class to database tables.

Q.53 what is Mapping rule in JPA?


 We have to follow a concept of mapping a class with a table.
 one Java class will be connected to one table, such class called as Entity.
 A class mapped with database table.
 (we define Entity (pojo) class for one Table ]
 here inside the one to be doing variable, one variable connected to one column
example: Variable means 4 columns
 [ we define one variable for one column ]
 Then one object is converted into one Row (even Reverse)

Q.54 JPA vs Hibernate?


 ORM is theory, JPA i Coding Specification given by Sun Microsystems, now part of
Oracle.
 Jpa is a specification that defines a set of interface and annotation for orm in java
applicaton.
Mr. Raghu sir ( Spring JPA )

 They said, follow some interfaces & classes that you will achieve the JPA That
follows ORM. but wing JPA we can't write Complete application. we have to use
one vendar, Actually implementation of JPA is Hibernate.
 [Hibernate is a JPA implementation ]
 other implementation are available, but mostly large application even spring boot
internally same Hibernate
 example. Eclipselink, Apache Open JPA, iBatis, TopLink.. etc

Q.55 Advantages of using JPA?


 If you use JPA compare to JDBC mainly we can say database independent
(Dialect) If you are working with ORACLE, & you are moving to MYSQL database,
JPA codes works as it.
 Here the major concept called dialect that we need to change,
 easy to develop database Operations if your performing operation ( insert, delete
u need not write any query.
 even we can define our own query Generally we write SQL query but here we use
JPQL specification & HQL implementation.
 Java persistency query language (JPQL) Hibernate Query. Query language (HQL)
 We can independent database data types Generally databases follows different
data types and parameter mechanism handling).
 For example using MYSQL int, double data type, varchar datatype. u go for
oracle, number, varchar2
 but here automapping, or implict mapping

Q.56 what is spring data (or) spring boot JPA Repository?


 Generally we use JPA with hibernate,we write code manually (for save, Update)
but springBoot Data JPA says, we are giving some set of interfaces those
 CRUD Repository
 PagingAndSortingRepository
 JPARepository
 This are all predefined interfaces Actually given by springboot. You dont create
the class extend the interface with your created interface & Generate one class
that will perform the Operation at runtime.
 [Here all this three interface, it's implementation class (proxy class)is generated
by spring boot at run time]

Q.57 PagingAndSortingRepository vs JPA Repository?


 Both are similar.
 JPARepository is defined for only Relational databases (sql)
Mr. Raghu sir ( Spring JPA )

 (If you are using MYSQL DATABASES, Oracle DATABASES, POSTGREY )


 But PagingAndSortingRepository and Crud Repository work for SQL & NOSQL
DATABASES.
 (for mongo DATABASE, specially PagingAndSorting are used)
 also PaginAndSorting used for Pagination and sorting page purpose.
 Also available in JPA Repository.
 JPARepository extends internally PaginAndSortingRepository.

Q.58 what are the pre-defined methods given by repository interfaces?


 save(), delete(), count(), findById(), findByAll(), deleteById(), existById()

Q.59 what is Entity class of explain commonly used annotations for this?
 A class mapped with Database Table is called as Entity class.
 (one class -> one Table) we can also called as PoJo Class (Plain old Java object)
 It must be mapped with Database table using JPA Annotations.
 Like @Entity. @Table, @Id, Columb
 @Unique @ Generated value etc

Q.60 what is the naming convention for finder methods in the Spring data
repository Interface?
 Even we can defined our custom method By wing finder mechanism & findBy
Mechanism followed by variable name in Java class.
 syntax < ReturnType> findBy <Variable name, DataType Parameter);
 exg1: Post pro findByIdAndName( int id, String name)
 exa2: List<User> findByName (string LName );
 ( no need to write any query)

Q.61 How can we create a custom repository in spring data JPA? Or How can
we create specific repository for Entity class?
 Product Repository, studetRepository, EmpRepository
 we use one of the below iuterfaces.
 [we can create custom repository by extending any one of the below interfaces ]
 repository -> create child interface
 crudRepository -> by extending crudRepository
 PagingAndSortingRepository
 JPARepository
 Repository use for custom repository
 Example : public interface stRepository extends Repository<>(){ }
Mr. Raghu sir ( Spring JPA )

Q.62 What is PagingAndSortingRepository?


 This repository given by spring data JPA.
 Two Purpose we use
 Sorting ( either in ascending / descending)
 findAll (it takes sort parameter)
 If you want to fetch the data page by page (pagination)
 ( we can convert the record or print the record in page by page (pagination))
 This Repository internally extends CrudRepository.

Q.63 @Query?
 If u want to define our own query which is actually not present in the Repository.
 I want to fetch 3 columns based on 2 condition then we can define @Query
annotation use. when you are writing this query make sure, you should hot write
SQL query, u should write JPQL/ HQL query.

 How it is different.
 (SQL query constructed using table name and column,
 But JPQL/HQL constructed class name in the place of table name. Variable name
in the place of column name.)
 It is case sensitive.
 Here JPQL is specification and HQL is the implementation. JPAL (child) HOL
(parent) (new implement)

Q.64 what type of Queries can be implemented By wing @Query


annotation?
 By using @Query annotation, we can write ( define) both select & non-
select(upate/delete) operations. but not insert.
 By using @Query we can write both JPQL and Native SQL Queries
 (like pure SQL queries we can write) database dependent queries also.
 but Remember, when we are using @Query with Update or delete, we should
also specify @modifying annotation
 (because @Query default internally select U to want to indicate non-select the
add @Modifying annotation)

Q.65 Can U Give me example of using @Query annotation with JPQL?


Mr. Raghu sir ( Spring JPA )

 Based on the employee name fetch the data from the database.

 must define Inside the Repository interface


 must be abstract method
 must check JPQL/HQL syntax

Q.66 what are collection mapping supported by JPA?


 JPA Supports list, set, map. Of course you can use collection interface also.
 We need to add @ElementCollection over variable.
 Whenever use @ElementCollection that is actually going to create a child table
with 2/3 columns.
Those 3 columns will be key column
Key Column: Foreign Key column
Index Column: Position of value in collection (in case of list, in case of map, in
map key will be position)
Element Column: Actual data of the collection will be store.
 For set collection, table is created with 2 columns (key, Element)
 For Map collection, table is created with 3 columns (key, value, Element)

Q.67 what is PlatformTransactionManager?


 It is an interface. that internally extends transactionManager.
 transactionManager is handle transaction.
 It is the central interface in spring transaction infrastructure.
 Internally use @Transactional, this annotation actually going to do commit,
rollback based on the operation.
 ( It enable @Transactional Annotation which does commit in case of success and
rollback in case of exception/failed)
 If the query is successfully is executed without any issue means commit.
 If query I fail exception is occurred it is going to do rollback.
Mr. Raghu sir ( Spring JPA )

Q.68 How can we enable spring data jpa?


 Actually it is autoconfigured, you need not to do any specific configuration or
enabling. just add a dependency
 (Springboot comes with auto configuration, if add spring data jpa dependency in
pom.xml ===spring-stater-data-jpa)
 (we must write some configuration in application.properties, like db connection
details and jpa details. Like url, username, password, jpa-show, dialect )

Q.69 findById() and GetOne() ?


 Both are used to fetch one row data based on pk
 But findbyid() method present in CRUDRepository
 And GetOne() method present in JPARepository.
 findByid() if value not found it will return optional class, it will return null means
its return type is Optinal<T> class.
 If get() method it will throw the exception if record not found
(EntityNotFoundException)

Q.70 What is use of dialect and give some examples ?


 Dialect is class defined in the JPA
 All the sql query generated by Dialect. (It generate SQL’s at runtime based on our
operation ( method class) like save it generate insert query
 If you move one database to another database dialect you should change in
application. Properties
 Example :Oracle10gDialect,MySQL8Dialect,PostgressDialect.. etc

Q.71 what I fetchType in JPA?


 Generally you performing a select operation used for association mapping or
collection mappijng.
 Do want to fetch parent data along with child data that is decide by fetchtype.
 fetchTypes are : EAGER and LAZY
 Eager means I want to load parent with child(customer and order at a time with
single query)
 Lazy means it loads only parent data from the table later if u call the specific get
method and all then it will load.
 Both used for select operation.
 Fetch internally is an enum.

Q.72 What is default fetchType in JPA?


Mr. Raghu sir ( Spring JPA )

 There is no default fetchtype depends on the operation we do.


 Lazy: For example you are writing one-to-many or many-to-many for
elementCollection(one-to-many ,many is child)
 EAGER: For example you are writing one-to-one or many-to-one for
elementCollection(one is child)

Q.73 How can we see generated SQL’s at cosole/Log files?


 By default jpa will not display, but will generate the query
 Data JPA by default SQL and it will not be shown
 To view them add property in application. Properties
Spring.jpa.Show-sql=true;

Q.74 What are the different type of joins supported by JPA?


 INNER JOIN | JOIN: get commonly connected rows from both tables.
 Fetching data from two table actually common records they are connected to
each other.
 OUTER JOIN
 1.LEFT OUTER JOIN | LEFT JOIN (gets all rows from left side table and connected
rows from the right side table)
 2.RIGHT OUTER JOIN | RIGHT JOIN
 3.FULL OUTER JOIN | FULL JOIN (gets all rows(both connected and non-
connected) from both side table)

Q.75 provide both SQL and JPQL/HQL Joins SYntax?


 SQL:
Select <columns> from <ParentTable> <p> [JOIN TYPE] <childTable> <C>
ON <p>.<pk> =<C>.<FK> where <CONDITION>
 JPQL/HQL:
Select <variable> from <ParentClass> <p>
[JOIN TYPE]
<p>.<HasVariable> As <C>
where <CONDITION>

You might also like