This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

hibernate joining two tables

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am new to hibernate and maybe this had been answered before, but let me ask it again.

I am trying to return a join result between two tables and i read that by using the createAlias() i could associate the two tables together.

My two tables are Document and DocumentEntity and i am trying to use the entity_id(which is the primary key for DocumentEntity) and
using the doc_id from the DocumentEntity table to associate with the Document table to get me the doc_type from the Document table

Criteria crit = session.createCriteria(DocumentEntity.class)
.createAlias("Document", "d")
.add(Expression.eq("entity_id", entity_id)
.setProjection(Projections.projectionList()
.add(Projections.property("title"))
.add(Projections.property("d.doc_type"))
);

Is there anything I am missing out on, like whether do i need to specify the document.class anywhere so that the alias can be linked up

Replies are much appreciated thanks
 
author and cow tipper
Posts: 5000
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a foreign key relationship between the two tables?

-Cameron McKenzie
 
Jonathan LeeCh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh yes, sorry for missing that out, the two tables are related by the field doc_id.
 
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
Just get the document object, and then call getDocumentEntity. It gets returned automatically if the foreign key relationship is mapped.

-Cameron McKenzie
 
Jonathan LeeCh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Cameron for the reply, ok what if i want to get projections from two totally unrelated tables then?

i want to know how exactly does the createAlias() field helps in doing this.
 
BWA HA HA HA HA HA HA! Tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!