How to Fetch Data with JPA In Spring

Furkan Topal
2 min readSep 15, 2020

I’m starting by adding these artifacts to the pom.xml

Then I’m creating Entity class and assigning Data Source to that
P.S. how to assign a data source is pretty straightforward, a quick check from the web would be alright

creating a BaseRepository interface for inheritance

Now I’m creating my DemoRepository interface, I’m going to leave blank inside of that because I will only use generic findById method for this time. But you can create your own custom methods.

From now on, by Autowiring your DemoRepository interface you can call findById() or your custom methods, as we mentioned above, in your service.

Last but not least, I may recommend to getting values as Optional and only assign into actual attributes after checking with isPresent() while doing that.

--

--