-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHibernate
More file actions
196 lines (167 loc) · 10.1 KB
/
Hibernate
File metadata and controls
196 lines (167 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
criteria in hibernate ?
Difference between Hibernate and Jdbc (hibernate vs JDBC)
Q2. Session VS Session factory (Differences)
Q3. Can you create multiple session factory objects? Justify your answer!
What is the equivalent object of the session in JPA?
What is persistence context in JPA / Hibernate?
Explain the first level of caching in Hibernate.
Justify the difference between get() and load() in Hibernate.
Explain the transient state in JPA/ Hibernate.
What is a detached state in hibernate?
How to detach an entity object from the persistence state without closing the session?
Coding challenge on Hibernate states.
Why do we need a connection pool?
How will you implement connection pooling in your project?
What are the fetch types available in hibernate? Explain how will you use it in real-time.
Eagar Vs Lazy code practice
What is a @Generated value annotation? Explain the different generation types available in JPA / hibernate.
Code Practice [Auto Vs Identity Vs Table generation type]
Explain the advantages of Named queries in hibernate/JPA?
What is Criteria API in Hiberante? Explain the advantages.
@NamedQuery/@NamedQueries quick KT
Criteria API In Hibernate quick KT[Query/Restrictions /Pagination/Filter demo]
=============================================================================================================================
connection pooling ?
-- no need of creating a new connection every time you fire a query
-- we maintain the session at server level not at data base level.
genric case if you need to swim you will use the existing pool .. you will not construct a new pool for your self.
-- vendors or service providers for connection pooling in hibernate.
-- Hikari connection pooling -- vendor.
hibernate -- Apache DBCP// D3PO
=============================================================================================================================
-- Data Source is the interface which maintains the connection pooling in java.
-- spring driver manager doesnot provide any connection polling facility
-- were it comes to hikariDataSourece
=============================================================================================================================
what is lazy initilaization exception in hibernate
-- when ever an object is present in detached state?
-- when ever the objects are out of @Transactional boundery?
=============================================================================================================================
fetch type in hibernate?
-- for one to many and many to many the default fetch type is lazy by default.
@ManyToone and @OneToOne eager in nature
@OneToMany, @ManyTOMany -- LAZY
=============================================================================================================================
@GenerateValue
hibernate-- GeneratedValue.AUTO
-- GenerateValue.IDENTITY -- it will verfiy weather data base it is and then generate the sql query for it.
=============================================================================================================================
why use hibernate/JPA ?
-- to be data base egnostic
-- dilect is responsble for changing the hql query to the SQL native query
-- the wraps up the exceptions and make the checked to un checked exception.
-- handles caching
-- transactions are handled by anootations provided.
=======================================================================================================================================================================
what is the difference between session and session factory?
-- session factory is heavy weight object and sessoin is light weight object.
-- session is independent to a single thread and session factory provider to it.
=======================================================================================================================================================================
can we create mutiple session factory in hibernate ?
-- yes we can create multiple session factory
-- when we are having multiple data bases then we have to create each session factory independently for each data bases.
=======================================================================================================================================================================
coming to JPA similar methods?
EnitiyManager-- session
-- persist() <<>> save()
-- merge() <<>> merge()
-- delete() <<>> remove()
-- detach() <<>> evict()
-- close() <<>> close()
-- clear() <<>> clear()
==============================================================================================================================
hibernate states ?
state of objects in hibernate--
-- transient
-- detached state.
-- persistent.
-- detached.
-- removed.
==============================================================================================================================
what is first level cache in hibernate?
-- so you have student object
Student student=new Student("Yash");
sessoin.save(student);
lets say we are changing the name multiple times
-- student.setName("ajay"); -- this will be working on copy of the object.
-- student.setName("polaki");-- this will be working on copy of the object.
-- student.setName("kumar"); -- at last it compare with the first saved object and if they are not same then it changes it, with the last object and move further.
persistance context is a mechanisum were it acts as first level cache -- so it hold the state of the object when the final commit is done.
session is the persistance context.
======================================================================================================================================================================
what is the difference between get and load methods in hibernate?
get() -- it is said to be eager loaded object when ever called and not even used.
Song song = session.get(Song.class,1);-- song is equiped with the object.
load() -- it is said to be lazy loaded we can also say that is a proxy of the object created for performance.
Song song = session.load(Song.class,1);-- song is equiped with proxy.
if the objects are not present then you might get null pointer for get call.
you might experiance object no found for load-- method call.
System.out.println(song.getName());-- under this line we have the object loaded
which internally follows proxy design pattern.
======================================================================================================================================================================
expalin N+1 problem in hibernate?
: excution of N+1 number of quries while using hibernate is defined as N+1 problem in hibernate.
this is arised : two entities ie.. Employee and department related to each other having @OnetoMany relationship between them and you trying to fetch the department details then
we get this isssue.
eg : Department -> id , name, ListOfEmployee.
first query : select id, name from department;
according to the result have n number of rows then, n number of quries are excuted here.
addition of this N+1(first query) is the problem .
Solution 1:
To slove this issue, we can use : select id,name, .... from department d Join Fetch d.listOfEmployee;
here internally : select dp.id , dp.name, emp.name, emp.id from department left join employee on dl.id=emp.id;
solution 2 :
At reposetory level we can have a annotation as @EnityGraph(attributePaths="addresses").
be paticular with the commands.
=======================================================================================================================================================================
do you know hibernate does not support lazy loading for @Onetoone relationship.?
=======================================================================================================================================================================
why do we use @transient in hibernte?
exception handling in hibernate
-- roll
hibernate propogation ?
Answers :
REQUIRED (Default): Uses existing transaction or creates new
REQUIRES_NEW: Always creates new transaction (suspends current if exists)
SUPPORTS: Runs in transaction if exists, otherwise non-transactional
NOT_SUPPORTED: Executes non-transactionally (suspends current if exists)
MANDATORY: Must run within existing transaction
NEVER: Must not run within transaction
NESTED: Creates nested transaction if exists.
=======================================================================================================================================================================
hibernate cascading ?
Answers:
PERSIST: Saves child when parent is saved.
MERGE: Updates child when parent is updated.
REMOVE: Deletes child when parent is deleted.
REFRESH: Refreshes child when parent is refreshed.
DETACH: Detaches child when parent is detached.
ALL: All operations are cascaded.
example and understanding.
=======================================================================================================================================================================
why do we use cascading in hibernate ?
Over a two classes who are related as parent and child ,
if you say @OneToOne(mapping="childClassName", cascade=CascadeType.ALL, )
what are the annotations used in spring boot? -- good.
Answer : application level --@SpringBootAnnotation -- @ComponantScan, @Configuration, @AutoCongiguratoin.
configuration level --- @Configuration
@Bean
Controller --
@RestController
@RequestBody
@PostMapping
@GetMapping
@PutMapping
@RequestParam
@Pathvariable
real life case of @singleton ? -- good.
-- data base connection,
-- config mannager
-- logger
can we work without using @Transnational annotation ? -- yes we can maintain the session and do the job mannually.
Hibernate level validations ?
@NotNull,
@Size, ...(min= ? ,max=?)
@Email
@transactional level understanding in hibernate?
it is all about the either commit or roll back mechanisum of your method or a class