Java Spring Boot - JPA - Hibernate - H2 - Entities Inheritance Mapping - Joined Table Strategy

  Рет қаралды 14,241

CodeForgeYT

CodeForgeYT

4 жыл бұрын

In the first place we will configure database connection. Later we will learn how to use inheritance together with JPA and Hibernate in Java Spring Boot application. We will create abstract super class (parent class) and then two sub-classes (child classes) which will inherit all of the properties and mapping information from parent class. We will use @Inheritance annotation to specify strategy which we will use. This time it will be JOINED strategy. In our super class we will also use @Enumerated annotation to learn how to persist enums. In the final stage we will learn how to use inheritance in case of repositories which we will use to persist and delete our entities from the database. We will track everything using H2 web console. Keep coding!
Joined table source code: github.com/codeforgeyt/jpa-in...
Joined table video: • Java Spring Boot - JPA...
Single table source code: github.com/codeforgeyt/jpa-in...
Single table video: • Java Spring Boot - JPA...
Superclass source code: github.com/codeforgeyt/jpa-in...
Superclass video: • Java Spring Boot - JPA...
Table per class source code: github.com/codeforgeyt/jpa-in...
Table per class video: • Java Spring Boot - JPA...
Follow //CodefForgeYT on:
FB: / codeforgeyt
Twitter: / codeforgeyt
GitHub: github.com/codeforgeyt
Instagram: / codeforgeyt

