• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate & JPA Training question

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello and welcome to the ranch
I am really exited about this course beacause i would really like to learn hibernate and spring soon
I have experiece with jdbc and sql and here is my question.
I have never used hibernate but most of time we're using data base connection for inserting records and getting it later somewhere, BUT does hibernate allows to write difficult and long querys to get "reports" ?
Let's say we have to do a query with many Withs, many many joins etc. I think creating query should be done in sql developer, because we want to see if the data that we are getting is right- tell me if i am wrong. But how is it done with hibernate?
What about those long long querys, that are not that simple like insert or select with 2 joins?
And about that course - do you cover how it should be done (those long querys) , or some best practices what is right and what to avoid ?
 
Virtual Pair Programmers Rep
Posts: 29
7
Hibernate Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krystian,

A good question for me to start on - and I reckon this is probably one of the most frequently asked questions!

It's really important to remember that Hibernate's (and JPA, very closely related) prime job is as an "Object Relational Mapper". Briefly, it's trying to make the gap between the OO world and the relational world easier to deal with.

So, for me at least, Hibernate's "sweet spot" is:

1) Grab an object, or a small(*) group of objects, using a hibernate query
2) Now that you have the object(s), forget about the database and manipulate the object(s) using regular OO (ie calling business methods on the objects that change the state of the objects)
3) When your business process is finished, tell Hibernate you're finished (this is actually done with a "commit"), and then Hibernate will automatically work out which objects have changed, and what UPDATES need to be issued to sync the database state with the object state.

(*) "small" here is subjective, you certainly don't want to be retrieving the whole database into memory!

It's not a reporting tool or intended to help with complex queries - if you have a complex SQL query, the type that you've just described, then I would absolutely use SQL for that. (Actually, truthfully, I would get my DBA to write the long long query, I'm hopeless at writing performant SQL!)

Related to this - Hibernate isn't intended for bulk operations. What can be achieved in a single UPDATE statement could possibly be very bad for performance if you try to do it the "OO way".

Quick example of this "anti pattern" (pseudocoded, it won't compile!)



- this would result in an update statement for every single credit card in the database! Of course a simple update is fine for this.

It's really important to remember that using Hibernate does not in any way block you from using regular SQL as well (Hibernate does allow the execution of "native" SQL queries as a kind of convenience, but you can call JDBC as well). It's crucial to use Hibernate for what its good at - working on small groups of objects where you want to focus on objects rather than tables and joins.

We have a chapter on the course covering this, I'm going to arrange for a preview to be available of the chapter just for JavaRanch - it will take a while to get it ready but I'll reply back here when it's ready.

I hope that helps, do keep asking if you need to know more.
 
Krystian Kowalski
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answer, you made it more cleaner for me now
It looks way batter than writing all this things in Strings with jdbc.
Just 2 more questions :P :
1. When creating database, is there any "hibernate" way to do it automaticly ? Or we need to create database and after that create objects, map fields with columns etc ?
2. let's say i've made a table and i have an mapped object. I am doing some inserts, updates and everything is fine and after 1 month i've made a a lot of "querys" using that object in my project. I conclude that i have to add a column. Do i need to get back to all places in my project and use that column and insert something, at least null? Or null is automaticly ? Or deleting column , is there any problem with it ? Just wondering how hard is to keep mapped my database with my hibernate objects. Because we all know how it is in real life that not everything is going like we assumed, and some changes need to be done
 
Richard Chesterwood
Virtual Pair Programmers Rep
Posts: 29
7
Hibernate Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Krystian Kowalski wrote:Thanks for your answer, you made it more cleaner for me now
It looks way batter than writing all this things in Strings with jdbc.
Just 2 more questions :P :
1. When creating database, is there any "hibernate" way to do it automaticly ? Or we need to create database and after that create objects, map fields with columns etc ?
2. let's say i've made a table and i have an mapped object. I am doing some inserts, updates and everything is fine and after 1 month i've made a a lot of "querys" using that object in my project. I conclude that i have to add a column. Do i need to get back to all places in my project and use that column and insert something, at least null? Or null is automaticly ? Or deleting column , is there any problem with it ? Just wondering how hard is to keep mapped my database with my hibernate objects. Because we all know how it is in real life that not everything is going like we assumed, and some changes need to be done



Hi again,

1: yes, you can switch on "auto DDL generation" - we do that on the course to save time, and of course this is useful for some projects that don't need a fine tuned schema or DBA (eg smallish web applications). Of course, this can also be switched off.

2: the column would be automatically added to your schema and the table would default to null (unless you do a tweak or add the column manually). You would then add an attribute into whichever class you're mapping to - and now automatically all of the hibernate generated queries, updates and inserts will include that column. Pretty much the same thing in reverse if you remove. If you delete a column, then you would remove that attribute from the mapped class, and again the inserts etc would not include that column (Note: the auto DDL doesn't delete columns automatically, you would need to do this manually in the database).

I hope that helps!

Richard.
 
Richard Chesterwood
Virtual Pair Programmers Rep
Posts: 29
7
Hibernate Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We've uploaded a chapter from the course to youtube as a private unlisted video for JavaRanch (I hope it's ok to link here, if not let me know and I'll take the link down!)



This chapter is late in the course, so don't be put off if it feels a bit advanced - by this point in the course you'll be well advanced yourself. The first 30 minutes or so are about Batch Fetching which is a bit deep (including the infamous n+1 selects problem), but towards the end we talk about the kinds of things that many have asked about - such as when NOT to use hibernate, and what the alternatives are.

I hope it's useful - I don't know how long the link will be live, but at least for this week.

Richard.
 
Krystian Kowalski
Ranch Hand
Posts: 83
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I hope i will have pleasure to learn hibernate from your course
reply
    Bookmark Topic Watch Topic
  • New Topic