GeeCON Prague 2014*
The moose likes Object Relational Mapping and the fly likes inner-join? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "inner-join?" Watch "inner-join?" New topic
Author

inner-join?

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

please anyone can tell me..how to join 2 table in hibernate??


example i want to execute this query
---------------------------------------------------
select * from table Employee e,Departments d where e.depid=d.depid
--------------------------------------------------


Thanks & Regards,
seetharaman.v
[ June 04, 2008: Message edited by: Bear Bibeault ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What you've got looks like it should work. What is going wrong?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Originally posted by Paul Sturrock:
What you've got looks like it should work. What is going wrong?


I think the query he posted was SQL.

select * from table Employee e,Departments d where e.depid=d.depid

So lets say you have an Employee and a Department object. What is the relationship. is it one to many, many to one, etc.

Lets just assume a Department has many Employees, but an Employee has only one Department. So Department has a Collection of Employees.

In Java the Ids should be called id, and not depid, and we are going to assume you mapped the "id" attribute to your actual PK fields in the database

so your HQL query would be

select d from Employee e, Department d where e.id=d.id

See not much different than you SQL, but in Objects. You only need to "select d" to get a List of Departments. I believe, but not positive, that the Employees Collections should also be populated.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks mark...

one thing...i still have doubt on mapping(one-to-many,many-to-one,etc).

i studied some url...but i can not understand..

please can you give me the good and easy url

Thanks & Regards,
seetharaman.v
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi,
first of all please use a meaningful subject line.Those question marks aren't good.

And I think there are two types of mappings
one to one - one employee works in only one department
one to many - one department has many employees.


Hope this helps .


SCJP, SCWCD.
|Asking Good Questions|
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks Amit
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Originally posted by Amit Ghorpade:
Hi,
first of all please use a meaningful subject line.Those question marks aren't good.

And I think there are two types of mappings
one to one - one employee works in only one department
one to many - one department has many employees.


Hope this helps .


Did you forget many to many mapping. An employee can work in different departments.


Groovy
Matt Gaunt
Ranch Hand

Joined: Sep 05, 2003
Posts: 34

And I think there are two types of mappings
one to one - one employee works in only one department
one to many - one department has many employees.


I think you will find the relationship for employee to department should be many-to-one. For many employees may work in one department. Your one-to-one relationship indicates that only one employee works in one department (in other words, each department has only one employee)
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Well post some more questions and win one of Cameron's books. And also you can see one-to-many mappings here for xml mapping

http://www.hibernate.org/hib_docs/v3/reference/en/html/associations.html#assoc-bidirectional-m21

Or, Cameron can post a link to his tutorial for ya too.

Actually, just find one of his posts and look in his signature at the bottom of his posts for the link.

Mark
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

oops I just missed that many to many mapping. Sorry for that
Reehan Lalkhanwar
Ranch Hand

Joined: Jun 16, 2007
Posts: 106
Why do we not have a many-to-one mapping?
Like many employees working under one department.
Just inquisitive...


Thank you,
Reehan
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Originally posted by Reehan Lalkhan:
Why do we not have a many-to-one mapping?
Like many employees working under one department.
Just inquisitive...


Isn't same as 1-to-n mapping
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
Originally posted by Mark Spritzler:

...
In Java the Ids should be called id, and not depid, ...


Why not???
I believe that it's more a convention than a language requirement.
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3220
Probably you need to get The Hibernate Made Easy


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Reehan Lalkhanwar
Ranch Hand

Joined: Jun 16, 2007
Posts: 106
I hope the Ranchers read this and include my name in the prize winners list.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by Reehan Lalkhan:
I hope the Ranchers read this and include my name in the prize winners list.


Unfortunately, posts that are not on topic will not be included. If you want your post to be considered for the draw, you need to ask a genuine question or answer a question for another rancher.

See here for more info.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Originally posted by Rodrigo Lopes:


Why not???
I believe that it's more a convention than a language requirement.


Most conventions in databases have ids as tablename_id

And Hibernate by default will take the table/object name and concatenate it with the "id" attribute and use that as the PK.

So if I have a table

ADDRESS
address_id Numeric PK
street String
...

And I have a Java object

public class Address {

private long id;
private String street;

...

}

Mapping is real easy, I let Hibernate or most ORMs use their defaults and I am good.

Mark
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
Ok, Mark, I got your point.
But when you wrote that "In Java the Ids should be called id, and not depid", it sounded that it's mandatory, while it's not.
 
GeeCON Prague 2014
 
subject: inner-join?