Spring Data - basics - Hibernate Flashcards

1
Q

What are these terms [ ORM , JPA , Hibernate ] ?

A

ORM [ Object Relational Mapping ] is the concept of dealing with database via programmable objects .

JPA [ Java Persistence API ] : is an interface that represent the specifications to provide ORM solution .

Hibernate : is one of the implementations that implement JPA specifications .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the relation between JPA and JDBC ?

A

JPA internally is using JDBC , it is an abstraction layer on top of JDBC .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Is spring boot automatically configure the datasource after adding the driver dependency in maven ?

A

Yes , depending on the driver type [ mysql , oracle , etc ] , it define the configurations of datasource .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is CommandLineRunner ?

A

Is an interface that related to spring and is used to execute some code after loading the context of spring .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the main component for creating queries in Hibernate ?

A

EntityManager

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

list to me the 5 steps for being ready for work with Hibernate ?

A
  1. Adding database driver in maven .
  2. Adding JPA starter in maven .
    3,4,5 . specify the url , username and password of the database in application.properties file .
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is [ Entity ] Term in JPA ?

A

Entity is a java class that is mapped to a table inside the database .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

To create an Entity class , then there are 2 main required steps , list them ?

A
  1. it must be annotated with @Entity .
  2. it must contain public or protected constructor .

The idea behind the constructor is to be able to initiate an empty object from it to be able to fill it with the values that are fetched from the database .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the usage of @Table annotation ?

A

is used if i want to map an entity class to table inside the database but with different name [ the name that is provided in @Table annotation will be the name of the table inside the database ].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the usage of @Column annotation ?

A

@Column is used to map a field to a column inside the table but with different name .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the default value for @Table annotation in case i have an entity class but not use @Table annotation ?

A

in case you didn’t specify the @Table annotation , then by default it will take the name of the class .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to define a primary key but in Hibernate ?

A

Using @Id , this annotation is added on a field to mention that field as a primary key .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the characteristics that are defined to the field that is mentioned with @Id ?

A
  1. it’s values can’t be null .
  2. it’s values can’t be duplicated .
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

List the 4 Generation strategies for the Primary key in Hibernate ?

A
  1. Auto : which i tell hibernate that choose the best key generation way depending on the database type [ not recommended ].
  2. Identity : it is something like auto increment in mysql db .
  3. Sequence : using sequence which is a feature that exists in a lot of database types .
  4. Table : by storing the values in other table .
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is datasource object in JPA ?

A

Datasource is an interface that abstract the database connectivity details in JDBC.

In production the database connection may has a connection pool that manage the number of database connection , the object who is responsible for this is [ Datasource ].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Where the datasource object is configured inside your spring project ?

A

in application.yml file

17
Q

Can you till me how JPA is dealing with datasource ?

A

JPA is relying on datasource [ via enitityManagerFactory ] which is used to create entityManagers [ that perfrom the database queries ].

18
Q

List the types of datasource ?

A
  1. Basic Datasource : that create a new conenction every time .
  2. Connection pool datasource : A more advanced implementation that manages a pool of reusable connections (e.g., HikariCP, Apache DBCP).