• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic