JPA/Hibernate Fundamentals 2023 - Lesson 1 - Entities
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
Code on GitHub: github.com/lspil/youtubechannel/tree/master/jpa_2023_c1_e1
Please laur complete this series as soon as possible. We are always eagerly waiting for lectures.
Love From India 🇮🇳🇮🇳
I wish I knew you 3 years earlier. It would be a rescue for me and my life could go anorher way...
Thank you for the never ending great tutorials Laur!
I hope also to talk about:
- the comparison jOOQ and JPA.
- Database migration using flyway for ex.
I love your tutorial.
Thank you Laur, you're amazing!
Thank you Laur for starting the playlist.
I am big fan from Egypt, professional SW as well. Your videos and books are intensive and sufficient!
Greetings from Cairo again.
Thank you!
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.
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
🤟
Good in depth detail explanation.
great lecture as always sir, learn alot thank you
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!
Great help... oh... My savior!!
Thanks for this playlist
Thanks a lot Laur. Your lessons are so precious. ❤️
Thank you!
It is a great tutorial 👏👏...keep up your work......
Enjoyed learning from your video. thanks💯
awesome 🤩
It's been an honor learning from you☺
YOU ARE AWESOME
I really like your videos and have actually learnt a lot, would be good if you'd do a Spring Data Mongo series
Great video. Expecting more.
More will come, definitely :)
very good tutorial
I used to come to București for work in the past, miss the wild night parties and good people there :)
thanks :)
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!
Glad to hear it helps :)
Thank you so much for the course. You are great, greetings from Turkey!
Btw. I will be at Java Day Istanbul :) Maybe we meet there.
Great video! Please take into consideration buying pop filter for your mic so the video quality will be even better!
please cover bidirectional, earger and lazy fetch, cache
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.
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.
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.
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.
@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 ?
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.
Thanks Laur for being so nice. Want to know your view on impact of AI on Java development
Well, hard to predict that :)
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.
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.
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.
I hope you will write book about jpa/hibernate, thank you
Hello, you mentioned you have covered java fundamentals, did you cover by the chance sql, more specifically transactions and acid?
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 :)
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?
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.
@@laurspilca thanks
Laur Spilca the legend
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.
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.
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
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.
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.
Hey. Let's discuss this in future lessons.
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?
Please try to schedule the live before it's time with one or two days
Sure. This one was scheduled at least a week in advance :)
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?
Hi, Yes. I would expect the @Transactional in the repository to override the setup at the service level.
@@laurspilca thanks a lot. So propagation is not helping here. The Transactional will always override Transaction(readOnly=true) ?
@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
@@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.
How come the XML configuration variant doesn't use a connection pool? I thought they were essentially equivalent approaches.
Hello. It does use a connection pool behind the scenes. But I minimized the configuration and this way I left the default implementation.
35:00
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?
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.
which software are you using for recording videos?
It's OBS. But I don't usually record, I most often just go live.
Please let me know the date of spring security 2nd edition releasing
Somewhere in autumn maybe? It's difficult to give a date since it's mainly not me making this possible
where did that persistence xml text came from??
Hello. Thanks for the question. The syntax is described in the JPA specification. That XML in the example I configured it myself.
@@laurspilca thank you!
can you send us github link
Yes. Already added it. You can find it in the pinned comment.
Please subttitle in english
Hi. Subtitles are enabled but YT takes longer to process long videos. I don't think it's something I could do about that.
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*
Hahaha. Lol. Never thought about that. Maybe you just have a dirty mind :))))
😂 actually I love the theme per topic, it makes it much more easier to differentiate between a sea of videos
sir, please make a video on Spring security 6 with Keycloak using OpenID.
@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
Hi. Did you enumerate your entities in the PU class?