Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

hibernate joining two tables

 
Jonathan LeeCh
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
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
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
Saloon Keeper
Posts: 4968
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.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!