File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes how can i join two different table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "how can i join two different table" Watch "how can i join two different table" New topic
Author

how can i join two different table

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

this is my first hibernate program. and i read some of the tutorial. but still i struggled

i have table A which has two column such as id and name here id is an primary key (and)

table B which has two column such as id and email here id is an primary key

now i need to fetch A.name and B.email from tables A and B based on id . i tried but failed to obtain the data .

please anyone can tell me the way to achieve it ?



Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

So is there is not relationship between these tables? Or is it one to one?

Can you post what you tried?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

well,relation is one-to-one.

Here is my UserName.hbm.xml[For database table A]



and UserName.java[javabean]




and UserMail.hbm.xml[For database table B]



and UserMail.java[java bean]



and finally hibernate.cfg.xml



and i create a java class to test the application

Execute.java [assume it HibernateUtils class is fine]




Now it is working fine for retrieve the values from table A only.. now please see my question in my first post[how to inner join and get the datas]

Sorry for the long post
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

OK, so you need to define this association in your mapping file and you do this with the one-to-one mapping. Have a read of that link, it includes a few examples.

(One question - why are you using Hibernate 2? Its a bit old. )
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks a lot .

Paul Sturrock wrote:(One question - why are you using Hibernate 2? Its a bit old. )


Yes . really i need to switch to Hibernate 3
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

in Hibernate Query , i need to fetch the records from two table. example,

select a.name,b.email from A as a,B as b where a.id=b.id


how to execute this query and how to get the values from the tables .



Problem here UserName and ,UserMail are created and i am only casting in to the UserName . so i am getting classcastException

any help ?
Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
your "alist" will be a List of Object[]
So, it should be List<Object[]>
and when you iterate over this list, in each iteration, you get an Object[], cast the first element of the array to type of "a.name" and second element to "b.email" and create the required objects.


Rahul Babbar
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Its worth having a read through the documentation. It includes a tutorial too, so you'll probably save yourself a good deal of hassle.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks Paul and Rahul
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can i join two different table
 
Similar Threads
retriving two primary keys
SQL Query for two tables joining
SQL Query for two tables joining
hibernate mapping
Hibernate many-to-one composite key as foreign key