Skip to main content

Spring Data JPA All in One

 















when we call save(entity) behind the seen it call persist method of entity manager 


and when we update the object it calls merge method on the entity manager. 


So same save method is called for persisting and updating the object. 





















Here when we call save method it will call behind the seen entity manager persist method and it issues the insert query (because y persist ke saath saath sql query ko flush bhi kar deta hain so alag se entity manager dot getTransaction dot comit karne ki jarurt nahi hoti hain y automatically ho jaata hain.) in sql at runtime. 




Now suppose we want to make change in returnedStudent object then we will call the save method but entity manager behind the seen will call to merge method. 


You might be asking this point that why we are calling save method for updating the student ?






So let's go to the update process again and see how the merge method of the entity manager is actually doing update when save method is called. so lets say line 20 has been executed and we have persisted student object into student table. so here both the student and returnedStudent variable the both would be refering to same student object in java memory which would be in a detched state.  After that we realised that we have misspelled name alisa we correct that in next time 21 by updating it using the returnedStudent variable but the object being referenced by the returned by the student variable is in the detched state so whatever changes we make to it we will not save to database automatically  and will updated only in java memory its like client or user working with detched object rendered on his screen or a web page an example whatever update he makes to detached object unless he saves the detched object 













When we delete a record then why an extra select sql statement get executed when we call delete method


Because before deleting student hibernate force check weather this student is exist in our database weather even we have that student in our database or not the way it does that under the hood it calls entity manager. find the student whose id is one.

Then it checks weather find method is null or not. Only if we have not null means we do have student with id one then only it deletes. So here due to this find method it fire the select query.

 









Spring Data Commons








If we want to delete in batch then we suffix as deleteAllInBatch (InBatch as suffix)


If our database is not a relational db then still we have Interface like JpaRepository like show in below image.



Question: 



Here you might be thinking why C option is correct because sometimes we don't want to expose all the methods so we can create only the methods that we need it so we can only extend the Repository interface like shown below.

So we can create our own customer repository like this with only the methods we need it. 






Derived Queries, Paging and Sorting






Question :


If all query available for spring data jpa then it will execute as follow 

First Priority for @Query()
If @Query is not available then Second Priority for @NamedQuery() and if NamedQuery also not available then it will move to the drived query by method name.


Note: Never Pass String literal into your JPQL query like in @Query() always used the name parameter in the query


Order By ASC, DESC




StartingWith, EndingWith, Containing 



Ignore Case




IsNull



True


Same for False





Pagination and Sorting

                                  




Sorting 




























Comments

Popular posts from this blog

How do I change the time zone of my Amazon RDS database instance?

As we know bydefault time in the format of UTC in mysql.We can set local time zone to our AWS RDS Instance for our application. or any other time zone prefared Cloud Based Website Hosting Service Provider Steps 1: Go to Services and Select RDS Now to change time zone we have to change "Parameter Group" in left side that is associated with DB instance first we can check default Parameter Group for our instance is Parameter group default.mysql5.7  ( in-sync ) like this. So we have to change the time zone in this Parameter Group.  now open that parameter group (default.mysql5.7)  and click on edit parameter. then search for time_zone (because we want to change it.) then we have to change time_zone only by default it is engine-default (that is utc)  we have to select Asia/Calcutta.  More information we can ref.  https://aws.amazon.com/premiumsupport/knowledge-center/rds-change-time-zone/

Changing the Time Zone on Amazon Linux Ec2 Instance

Amazon Linux instances are set to the UTC (Coordinated Universal Time) time zone by default, but you may wish to change the time on an instance to the local time or to another time zone in your network. Important These procedures are intended for use with Amazon Linux. For more information about other distributions, see their specific documentation. To change the time zone on an instance Identify the time zone to use on the instance. The  /usr/share/zoneinfo  directory contains a hierarchy of time zone data files. Browse the directory structure at that location to find a file for your time zone. [ec2-user ~]$ ls /usr/share/zoneinfo Africa Chile GB Indian Mideast posixrules US America CST6CDT GB-Eire Iran MST PRC UTC Antarctica Cuba GMT iso3166.tab MST7MDT PST8PDT WET Arctic EET GMT0 Israel Navajo right W-SU ... Some of the entries at this location are directo

Digital Marketing

What actually is Digital Marketing? This post will help you understand the insights of Digital Marketing What is Digital Marketing? Digital Marketing is an integral part of the overall marketing strategies of any business. It basically covers the advertisement of products/services/business/brand via digital channels. The digital channels could be of any type like websites, search engines, social media, emails, SMS, and MMS. In case if you're using all these digital channels for the marketing, make sure to have all the statistics & workflow of your campaigns via marketing automation. What are the types of digital marketing? Well, there are 6 core digital marketing types: Search Engine Optimization (SEO) : Search Engine Optimization is nothing but a long-term process of improving your website rankings on search engine results pages (SERPs), which in turn has a wide range of tactics & strategies to implement. Although there is no specific method or a  spec