JPA/Hibernate Fundamentals 2023 - Lesson 2 - The context

  Рет қаралды 10,658

Laur Spilca

Laur Spilca

Күн бұрын

Пікірлер: 51
@laurspilca
@laurspilca Жыл бұрын
Code on GitHub: github.com/lspil/youtubechannel/tree/master/jpa_2023_c2_e1 Jakarta persistence API specification: jakarta.ee/specifications/persistence/3.0/jakarta-persistence-spec-3.0.pdf
@nerminkarapandzic5176
@nerminkarapandzic5176 Жыл бұрын
I always learn something from your videos, this channel is so underrated. Thank you for sharing the knowledge
@laurspilca
@laurspilca Жыл бұрын
Glad to hear this :)
@rohitgupta025
@rohitgupta025 11 ай бұрын
One of the most underrated and detailed content creators on KZbin
@laurspilca
@laurspilca 11 ай бұрын
Thank you :)
@taif2705
@taif2705 Жыл бұрын
Thank you Laurentiu Spilca for your efforts. I always come to this channel first to get a clear understanding of the fundamentals. I was playing around (as always) with the code and I found out that using persist() instead of merge() when there's an entry in the DB with same PK, actually throws ERROR: Duplicate entry '1' for key 'employees.PRIMARY' 😄
@laurspilca
@laurspilca Жыл бұрын
Hello. Yes, it makes sense. You cannot have duplicate PKs in the DB.
@raghavanaliassaravananm1546
@raghavanaliassaravananm1546 Жыл бұрын
Thank you very much for sharing your knowledge @Laur Spilca.
@Boyarsskiy
@Boyarsskiy Жыл бұрын
I'm an experienced developer, but every time I learn some new details from these videos. Thanks for your videos, Laurentiu, it's a good refresher for experienced developers too.
@laurspilca
@laurspilca Жыл бұрын
Happy to hear this :)
@sahil_0562
@sahil_0562 6 ай бұрын
Thank you so much for sharing your knowledge Laur.
@fiorini_mochachino
@fiorini_mochachino Жыл бұрын
Finally understood what merge does. Thanks!
@ggsay1687
@ggsay1687 4 ай бұрын
Thanks for videos, great work!
@trishulcurtis1810
@trishulcurtis1810 Жыл бұрын
In-depth clear explanation!
@ionut-cosmingradinaru5514
@ionut-cosmingradinaru5514 Жыл бұрын
Awesome work man 👏
@anuj_in_nz
@anuj_in_nz Жыл бұрын
I just had a go with playing around with this to understand persist and merge better. In these examples, the Id is auto set. Example 1: Employee e1 = new Employee(); e1.setName("Captain America"); em.persist(e1); // e1 now enters the context and becomes managed e1.setName("Thor"); em.merge(e1); // e1 is already managed therefore this merge operation is ignored. em.getTransaction().commit(); // a single row with value "Thor" exists. Example 2: Employee e1 = new Employee(); e1.setName("Captain America"); em.merge(e1); // e1 now enters the context, however is not managed. e1.setName("Thor"); em.merge(e1); // e1 not managed therefore is added to the context em.getTransaction().commit(); // 2 rows exists, one with value "Thor" and other with "Captain America" Example 3: Employee e1 = new Employee(); e1.setName("Captain America"); var updated = em.merge(e1); // e1 now enters the context, however is not managed. Use the returned entity which is managed updated.setName("Thor"); em.merge(updated); // updated is managed therefore is merged with the existing in the context em.getTransaction().commit(); // 1 row exists with the value "Thor"
@BravePro
@BravePro Жыл бұрын
wdym by not managed? isn't it that when the entity enters the context it automatically becomes managed?
@hoki8296
@hoki8296 Жыл бұрын
Thank you! It was great lesson as always
@rishabhkumar8042
@rishabhkumar8042 6 ай бұрын
Thank you so much for this series, i am learning a lot
@laurspilca
@laurspilca 6 ай бұрын
Glad to hear !
@lts8683
@lts8683 Жыл бұрын
Thank you very much
@ravi-thestar8501
@ravi-thestar8501 Жыл бұрын
Audio quality is great in this video.. please continue
@laurspilca
@laurspilca Жыл бұрын
Older videos have a poor quality. It seems it was mi laptop at that time which wasn't so performant. I try to improve.
@sandeepspurgeon2317
@sandeepspurgeon2317 Жыл бұрын
Thank you so much for this series, i am learning a lot❤
@Daniel-Murphy951
@Daniel-Murphy951 9 ай бұрын
Best JPA teacher ever!
@laurspilca
@laurspilca 9 ай бұрын
@intBRiNGER
@intBRiNGER Жыл бұрын
Hey @Laur, First of all, thank you for the content. I just paid attention to defining DataSource in Java-based config. The type of PersistenceUnitTransactionType was chosen as RESOURCE_LOCAL and my understanding is that the more appropriate way here is to use the getNonJtaDataSource() method for defining the DataSource instead of getJtaDataSource() method.
@sagarsrt6321
@sagarsrt6321 Жыл бұрын
Do we need to use Lock mode types? How these are different from Isolation levels? Please make a video on them.
@webeltech8755
@webeltech8755 Ай бұрын
In which video did you teach about @Embendable classes?
@mosaed_b
@mosaed_b Жыл бұрын
If you could later (in the next session - or the after) explain the ByteBuddy library, and how it is related and used in Hibernate. Thank you
@nipunkataria4588
@nipunkataria4588 Жыл бұрын
I have a question em.merge(entity); entity.setName("anyName"); will this change mirrored to the db at end of transaction when we try to commit ? I tried it in my local but the new change is not getting reflected in database. Can someone explain ?
@laurspilca
@laurspilca Жыл бұрын
Hi. Yes. It will be mirrored as you can see in my demonstration. In your case if it doesn't it's most likely because you tried to merge en entity that doesn't exist (by primary key) in the DB. Rememeber that when you use merge, you need to merge something that already exists in the DB. So the ID should exist. Otherwise, you need to use persist(). I hope this helps :)
@ravi-thestar8501
@ravi-thestar8501 Жыл бұрын
@@laurspilca I have tried this case with an entity already in the database. but i see that new changes after merge call are not reflected in database. I see merge is returning me an instance of merged entity, so i tried updating the entity returned by merge and i can see the change now. so i think. merge will clone and use a copy of data. please correct me if i am wrong Note : i read below lines on web JPA’s merge method JPA’s merge method copies the state of a detached entity to a managed instance of the same entity. Hibernate, therefore, executes an SQL SELECT statement to retrieve a managed entity from the database. If the persistence context already contained a managed instance of the entity, Hibernate uses the existing one instead. It then copies all attribute values to the managed entity and returns it to the caller. 1 Author managedAuthor = em.merge(a);
@laurspilca
@laurspilca Жыл бұрын
@@ravi-thestar8501 Ok. Cool. Maybe it depends on the implementation. I'll check that.
@sanketrajgarhia5532
@sanketrajgarhia5532 Жыл бұрын
At 17:00 mins - The name of the table is case insensitive is NOT TRUE for MySQL. If the name of the table is `employee` and the name of the class is Employee then @Table(name="employee") is definitely required unlike discussed in the video. This is what I experienced on MySQL.
@laurspilca
@laurspilca Жыл бұрын
Hey. I'm using MySQL in this playlist. :)
@zaidrj7374
@zaidrj7374 Ай бұрын
Thanks
@latin_void
@latin_void 9 ай бұрын
Hi! I have a question, where does the context "live"? Is it like a cache given by hibernate? Love the content greetings from Argentina :)
@laurspilca
@laurspilca 9 ай бұрын
Hola. The context is actually a collection managed by the framework. I wouldn't call it a cache.
@ravi-thestar8501
@ravi-thestar8501 Жыл бұрын
Hi Laur, i tried to merge an entity which is not in DB and it was working actually. can you try once by inserting a new record thats not in DB please
@laurspilca
@laurspilca Жыл бұрын
Hi. Since I can't see your code I don't know what exactly you do there, sorry. I can just explain how it should work. merge() -> used to merge in context an instance with a primary key existing in DB. persist() -> adding a new instance which will result in adding a new record to the DB.
@AliHassan-ty2me
@AliHassan-ty2me Жыл бұрын
Hey, Consider these two situations: 1) if you try to merge an entity whose primary key matches one of the entity in the database, then hibernate will update that entity which is present in the database. 2) if you try to merge an entity whose primary key doesn't matches one of the entity in the database, then hibernate will make an Insert query. So, merge() actually behaves as persist() if hibernate fails to find an entity having the same primary key in the database.
@jinoia
@jinoia 6 ай бұрын
55:01 - merge
@YounasPA-d8r
@YounasPA-d8r 9 ай бұрын
thanks laur :)
@ravi-thestar8501
@ravi-thestar8501 Жыл бұрын
hi Laur, request you to please cover transaction propagation levels with some practical examples in this series
@laurspilca
@laurspilca Жыл бұрын
Hi. You can already find that in my Spring fundamentals series. Just search by isolation on the channel, you'll find them :)
@ravi-thestar8501
@ravi-thestar8501 Жыл бұрын
@@laurspilca sure Laur.. thanks much
My scorpion was taken away from me 😢
00:55
TyphoonFast 5
Рет қаралды 2,7 МЛН
BAYGUYSTAN | 1 СЕРИЯ | bayGUYS
36:55
bayGUYS
Рет қаралды 1,9 МЛН
Quando eu quero Sushi (sem desperdiçar) 🍣
00:26
Los Wagners
Рет қаралды 15 МЛН
JPA / Hibernate Marathon
3:47:39
Laur Spilca
Рет қаралды 2,7 М.
JPA/Hibernate Fundamentals 2023 - Lesson 1 - Entities
1:05:22
Laur Spilca
Рет қаралды 38 М.
Service Class Episode 01: Service and CRUD Operations
44:53
Shieth شِيث
Рет қаралды 9
With Thorben Janssen on Java Persistence
53:51
Laur Spilca
Рет қаралды 821
JPA/Hibernate Fundamentals 2023 - Lesson 4 - Primary keys
41:06
Laur Spilca
Рет қаралды 5 М.
JPA/Hibernate Fundamentals 2023 - Lesson 8 - Entity Inheritance
41:45
Ways to create thread : Part : 2
1:49:53
SmartCode Studio
Рет қаралды 4
JPA/Hibernate Fundamentals 2023 - Lesson 13 - Criteria query
51:45
Laur Spilca
Рет қаралды 4,8 М.
My scorpion was taken away from me 😢
00:55
TyphoonFast 5
Рет қаралды 2,7 МЛН