Win a copy of Java Challengers this week in the Java in General forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Hibernate running multiple queries on load

 
Ranch Hand
Posts: 149
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All, hope you are doing good.

I have an application where a Game is Class which contains some properties along with a set called daysPlayed; and a list called bonusInfo..  which are from different tables..

Here is the piece of code..




and here is the snippet of XML for mapping..




When I load all the games using...



For bonus and days off Hibernate running multiple queries like two queries per Game .. Can't I load all the data at once.. like using Joins?


Regards,
Bala.
 
Saloon Keeper
Posts: 23540
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Balus!

Yes, you can do that easily, but before I start, I'd like to note that you are using very antiquated Hibernate. These days, the preferred version of Hibernate is Hibernate JPA - which uses an EntityManager rather than "session", and uses Java class annotations in preference to XML.

But, if you define two Hibernate/JPA Entity objects such that one object has a ManyToOne relationship to the other, and you have defined the relationship to be an "eager fetch", then a query returning a parent object will see that object's relation set contain all of its children.

In fact, eager fetching is so effective that often we have to override it and do lazy fetching, lest the entire database get sucked into memory!
 
Bala Tilak
Ranch Hand
Posts: 149
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim Holloway,

Thanks for the reply. Yes its a big legacy code.. but I think EntityManager is just a wrapper around session if I am not wrong.

And I prefer XML to keep my java classes real POJOs without any annotations.

So using XML , is there any possibility that I can fetch the one to many in one query, I can't go with lazy load as I need these values to be in memory...
 
Tim Holloway
Saloon Keeper
Posts: 23540
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would imagine that by now it's more likely that session is a wrap for EntityManager. Hibernate JPA has been in production for many years now and the Hibernate.org crew were among the major contributors to how JPA was defined and designed.

JPA is also officially blessed as part of the JEE standard. Legacy Hibernate is not. That means that ONLY Hibernate.org (IBM/Red Hat) supports legacy Hibernate and they can choose to drop it any time they want. JPA, being a standard is different. I had a project using Apache OpenJPA and their current version lacked a necessary feature. I made a couple of changes to my project build and had it running without code modification in under 15 minutes. Granted, if you're looking for a proprietary vendor and want long-term support, IBM is a leader, but consider also what they just did with CentOS 8.

Adding annotations to a JavaBean does not make it a non-POJO, In fact, one reason annotations were developed was so that POJOs could be better supported by external systems such as Hibernate JPA, Spring Data, JavaServer Faces, CDI and so forth. And, as I recall, even legacy Hibernate. The reason why XML still functions is that the annotation serve to provide default characteristics, but XML overrides them in a way that means you don't have to re-compile the POJO. Keeping Java source AND XML config files means that every such item requires double-maintenance and doubles the number of source files in a project. It's extra work and and a possible source for error. So XML is greatly discouraged these days except for exceptional situations.

As I said before, if you want everything in memory from a single query, use eager fetching. It's the default, not lazy fetching.
 
Bala Tilak
Ranch Hand
Posts: 149
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. So my question is for eager fetching...Hibernate running multiple queries...Like three queries for each game loading...One for game and one for days and another for bonus info. It's taking time. Is there a way we can load all at once (including sub entities) using joins ??
 
Tim Holloway
Saloon Keeper
Posts: 23540
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The whole point of ORM systems like Hibernate is not to use explicit joins. SQL joins are a royal   and are one of the biggest causes of confusion and frustration in DBMS queries.

Note that I used the worda "explicit join" here. Hibernate does do joins but it does so in a way that you don't have to.

ORM joins are accomplished using the @ManyToOne, @OneToMany and @ManyToMany annotations. When an Entity class - let's say "Game" - is defined as containing a Collection property (such as a List) of other Entities - such as "Day"s, and that collection property is annotated with the @OneToMany construct, that tells Hibernate to do the join. As I've said before, if you use Eager Fetch, then retrieving a Game via a JPQL Query will automatically retrieve the Days associated with that Game. Nothing more needs to be done. One query pulls it all into memory . both Game and Days and that's it.
 
Can you smell this for me? I think this tiny ad smells like blueberry pie!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic