Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner-join?

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you've got looks like it should work. What is going wrong?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Amit
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Matt Gaunt
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops I just missed that many to many mapping. Sorry for that
 
Reehan Lalkhanwar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do we not have a many-to-one mapping?
Like many employees working under one department.
Just inquisitive...
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably you need to get The Hibernate Made Easy
 
Reehan Lalkhanwar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope the Ranchers read this and include my name in the prize winners list.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic