JPA/Hibernate Fundamentals 2023 - Lesson 1 - Entities

Sdílet
Vložit
  • čas přidán 2. 08. 2024
  • In this stream, we discuss the JPA specification and learn to implement the persistence layer of a Java app using a JPA implementation such as Hibernate.
    KEY POINTS:
    Understanding JPA, ORM, and Hibernate
    In this lesson, we begin by explaining the concepts of JPA, ORM (Object-Relational Mapping), and Hibernate. JPA is a specification that guides the implementation of ORM frameworks, and it helps in treating database entities as objects in a Java application. Hibernate, on the other hand, is an implementation of the JPA specification and serves as a framework for achieving ORM. We emphasize the importance of understanding these distinctions as we delve deeper into the subject.
    Context and Execution of Operations in ORM
    As we progress through this playlist, you'll come to realize that in ORM (Object-Relational Mapping), operations are not executed directly on the database management system but within a context. This context contains instances of objects, known as entities, and operations are performed on these entities. The framework then decides which operations need to be translated into database queries. It's crucial to grasp this concept as it will affect how you work with Hibernate and JPA, especially when it comes to optimizing performance and understanding the behavior of your application.
    Understanding Hibernate and Its Common Use Cases
    Hibernate is a widely used Java Persistence API (JPA) implementation and one of the most common frameworks in the Java ecosystem. It's frequently used alongside popular application frameworks like Spring, particularly in Spring Data JPA. In this lesson, we'll explore the reasons behind choosing Hibernate as the JPA implementation for this playlist. Understanding Hibernate and its role is essential for any Java developer, as it provides valuable insights into the world of ORM and database persistence.
    Configuring Hibernate with Persistence XML
    To set up Hibernate for a simple Java project, we'll use a classic approach: the Persistence XML file. This file, typically named 'persistence.xml,' is located in the 'META-INF' directory of your project's resources folder. We'll explore how to create and configure this XML file, explaining key elements like the 'persistence-unit' name, transaction type, description, and provider. This configuration allows Hibernate to establish a connection to the database, laying the foundation for subsequent operations.
    Understanding Object-Relational Mapping (ORM) and Entities
    Object-Relational Mapping (ORM) is a framework philosophy that bridges the gap between object-oriented programming and relational databases. It allows developers to work with objects in their code, abstracting the complexities of database operations. Entities in ORM represent database tables and their attributes. Entities are a fundamental concept in ORM, and they serve as the foundation for modeling data in the database. In this lesson, we've explored how to define and use entities in Hibernate, illustrating how they map to database tables and how to persist them.
    Programmatic Approach to Define Persistence Unit
    In this lesson, we explored an alternative to using an XML file for defining a persistence unit in Hibernate. We demonstrated how to create a custom `PersistenceUnitInfo` class to programmatically configure the persistence unit. This approach allows you to replace the XML file with a Java class, simplifying the configuration process. We discussed the essential methods to implement in the `PersistenceUnitInfo` interface and how to set properties such as the data source. Additionally, we highlighted the importance of not hard-coding sensitive information like database credentials and suggested storing them securely in vaults or environment variables.
  • Věda a technologie

Komentáře • 89

  • @laurspilca
    @laurspilca  Před rokem +9

    Code on GitHub: github.com/lspil/youtubechannel/tree/master/jpa_2023_c1_e1

  • @hrsatsangi
    @hrsatsangi Před rokem +29

    Please laur complete this series as soon as possible. We are always eagerly waiting for lectures.
    Love From India 🇮🇳🇮🇳

  • @1711jk
    @1711jk Před rokem +6

    I wish I knew you 3 years earlier. It would be a rescue for me and my life could go anorher way...

  • @balazsklezli5702
    @balazsklezli5702 Před rokem +3

    Thank you for the never ending great tutorials Laur!

  • @lts8683
    @lts8683 Před rokem +6

    I hope also to talk about:
    - the comparison jOOQ and JPA.
    - Database migration using flyway for ex.
    I love your tutorial.

  • @Andr805
    @Andr805 Před rokem +1

    Thank you Laur, you're amazing!

  • @thalathotitharunprabhakar3390

    Thank you Laur for starting the playlist.

  • @ahmedelwa2000
    @ahmedelwa2000 Před rokem +7

    I am big fan from Egypt, professional SW as well. Your videos and books are intensive and sufficient!
    Greetings from Cairo again.

  • @terencechia9986
    @terencechia9986 Před 2 měsíci

    These fundamental lessons are exactly what I needed. I've starting software development lately and everything I'm using is highly abstracted, I admit I have no idea how most of my tools work.
    By knowing the fundamentals, I'm confident I can have much greater impact later in my career.

  • @jagdishtawde2077
    @jagdishtawde2077 Před rokem

    Thank you so much for creating such wonderful videos! Your content is truly inspiring and has brought me so much joy and knowledge. Keep up the fantastic work, and know that your efforts are deeply appreciated. Looking forward to more amazing videos from you. Thank you again
    🤟

  • @nehurane
    @nehurane Před rokem +1

    Good in depth detail explanation.

  • @rajan-u6b
    @rajan-u6b Před rokem

    great lecture as always sir, learn alot thank you

  • @Daniel-Murphy951
    @Daniel-Murphy951 Před 5 měsíci

    Hey there Laur. I'm pretty new to all this new java stuff like Spring, JPA, hibernate, etc. I used to work in AI/ML research but now I'm doing this.
    I just wanted to say that I absolutely love your teaching. It has seriously helped me understand all these crazy concepts. Thank you so much!

  • @hoki8296
    @hoki8296 Před rokem

    Great help... oh... My savior!!

  • @wul_frik
    @wul_frik Před 4 měsíci

    Thanks for this playlist

  • @marounsleiman9230
    @marounsleiman9230 Před rokem +1

    Thanks a lot Laur. Your lessons are so precious. ❤️

  • @arunkumarka1809
    @arunkumarka1809 Před 10 měsíci

    It is a great tutorial 👏👏...keep up your work......

  • @abhishekomprakash4074
    @abhishekomprakash4074 Před 3 měsíci

    Enjoyed learning from your video. thanks💯

  • @hossamaddin7980
    @hossamaddin7980 Před rokem +1

    awesome 🤩

  • @ezekieleromosei6627
    @ezekieleromosei6627 Před 10 měsíci

    It's been an honor learning from you☺

  • @JalilHasanov
    @JalilHasanov Před 10 měsíci

    YOU ARE AWESOME

  • @aviatorgs8202
    @aviatorgs8202 Před 8 měsíci

    I really like your videos and have actually learnt a lot, would be good if you'd do a Spring Data Mongo series

  • @ikennashonowo9250
    @ikennashonowo9250 Před rokem

    Great video. Expecting more.

  • @byronkenoly
    @byronkenoly Před 7 měsíci

    very good tutorial

  • @badscrew4023
    @badscrew4023 Před 8 měsíci

    I used to come to București for work in the past, miss the wild night parties and good people there :)

  • @user-dl5iw7nq4d
    @user-dl5iw7nq4d Před 5 měsíci

    thanks :)

  • @user-ty4qg2cs6f
    @user-ty4qg2cs6f Před 8 měsíci

    Hey Laur, I've been scouring the internet for resources on JPA/Hibernate, and I must say, your tutorial stands out as one of the most comprehensive ones I've come across. Kudos on creating such a valuable resource!

  • @enestalha5718
    @enestalha5718 Před 4 měsíci

    Thank you so much for the course. You are great, greetings from Turkey!

    • @laurspilca
      @laurspilca  Před 4 měsíci

      Btw. I will be at Java Day Istanbul :) Maybe we meet there.

  • @marekKulczyk
    @marekKulczyk Před rokem

    Great video! Please take into consideration buying pop filter for your mic so the video quality will be even better!

  • @kalaimaranmadhavan4182
    @kalaimaranmadhavan4182 Před 10 měsíci +1

    please cover bidirectional, earger and lazy fetch, cache

  • @naeemkhan4534
    @naeemkhan4534 Před rokem +2

    Hi Laur, Few Questions?
    1. Are ORM frameworks used in Production Level Application?
    2. If Yes, Can they be used with Large Scale Applications?
    3. If Not, Why Learn them?
    3.1 If Not, What is/are used in actual production Level Application?
    Looking forward to your answer.

    • @laurspilca
      @laurspilca  Před rokem +1

      Hello. Thanks for your questions. Here're my answers.
      1. Yes, definitely
      2. Yes, of course they are
      In fact, Hibernate and the JPA specification in general is one of the most encountered technologies in production apps in the Java world.

  • @piyushlovanshi8142
    @piyushlovanshi8142 Před rokem +1

    Hi Laur, I went through a course(a few years old course) starting that "session is to be used" in place of Entity which you are using, my question is "was there a change recently or can session still be used?".
    Thanks you for the educational videos, greatly appreciated.

    • @laurspilca
      @laurspilca  Před rokem +1

      Hey. Even when working with plain Hibernate today it's likely you'll prefer using EntityManager rather than Session today. But in the end, behind the scenes this only adapts the Session to the JPA specification.

  • @vakhtangdonadze9490
    @vakhtangdonadze9490 Před 8 měsíci

    @laurspilca First of all I'm really grateful for all your videos, they are truly one of a kind as they feel more like a solid lecture than a rushed tutorial. I really appreciate your in depth and theoretical explanation. My question is a bit off topic, but I'm really in your opinion regarding Maven and Gradle, what is the case for using Maven today when Gradle is also available ?

    • @laurspilca
      @laurspilca  Před 8 měsíci

      Hello. Thanks for your question. I don't think Gradle excludes Maven or the other way around. From my experience Maven seems to be the preferred in most situations. But it's only a matter of taste.

  • @buntyshadangi-ou6vk
    @buntyshadangi-ou6vk Před 4 měsíci

    Thanks Laur for being so nice. Want to know your view on impact of AI on Java development

    • @laurspilca
      @laurspilca  Před 4 měsíci

      Well, hard to predict that :)

  • @anuj_in_nz
    @anuj_in_nz Před rokem +2

    Hello ! Thank you very much for this video, as always you explain things very well. A question if I may: When the persistence.xml file was being used to bootstrap the emf, what datasource was used ? The reason why I ask is because when using the CustomerPersistenceUnitInfo, you explicitly added and configured Hikari datasource which makes me wonder what datasource did the xml file approach used.

    • @laurspilca
      @laurspilca  Před rokem

      Hey. Good question. In that case you don't have too much flexibility. Depending on the implementation you use and its version it may be different types of data sources configured.

  • @user-nv1ym9qz7g
    @user-nv1ym9qz7g Před rokem +2

    I have gone through all your spring and jpa course. Is there any chance of creating Kafka and RabbitMq using spring play list as not much content is not availble in google which explains the whole concepts.

  • @adhiprasetyo4052
    @adhiprasetyo4052 Před rokem +1

    I hope you will write book about jpa/hibernate, thank you

  • @loray2944
    @loray2944 Před 7 měsíci +1

    Hello, you mentioned you have covered java fundamentals, did you cover by the chance sql, more specifically transactions and acid?

    • @laurspilca
      @laurspilca  Před 6 měsíci

      Hello. No, I didn't make any playlist on SQL. I only have Java lessons on this channel :) But maybe it''d be a good idea to add a SQL fundamentals playlist at some point :)

  • @AliHassan-bz9sk
    @AliHassan-bz9sk Před 11 měsíci +1

    Thanks! I just have two silly questions.😅
    -> what does the term "persistence" actually means when we talk about JPA because in different context it have different meanings?
    -> Are getters mandatory to be specified by a class annotated with @Entity?

    • @laurspilca
      @laurspilca  Před 11 měsíci +2

      Hi. Thanks for your questions.
      1. In this case, persistence means persistently storing data in such a way in which you can later retrieve it.
      2. While a JPA implementation can inject directly in the fields without needing to call setters and getters it's hard to believe you'll not need to access the data at some point later. So getters are not mandatory, but to get the values outside of the class, you'll most likely need them.

    • @AliHassan-bz9sk
      @AliHassan-bz9sk Před 11 měsíci +1

      @@laurspilca thanks

  • @shramlearn
    @shramlearn Před 6 měsíci

    Laur Spilca the legend

  • @momedalhouma14
    @momedalhouma14 Před rokem

    Can you show how spring boot autoconfigure the connection to the database? what you showed here is a manual configuration. it will help us undestrand how spring works in the background.

    • @laurspilca
      @laurspilca  Před rokem +2

      Yes. But not in this playlist. This playlist is only about JPA and Hibernate. The whole idea (as I mentioned also in the video) is having developers understand properly everything that's behind the scenes when they use Hibernate regardless of the framework they use.

  • @balajikalva188
    @balajikalva188 Před 11 měsíci +2

    Hi Laur,
    I have a doubt on when the context is created here . Is it specific to a transaction or will that be specific to each entity manager? We are getting EntityManager from the factory specifying the database (persistence unit details) , will the context be specific to this entity manager created

    • @laurspilca
      @laurspilca  Před 11 měsíci

      Hi. The entity manager has the context and manages it. An entity manager can execute multiple transactions on a context. At a transaction commit the context is mirrored to the DB. I hope this helps.

  • @rydmerlin
    @rydmerlin Před rokem +1

    I have a JPA question. Suppose you have an entity with 10 attributes and you want to support aggregations like sum, avg etc on any combination of those 10 attributes. What approach do you take that isn’t too static? Ie. You don’t want to have an explosion of statically defined queries to support this.

    • @laurspilca
      @laurspilca  Před rokem +1

      Hey. Let's discuss this in future lessons.

  • @naeemkhan4534
    @naeemkhan4534 Před 3 měsíci

    Hey Laur, I have some questions about the flow from application to DB.
    We have two dependencies now, Hibernate and Hikari.
    From what I know DataSource manages the connection pool.
    Also Hibernate persists finally to DB.
    Where does Hikari comes then in this flow. Can you please tell how Hikari is used here.
    Hibernate calls Hikari?

  • @medokarim2511
    @medokarim2511 Před rokem +1

    Please try to schedule the live before it's time with one or two days

    • @laurspilca
      @laurspilca  Před rokem

      Sure. This one was scheduled at least a week in advance :)

  • @marounsleiman9230
    @marounsleiman9230 Před rokem +2

    Hi Laur, I have a question about @Transactional((readOnly = true) and @Transactional. If I call a repository from a service class where the method in service class is annotated with @Transactional(readOnly=true) and the method in repository interface annotated with @Transactional. Will @Transactional in repository class override @Transaction(readOnly=true) in service class? What I think is that the answer is no, since the default propagation is REQUIRED which will take the first place Transactional is set and it will be applied through the chain of calls. Is that correct?

    • @laurspilca
      @laurspilca  Před rokem +1

      Hi, Yes. I would expect the @Transactional in the repository to override the setup at the service level.

    • @marounsleiman9230
      @marounsleiman9230 Před rokem

      @@laurspilca thanks a lot. So propagation is not helping here. The Transactional will always override Transaction(readOnly=true) ?

    • @marounsleiman9230
      @marounsleiman9230 Před rokem

      @laurspilca since the propagation has as default required which means that the @transactional you define in the first method should follow to all sub methods. Doesn't this mean that the Transactional defined in sub methods are not considered? Which means that if the Transaction defined in first method is defined as readOnly=true, this will follow to all sub methods? Or is it per design that Transactional will always override Transaction(readOnly=true) ? Thanks for your time

    • @laurspilca
      @laurspilca  Před rokem +1

      @@marounsleiman9230 To be honest, I'm not 100% sure about the readOnly attribute. This situation is quite strange. I don't see why you'd want to override the readOnly. Normally for other attributes it makes sense and for those I'm pretty sure the following @Transactional overrides the initial. But those are situations you find in real-world. The case of readOnly you mention is quite theoretical. I have to check to see what happens to be sure.

  • @2GGop
    @2GGop Před 5 měsíci

    How come the XML configuration variant doesn't use a connection pool? I thought they were essentially equivalent approaches.

    • @laurspilca
      @laurspilca  Před 5 měsíci

      Hello. It does use a connection pool behind the scenes. But I minimized the configuration and this way I left the default implementation.

  • @user-xg5oq2ks6z
    @user-xg5oq2ks6z Před 11 měsíci

    35:00

  • @MrC0pyP4st3
    @MrC0pyP4st3 Před rokem +2

    Hey Laurentiu, big fan! Just wanted to ask a quick question: Is your JPA/Hibernate playlist from 3 years ago still relevant? In which way will the current series differ from the old one?

    • @laurspilca
      @laurspilca  Před rokem +2

      Hi. The old playlist is still relevant. However it had not been recorded quite well. There are a lot of problems with the sound. That's one of the reason I decided to remake it.

  • @ravi-thestar8501
    @ravi-thestar8501 Před rokem

    which software are you using for recording videos?

    • @laurspilca
      @laurspilca  Před rokem +2

      It's OBS. But I don't usually record, I most often just go live.

  • @DeepakLalchandaniProfile

    Please let me know the date of spring security 2nd edition releasing

    • @laurspilca
      @laurspilca  Před rokem

      Somewhere in autumn maybe? It's difficult to give a date since it's mainly not me making this possible

  • @LeoVEggers
    @LeoVEggers Před 6 měsíci

    where did that persistence xml text came from??

    • @laurspilca
      @laurspilca  Před 6 měsíci +1

      Hello. Thanks for the question. The syntax is described in the JPA specification. That XML in the example I configured it myself.

    • @LeoVEggers
      @LeoVEggers Před 6 měsíci

      @@laurspilca thank you!

  • @henokkeraga8582
    @henokkeraga8582 Před rokem +1

    can you send us github link

    • @laurspilca
      @laurspilca  Před rokem

      Yes. Already added it. You can find it in the pinned comment.

  • @cesarjusto6225
    @cesarjusto6225 Před rokem

    Please subttitle in english

    • @laurspilca
      @laurspilca  Před rokem

      Hi. Subtitles are enabled but YT takes longer to process long videos. I don't think it's something I could do about that.

  • @valdrinium
    @valdrinium Před rokem +1

    I like your videos, but why are you using such a Tinder thumbnail? It looks like you wanna stick in your viewers more than just jpa/hibernate knowledge, if you know what i mean *wink* *wink*

    • @laurspilca
      @laurspilca  Před rokem +1

      Hahaha. Lol. Never thought about that. Maybe you just have a dirty mind :))))

    • @piyushlovanshi8142
      @piyushlovanshi8142 Před rokem

      😂 actually I love the theme per topic, it makes it much more easier to differentiate between a sea of videos

  • @a.m.jyotiprakashsahu5754

    sir, please make a video on Spring security 6 with Keycloak using OpenID.

  • @JalilHasanov
    @JalilHasanov Před 10 měsíci

    @laurspilca Hi first of all thanks for a deep explanation. I get this kind of error while running: Unable to locate persister.
    p.s I use mariaDB

    • @laurspilca
      @laurspilca  Před 10 měsíci

      Hi. Did you enumerate your entities in the PU class?