File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes how to write a simple table join in hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "how to write a simple table join in hibernate" Watch "how to write a simple table join in hibernate" New topic

how to write a simple table join in hibernate

hitendra sunkara
Ranch Hand

Joined: Jul 25, 2013
Posts: 43
Hi ,iam new to hibernate,iam feeling some complexity regarding table joins using hibernate:

i have the following tables:

create table tasktype (
tasktype_id INT NOT NULL auto_increment,
tasktypedescription VARCHAR(20) default NULL,
tasktypecode VARCHAR(20) default NULL,
PRIMARY KEY (tasktype_id)

create table tasktyperelation (
task_id INT ,
tasktype_id int ,
FOREIGN KEY (tasktype_id) REFERENCES tasktype(tasktype_id)

before i have used hdbchelper class and retrived results with the help of this query:

select tasktype.tasktype_id,tasktypedescription,tasktypecode from " +
"tasktyperelation , tasktype where task_id=? and tasktyperelation.tasktype_id = tasktype.tasktype_id

But in hibernate we need to use some manytoone or one to many relationsShips to achive the same result..

So can any one tell me about how to implement above one using hibernate?

hitendra sunkara
Ranch Hand

Joined: Jul 25, 2013
Posts: 43
i have written code for hbm.xml files and configuration files:
jim mcnamara
Ranch Hand

Joined: Jun 13, 2007
Posts: 70
Hi Hitendra-

I put up some sample code I fooled with a little tonight. I did a native SQL join. The code is on my site. I think this way is the quickest. All you need are 2 pojos, the mappings, and the config file and if you have db experience, it is really easy to write the sql.

The joins you are talking about may be the collection mappings. I also have an example of that on my site, my first "one to many" mapping. If you need finer grain control than mapping a collection, like a set, you can use HQL or write the native query.

You can email me if you like, and I will try to send my config files or whatever you need to get going. I could probably add a link for project files for download.

My site is at 3rdshiftcoder

My email is

I am just a keyboard specialist that likes to code as a hobby, and I have learned to keep my attitude in check. Helen Borrie, who writes Firebird books, described herself, among other things, as the bottle washer for the Firebird Foundation. That teaches me a thing or two about not worrying about a title.

I like the Hibernate without JPA. I don't have a manager worrying over my shoulder about switching ORM provider, so thus, no need to worry about the standard JPA.

I might be able to help with m2 plug-in with Maven if you need help. I am learning that with a little success with the help of the forum. I only know a subset of it though.

I don't know if you use Eclipse.

Well, whatever your decision, that is cool. I am just learning too.

Having said all that, I find the tutorialspoint site for Hibernate is really good. It is maybe all you need anyway. Also, the new Hibernate book comes out sometime around March at

good luck,
jim (hobby coder)

Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 502

Hi hitendra sunkara,

Your problem is actually involving in mapping a one-to-one association. Basically, a one-to-one is a special case of many-to-one association with unique of the foreign key=true.
There's a lot of tutorials about such topic, and here is one of them:

Hibernate One-to-One With Foreign Key XML Mapping Example

PM Me If You Want to get Urgent Help on Java Programming
hitendra sunkara
Ranch Hand

Joined: Jul 25, 2013
Posts: 43

Small change in the Question:

I have three tables basically,task,taskType and taskType Relation:

task_id integer NOT NULL ,
taskname character varying(30),
taskdescription character varying(400),
startdate date,
enddate date,
estimatedeffort numeric(6,2),
techowner integer,
PRIMARY KEY (task_id),

CREATE TABLE tasktype (
tasktype_id integer NOT NULL,
tasktypedescription character varying(100),
tasktypecode character varying(10),
PRIMARY KEY (tasktype_id)
CREATE TABLE tasktyperelation (
task_id integer,
tasktype_id integer,
CONSTRAINT FOREIGN KEY (tasktype_id) REFERENCES tasktype (tasktype_id)

i want to perform this task in hibernate Annotations joins.

Assume we have 3 tasks:
1.craete site
2.add buttons to site for that site

in tasktypes

in taskTypeRelation we will have:

taskId taskTypeId



i have sucedded in doing this by using native SqlQuery:like this:

But i need to implement the same feature in (Hibernate Joins using annotations )...So please can you help me in this...

I agree. Here's the link:
subject: how to write a simple table join in hibernate
jQuery in Action, 3rd edition