Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Three tables query

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I need to query three tables at the same time. Could anybody help me. User has one-to-many relation with lectures and lectures has one to one with Professor.


How can I query all three in one query an display data on one jsp form. Please help me.

Thanks in advance,
Arul
 
author and cow tipper
Posts: 5000
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just load the user. Then, when you invoke user.getLectures or user.getClassrooms, all of the associated data will automatically be loaded.

That's how ORM works! You don't query the database. You just grab your object. And when you call various Java methods on your object, the underlying database is queried, and you get your data. Just make sure you're Hibernate Session is active while the call to the getter happens, otherwise you'll get a LazyLoading exception.

-Cameron McKenzie
 
arul meh
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cameron for such a quick reply. I really appreciate this.
 
arul meh
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cameron,

I need to display User - Lecture - Professor all on one page in the form of report. I can pass only one user.getLectureDetails or User.get ProfessorDetails in the page. How o I pass both and start displaying. I am lost somewhere.

Also, I need to define two methods getLectureDetails an get ProfessorDetails in User ? Remember, User has direct relation with Lecture and not Professor.


Thanks,
Arul
 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5000
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really, you first need to do some mappings. Check out my signature links for some tutorials on doing one to many mappings and many to many mappings. That's the best place to start! Just get the mappings working, then the display stuff will come easlity.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Follow the steps below

1. Create the necessary hbm.xml file for your table Users, Lecturers and Professor.
2. Ensure that in your table hbm.xml file there is <one-to-many> relationship to the Lecturer table.
Similarly make sure there is one-to-one relationship between Lecturers and Professor

3. Finally, all you need is a method called getUserDetails(). In there all you will need to do is query User

session.createCriteria(Users.class).list();

4. In yous display screen you can get the lecturers details like users.getLecturers() and professor like users.getLecturers().getProfessor()

Hope that this helps to lead you to the correct path
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic