• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Hibernate Query

 
Edward Durai
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
devil devil
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using joins. do it through SQl joins
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
     
    Edward Durai
    Ranch Hand
    Posts: 223
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 10336
    Eclipse IDE Hibernate Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 223
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 223
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 362
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic