aspose file tools*
The moose likes Object Relational Mapping and the fly likes  Regarding Hibernate Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark " Regarding Hibernate Query" Watch " Regarding Hibernate Query" New topic
Author

Regarding Hibernate Query

Edward Durai
Ranch Hand

Joined: Oct 09, 2004
Posts: 223
Hello Experts,

Thanks for Reading and Solving this Problem.

My question is:
for eg. get all the records from single Table:

hibernate query is : from <table name>

that's fine.

How can I connect two tables and get the results. Pls give me some idea.

Thanks
_________________
A.Edward Durai
"The things which are impossible with men are possible with God."


Thank You<br />Edward
devil devil
Greenhorn

Joined: Feb 13, 2006
Posts: 4
using joins. do it through SQl joins
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You should only be using SQL for the very specific occasions where HQL won't do. That being said HQL's join syntax is very simmilar to ANSI SQL. How you use the join the two tables depends on what sort of join is appropriate for your situation. Hibernate supports:
  • inner join
  • left|right outer join
  • full join

  • For example you could join two tables like this:

    The documentation covers joins. Its worth reading.

    You can also use "fetch" joins, which are useful because you can override the behaviour defined in your mapping files. For example:


    JavaRanch FAQ HowToAskQuestionsOnJavaRanch
    Edward Durai
    Ranch Hand

    Joined: Oct 09, 2004
    Posts: 223
    Pls give me that full line of joining two tables. or give me step by step procdure to connect two tables.

    It's more helpful to me.

    I eagerly waiting for your reply.
    Thanks
    Paul Sturrock
    Bartender

    Joined: Apr 14, 2004
    Posts: 10336

    I can't write the HQL for you without knowing what kind of join you are trying to perform. Even then, its not really in the spirit of these forums to write code for other people. Perhaps you could show us what you have tried and someone may correct any mistakes you've made?
    Edward Durai
    Ranch Hand

    Joined: Oct 09, 2004
    Posts: 223
    I have a two table.

    1. DEPTS
    2. EMPLOYEE

    BOTH FILES LOOK LIKE
    **********************************************************
    DEPTS.HBM.XML
    **********************************************************
    <class name="Depts" table="DEPTS">
    <id name="deptno" column="DEPTNO" type="short">
    <generator class="assigned"/>
    </id>

    <property name="deptname" column="DEPTNAME" type="string" />
    <property name="location" column="LOCATION" type="string" />

    <set name="employeeSet" inverse="true">
    <key column="DEPTNO"/>
    <one-to-many class="Employee"/>
    </set>
    </class>

    **********************************************************
    EMPLOYEE.HBM.XML
    **********************************************************
    <class name="Employee" table="EMPLOYEE">
    <id name="empno" column="EMPNO" type="short">
    <generator class="assigned"/>
    </id>

    <property name="empname" column="EMPNAME" type="string" />
    <property name="salary" column="SALARY" type="float" />

    <many-to-one name="depts" column="DEPTNO" class="Depts" />
    </class>
    **********************************************************

    I am using oracle 9i
    for ordinary query

    select * from employee e, depts d where d.deptno=e.deptno is working.
    but how to write in Hibernate?

    I wrote like this. from employee e, depts d where e.deptno = d.depts; Is it correct?

    And i also got some exception like depts.hbm.xml file not found error.
    Pls reply this. Thanks
    Edward Durai
    Ranch Hand

    Joined: Oct 09, 2004
    Posts: 223
    Hi,

    StringBuffer query = new StringBuffer(" from Employee as E,Depts as D Where D.deptno=E.depts");
    String strQuery = query.toString();
    Query q = session.createQuery( strQuery );
    System.out.println(q.toString());
    List students = q.list();

    The above query is working fine. i got a correct list.size();

    But how to get all the fields of two tables.

    i wrote like this

    Iterator itrStud = students.iterator();
    for( int index = 0 ; itrStud.hasNext() ; index++){
    Object s1 = (Object) itrStud.next();
    System.out.println("values :"+s1);
    }

    but i got values like

    values: [Ljava.lang.Object;@1a116c9
    values: from(depts)[Ljava.lang.Object;@1d1e730

    How can i get all this fields?

    Thanks
    Gerardo Tasistro
    Ranch Hand

    Joined: Feb 08, 2005
    Posts: 362
    I think you might be getting an Object[] (note array) with each .next(). Why don't you do a breakpoint in you code at


    System.out.println("values :"+s1);

    And check what the value of s1 is.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Regarding Hibernate Query