Excellent stream, I like your explanation and examples. I've been watching your jpa/hibernate playlist also - It's good but more certification and theory-oriented and I like it, but this is the good stuff. I would want to have only two more wishes. One is to have more time to watch your videos and code along. And the second thing is you to produce even more practical streams like this about java/jpa/rdbms and real-world app problems and best practices with spring, jpa etc.
I encountered the same issues during my development and I also came up with the same solution. Thanks for suggesting VisualVM. How to handle 'n+1' Query issue when there is a cyclic reference. For example, In your Classroom entity has a reference to the Student such as ClassLeader.
Hello. Do you mean cyclic or transient reference? It's transient only if class leader is also a student record. If you keep it in a separate table that it's transient.
Hi Rahhi. No. If the entity is no longer in the context, then the changes on it will not be mirrored. However, I don't think that's what you'd like to do in most cases in a prod app. If you don't want changes, you rather never make them at all.
Extending your repository with QueryByExampleExecutor would allow you to use query by Example methods. I think that's what that question regarding "example" was all about.
Hi Laur! Awesome video, really helpful. I am curious why are you always creating the tables manually in the database instead of letting the ORM do that?
Hi Alexandra. Thanks for the question. In a real-world app, we never allow Hibernate (or any other JPA implementation) to create the tables, so I don't want through my tutorials to make anyone believe this is a good practice. Thus, I create the tables manually so stay as close as possible to how a real-world app works. Of course, in a real-world app we would also version our migration scripts using either Flyway or Liquibase. But never through the ORM framework.
Is there a way to exclude some child properties on the entity graph? For example the classroom has 2 child elements; students and another entity e.g professors (both of them are one to many from classroom). Now, on my 1st endpoint I want to return all the classrooms and ONLY the students linked to them (so I want to left join only the students table and exclude the join with the other child (or other children elements if there are more relations), whereas on my 2nd enpoint I want to return the classrooms and all their child elements (in this case I want to left join the students table and the professors table). I could have an entity with 10 child elements (mostly many to one) and I don't want to expose all the child elements on every endpoint and join 10 tables everytime I want to fetch this entity; I would like to return specific child properties on specific endpoints. I hope my question is clear, thanks a lot!
@@laurspilca thanks I have tried that but now for example if I want to return in response the parent with for example 5 child elements (5 lists with tens of elements for example), I won't have one query with 5 joins but more queries.. just wanted to know whether it is possible to make any preconfiguration in cases where I know exactly what I will need to return in response.. because even if I write a @Query above the method, it still doesn't seem to work the way I want to - it still fetches the other elements
@@rinorahmeti462 Hm. Strange. I'm not sure about your case. Maybe I don't understand it. Usually you should be able to use a JOIN fetch on the query for exactly what you need while leaving the others lazy.
@@laurspilca I had one ManyToMany relationship which was causing the issue perhaps.. if I am able to reproduce that case I will let you know because looks to be an interesting issue. Thanks!
Thank you sir, this was a great tip. Please keep them coming. I am very excited for your new book as well.
Thanks, Shawn!
thank you, it was very useful and now I realized what a mess my project is
Excellent stream, I like your explanation and examples. I've been watching your jpa/hibernate playlist also - It's good but more certification and theory-oriented and I like it, but this is the good stuff.
I would want to have only two more wishes.
One is to have more time to watch your videos and code along. And the second thing is you to produce even more practical streams like this about java/jpa/rdbms and real-world app problems and best practices with spring, jpa etc.
I encountered the same issues during my development and I also came up with the same solution. Thanks for suggesting VisualVM. How to handle 'n+1' Query issue when there is a cyclic reference. For example, In your Classroom entity has a reference to the Student such as ClassLeader.
Hello. Do you mean cyclic or transient reference? It's transient only if class leader is also a student record. If you keep it in a separate table that it's transient.
@@laurspilca I was talking about if the Class leader is also a Student (Student table)
@@RajeevanKuganathan Ok, but in that case, you can apply @GraphEntity on that field. I don't see why that shouln't work.
Code on GitHub: github.com/lspil/youtubechannel/tree/master/two_big_problems_with_JPA
You r just amazing. All your video are excellent..super excellent
Does the unwanted update only occurs with @Transactional annotation? If we remove the annotation, This is not a problem?
Yes. If you remove the @Transactional annotation no update can occur anymore since you don't have a transaction.
Second problem starts at 39:15
Thank you! if we detach the entity from the context before any changes then will it update the changes into DB.
Hi Rahhi. No. If the entity is no longer in the context, then the changes on it will not be mirrored. However, I don't think that's what you'd like to do in most cases in a prod app. If you don't want changes, you rather never make them at all.
@@laurspilca Thank you
Extending your repository with QueryByExampleExecutor would allow you to use query by Example methods. I think that's what that question regarding "example" was all about.
Got it. Thanks. That'll be another video I guess :)
Hi Laur! Awesome video, really helpful. I am curious why are you always creating the tables manually in the database instead of letting the ORM do that?
Hi Alexandra. Thanks for the question. In a real-world app, we never allow Hibernate (or any other JPA implementation) to create the tables, so I don't want through my tutorials to make anyone believe this is a good practice. Thus, I create the tables manually so stay as close as possible to how a real-world app works. Of course, in a real-world app we would also version our migration scripts using either Flyway or Liquibase. But never through the ORM framework.
@@laurspilca Thanks a lot!
Is there a way to exclude some child properties on the entity graph?
For example the classroom has 2 child elements; students and another entity e.g professors (both of them are one to many from classroom).
Now, on my 1st endpoint I want to return all the classrooms and ONLY the students linked to them (so I want to left join only the students table and exclude the join with the other child (or other children elements if there are more relations), whereas on my 2nd enpoint I want to return the classrooms and all their child elements (in this case I want to left join the students table and the professors table).
I could have an entity with 10 child elements (mostly many to one) and I don't want to expose all the child elements on every endpoint and join 10 tables everytime I want to fetch this entity; I would like to return specific child properties on specific endpoints.
I hope my question is clear, thanks a lot!
Hi. I guess you can make the field fetch LAZY?
I mean, make the fields lazy and fetch them only when you need, only what you need on the query.
@@laurspilca thanks I have tried that but now for example if I want to return in response the parent with for example 5 child elements (5 lists with tens of elements for example), I won't have one query with 5 joins but more queries.. just wanted to know whether it is possible to make any preconfiguration in cases where I know exactly what I will need to return in response.. because even if I write a @Query above the method, it still doesn't seem to work the way I want to - it still fetches the other elements
@@rinorahmeti462 Hm. Strange. I'm not sure about your case. Maybe I don't understand it. Usually you should be able to use a JOIN fetch on the query for exactly what you need while leaving the others lazy.
@@laurspilca I had one ManyToMany relationship which was causing the issue perhaps.. if I am able to reproduce that case I will let you know because looks to be an interesting issue. Thanks!