Пікірлер: 50
@ranaisrivastav363
@ranaisrivastav363 2 жыл бұрын
This video saved me SOOOO much trouble. This video was a godsend. Thank you so much!
@CodeForgeYT
@CodeForgeYT 2 жыл бұрын
Awesome! Glad to hear that!
@GolFoxtrotAlpha
@GolFoxtrotAlpha 3 жыл бұрын
That was perfect. Thank you so much!
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
Glad it helped!
@OFaruk58
@OFaruk58 3 жыл бұрын
Dude very nice and clear video!
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
Hey! Thanks! I am glad you like it!
@deepusasidharan2012
@deepusasidharan2012 2 жыл бұрын
Super good explanations!!!
@jahadulrakib9928
@jahadulrakib9928 3 жыл бұрын
Best Video List about Spring Boot ever i seen yet
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
Wow! Big thanks! I really appreciate it!
@jahadulrakib9928
@jahadulrakib9928 3 жыл бұрын
@@CodeForgeYT sir, can you please make a complete Microservice video list using Spring boot?
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
@@jahadulrakib9928 You mean a playlist with existing videos about microservices using Spring Boot?
@jahadulrakib9928
@jahadulrakib9928 3 жыл бұрын
@@CodeForgeYT sir, i want to learn microservice with CQRS and Event Sourcing implementation. you can make a new video series about this topic.
@mauronunes7196
@mauronunes7196 Жыл бұрын
still kicking in 2023, awesome
@user-fj2dd2jx8d
@user-fj2dd2jx8d 2 жыл бұрын
It is so good explanation. Thank you for saving my time. I have one question, if I want to update knight to archer, what should I do? I tried creating new archer instance with old knight's primary key. Then I request save for updating, but new archer was created with ignoring old knight's primary key.
@CodeForgeYT
@CodeForgeYT 2 жыл бұрын
Well conversion between types is topic for separated video
@deepakrohilla5574
@deepakrohilla5574 2 жыл бұрын
Hi, I have to implement similar case. In my Order class, I have list of Product class objects. Where Product is the base Entity with some common properties and Book and Apparel is the child class Entity which extends the Product class. Now while saving the Order using REST service and sending the list of products in JSON format, data is not getting saved in Book and Apparel table. Data is saved only in base table entity means in Product table only. pls help me. let me know how can i share my code snippet?
@msea5654
@msea5654 3 жыл бұрын
i have a question : does the child class inherit associations defined in the parent class ( meaning if the parent have a relationship with another class like one to many or many to many ) does it apply on the child class ?
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
I think so, try it out!
@msea5654
@msea5654 3 жыл бұрын
@@CodeForgeYT i will but if you noticed i asked question , i was hoping to get more in depth info anyway good luck with detailed examples .
@GolFoxtrotAlpha
@GolFoxtrotAlpha 3 жыл бұрын
For example, I just want to find all Archerys from repository. How can I do that? My repository extends InfantryRepository and both has a method which name is findAll().
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
Well calling Iterable infantries = archerRepository.findAll() should do the work and find archers
@giovanni.n
@giovanni.n 3 жыл бұрын
So when you select from Infantry how do you know if it's a knight or archer? Do you have to use instanceof?
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
Hi! If you want to get e.g. archer data from db, you have to use archer repository. You know that you will get archers if you will use ArcherRepository so you don't have to use instanceof. Keep coding!
@giovanni.n
@giovanni.n 3 жыл бұрын
@@CodeForgeYT that of course if i know which type i want. But imagine for example i have a class Squad which has a member field of type Infantry. If I'm loading Squad from db using it's PK. How do i know which subtype the Infantry attribute is holding?
@giovanni.n
@giovanni.n 3 жыл бұрын
It's more of a question about polymorphism than it is about JPA mapping
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
@@giovanni.n Well it is determined when it is being saved to the db. Spring knows the type of object because it is saved in the specific table. Spring is smart enough to resolve entities when it is getting data back from db. It will create required object to put it as the reference to your Squad instance. You can experiment with it and share the results.
@giovanni.n
@giovanni.n 3 жыл бұрын
@@CodeForgeYT yes i think you're right Spring will instantiate it with the correct subtype based on what it found in the db. But for us to know which subtype to cast to in the code without getting ClassCastException error we need to use instanceof first. Or maybe I'm missing something?
@muratalikistan548
@muratalikistan548 3 жыл бұрын
Thanks for this video .I tried this. it is works perfect but i want to ask you something. For example now infantry id=2 . if we have another entity classes different from this , its id is starting from 3 not 1. How we can fix this problem?
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
You have to change the strategy of generting ids for primary keys. For example you can define separated sequence for each entity. Hope it helps, keep coding!
@muratalikistan548
@muratalikistan548 3 жыл бұрын
@@CodeForgeYT Yess Thanks bro. SequenceGenerator fixed this problem. Thank you so much.
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
@@muratalikistan548 You are welcome!
@mossabseifelislemdehane2849
@mossabseifelislemdehane2849 Жыл бұрын
Great explanations but how can I retrieve all infantry
@user-jq6pz1xh6h
@user-jq6pz1xh6h 3 жыл бұрын
Can you help me please . I have almost the same relation between classes, except abstract parent: @Entity Class A; @Entity Class B extends A; And method (A a){ repoA.save(a); B b = new B(); repoB.save(b); } And I get double records in table A and one record in table B. How I can avoid double in A?
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
What you mean by double record? You are creating two different entities and persisting them. Since those are two different entities you get two records in table for class A.
@djamalabdounassirharoun5878
@djamalabdounassirharoun5878 3 жыл бұрын
Hi ! I just came across your tutorial, good job, everything is clear. On the other hand, my problem is with the save function, there you have saved the data manually, what to do if the data comes from a client server like PostMan Sorry for my English, I translate on google translate , Thank's !
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
Hi! You do exactly the same. I have created video where we are implementing one to many web service there is no inheritance but it can be helpful for you. You can check it out, I bet it will answer your questions. Keep Coding!
@djamalabdounassirharoun5878
@djamalabdounassirharoun5878 3 жыл бұрын
@@CodeForgeYT Thank you for your reply ! Which video is in your channel ?
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
@@djamalabdounassirharoun5878 It is in spring playlist, video is called: Java Spring Boot - JPA - Hibernate - H2 - One To Many - REST Web Service - RESTful API
@djamalabdounassirharoun5878
@djamalabdounassirharoun5878 3 жыл бұрын
@@CodeForgeYT Thank's !
@djamalabdounassirharoun5878
@djamalabdounassirharoun5878 3 жыл бұрын
@@CodeForgeYT Unfortunately that doesn't help me. In fact my problem is that with the Joined inheritance strategy, the attributes are zero when I do the POST to save.
@pescoder7486
@pescoder7486 10 ай бұрын
How can you add a infantry object to infantry table???
@muaztastemel538
@muaztastemel538 3 жыл бұрын
How do you use it with spring controller and sevice?
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
You can inject depenedency of repository to service. Then you can simply call different methods from repository in your service depending on your business logic
@muaztastemel538
@muaztastemel538 3 жыл бұрын
CodeForgeYT i mean, for example every entity have different service and there are one service interface so we should these services inject to a main service. May be it should be at complie time.Coz detecting injection problems at complie time is better than run time. So we need to inject services which have same abstraction to a main service (maybe creating hashmap in a configure class). Do you have any example or link about that?
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
@@muaztastemel538 Hi, sorry but I don't have any example for such use case. For sure it is worth to expermient with approach you have described!
@alissonnunes5318
@alissonnunes5318 3 жыл бұрын
I know I am missing the point, but I could not avoid to say that knight is not a type of infantry.
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
Isn't it a type of heavy medival infantry?
@user-xi5vv3pg6o
@user-xi5vv3pg6o 10 ай бұрын
why it is CrudRepo and not JpaRepo ?
@hieuvuduc8840
@hieuvuduc8840 Жыл бұрын
Bad example What different between ArcherRepository and KnightRepository ? Only name ? The truth is you can use KnightRepository to create both archer and knight. Why you have to create two repository ?
Scary Teacher 3D Nick Troll Squid Game in Brush Teeth White or Black Challenge #shorts
00:47
Khó thế mà cũng làm được || How did the police do that? #shorts
01:00
Nutella bro sis family Challenge 😋
00:31
Mr. Clabik
Рет қаралды 12 МЛН
Heartwarming: Stranger Saves Puppy from Hot Car #shorts
00:22
Fabiosa Best Lifehacks
Рет қаралды 21 МЛН
Composition vs. Inheritance with JPA and Hibernate
10:23
Thorben Janssen
Рет қаралды 6 М.
Joined inheritance strategy in hibernate
10:56
KK JavaTutorials
Рет қаралды 4,6 М.
Learn how Spring Boot Model Mapper works Fast and Simple
20:55
Fast and Simple Development
Рет қаралды 22 М.
Scary Teacher 3D Nick Troll Squid Game in Brush Teeth White or Black Challenge #shorts
00:47