-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Getting parent object from a child
PostPosted: Mon Jun 12, 2006 10:05 pm 
Newbie

Joined: Fri May 20, 2005 6:05 am
Posts: 8
I want to query the child object to get all the parents that match them. Hibernate is not generating the join so the sub object query params fails.

Hibernate version: 3.1

Mapping documents:
Code:
<hibernate-mapping>
  <class name="Child" table="CHILD">
    <composite-id>
      <key-many-to-one name="parent" class="Parent">
        <column name="PARENT_ID" />
      </key-many-to-one>
      <key-property name="value" column="value"/>
    </composite-id>
  </class>
</hibernate-mapping>   

<hibernate-mapping>
  <class name="Parent" table="PARENT">
    <id name="id" column="PARENT_ID">
      <generator class="sequence">
        <param name="sequence">P_SEQ</param>
      </generator>
    </id>
  </class>
  <set
    name="children"
    lazy="true"
    inverse="true"
    cascade="all,delete-orphan"
  >
    <key column="PARENT_ID"/>
    <one-to-many class="Child"/>
  </set>
  <property name="sample" column="SAMPLE"/>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
Session session = HibernateUtil.currentSession();
Criteria criteria = session.createCriteria(Child.class);
criteria.createCriteria("parent")
  .add(Restrictions.eq("sample",
      "some value"));


Name and version of the database you are using: Oracle 10

The generated SQL (show_sql=true):
select this_.PARENT_ID as PARENT1_37_0_, this_.VALUE as VALUE2_37_0_ from CHILD this_ where PARENT1_.SAMPLE=?

(NOTE this was modified to simplify the problem, as the real query is VERY long

ERRORS:
"PARENT1_"."SAMPLE": invalid identifier

As you can see there is no reference to the parent table in the query. I have tried many different ways to get this to work with no success.
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 7:53 am 
Newbie

Joined: Tue Jun 13, 2006 5:59 am
Posts: 4
Try this:

Code:
Session session = HibernateUtil.currentSession();
Criteria criteria = session.createCriteria(Child.class);
criteria.createAlias("parent", "p")
  .add(Restrictions.eq("p.sample", "some value"));


This would create an alias for "parent", make the joins and return every instance of child where it's parent's "sample" is "some value".

Also you can use implicit joins ('though the first example is more declarative):
Code:
Session session = HibernateUtil.currentSession();
Criteria criteria = session.createCriteria(Child.class);
  .add(Restrictions.eq("parent.sample", "some value"));


Anyway, could you post the entire generated SQL Query?

Guido Scalise


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 9:28 pm 
Newbie

Joined: Fri May 20, 2005 6:05 am
Posts: 8
The first suggestion causes the same output. The second suggestion does the following.

Code:
could not resolve property: sample of: Parent



I will try and just build a simple example of this as I can not show my code due to ownership issues.

What it looks like is happening is that it can not detect the relationship so the query does not add both tables to the from clause.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 14, 2006 6:13 am 
Newbie

Joined: Tue Jun 13, 2006 5:59 am
Posts: 4
jtrollin wrote:
The first suggestion causes the same output. The second suggestion does the following.

Code:
could not resolve property: sample of: Parent



I will try and just build a simple example of this as I can not show my code due to ownership issues.

What it looks like is happening is that it can not detect the relationship so the query does not add both tables to the from clause.


Then there's something wrong with Parent's mapping. Check more exhaustively your code and mapping files.

Are you able to persist/retrieve a parent alone? If you are, does the "sample" field get persisted correctly?

You can also try enabling debug/trace logging so you get more info.

Kind Regards,
Guido Scalise


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 14, 2006 6:22 am 
Newbie

Joined: Fri May 20, 2005 6:05 am
Posts: 8
The mappings seem to work fine, infact if I dont try and add query params to the parent I get back all the child records and have no problem in the code drilling up into the parent class. I will take some time to day to simplify the problem and see if it is reproducible at that level then send the code with the debug logs.

Thanks for the help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 14, 2006 6:35 am 
Newbie

Joined: Tue Jun 13, 2006 5:59 am
Posts: 4
jtrollin wrote:
The mappings seem to work fine, infact if I dont try and add query params to the parent I get back all the child records and have no problem in the code drilling up into the parent class. I will take some time to day to simplify the problem and see if it is reproducible at that level then send the code with the debug logs.

Thanks for the help.


I meant the mapping of the "sample" property.

Check what happens for example if you do a query like:
Code:
Query q = session.createQuery("from Parent where sample=:val");
q.setParameter("val", "some value");
List<Parent> parents = (List<Parent>)q.list();


Does it run ok?

I think the problem is in your class "sample" field (check it's accessors) or in your Parent.hbm.xml mapping.

Guido


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 14, 2006 7:00 am 
Newbie

Joined: Fri May 20, 2005 6:05 am
Posts: 8
Ok,

I think I found out the problem. If I take the parent out of the composite-id then all works fine. But with the parent as part of the id for the child the query is not built right.

Here is the query with the parent in the key
Code:
<class name="test.obj.Child" table="child">
    <composite-id>
      <key-many-to-one name="parent" class="test.obj.Parent">
        <column name="parent_id" />
      </key-many-to-one>
      <key-property name="childVal1" column="child_val1"/>
    </composite-id>

    <property
        name="childVal2"
        type="java.lang.String"
        column="child_val2"
    />
  </class>

<class name="test.obj.Parent" table="parent">
    <id name="id" type="java.lang.Long" column="parent_id">
        <generator class="sequence" >
            <param name="sequence">parent_seq</param>
        </generator>
    </id>
    <property name="value1" column="value1"
    />
    <property name="value2" column="value2"
    />
    <set name="children" lazy="true" inverse="true" cascade="all,delete-orphan">
      <key>
          <column name="parent_id" />
      </key>
      <one-to-many class="test.obj.Child" />
    </set>
  </class>


Code:
Hibernate: select this_.parent_id as parent1_1_0_, this_.child_val1 as child2_1_0_, this_.child_val2 as child3_1_0_ from child this_ where p1_.value1=?


Here is without

Code:
<class name="test.obj.Child" table="child">
    <id name="id" type="java.lang.Long" column="child_id">
        <generator class="sequence" >
            <param name="sequence">child_seq</param>
        </generator>
    </id>
    <property name="childVal1" column="child_val1" />
    <property name="childVal2" column="child_val2" />
    <many-to-one name="parent" class="test.obj.Parent" not-null="true" >
      <column name="parent_id" />
    </many-to-one>
  </class>


The parent does not change

Code:
Hibernate: select this_.child_id as child1_1_1_, this_.child_val1 as child2_1_1_, this_.child_val2 as child3_1_1_, this_.parent_id as parent4_1_1_, p1_.parent_id as parent1_0_0_, p1_.value1 as value2_0_0_, p1_.value2 as value3_0_0_ from child this_, parent p1_ where this_.parent_id=p1_.parent_id and p1_.value1=?


As you can see as long as parent is not part of the key it builds the join fine.

Here is the trace / debug data

Code:
2006-06-14 06:59:05,189 [main] (AbstractBatcher.java) DEBUG - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2006-06-14 06:59:05,189 [main] (ConnectionManager.java) DEBUG - opening JDBC connection
2006-06-14 06:59:05,189 [main] (DriverManagerConnectionProvider.java) DEBUG - total checked-out connections: 0
2006-06-14 06:59:05,189 [main] (DriverManagerConnectionProvider.java) DEBUG - using pooled JDBC connection, pool size: 0
2006-06-14 06:59:05,189 [main] (AbstractBatcher.java) DEBUG - select this_.parent_id as parent1_1_0_, this_.child_val1 as child2_1_0_, this_.child_val2 as child3_1_0_ from child this_ where p1_.value1=?
Hibernate: select this_.parent_id as parent1_1_0_, this_.child_val1 as child2_1_0_, this_.child_val2 as child3_1_0_ from child this_ where p1_.value1=?
2006-06-14 06:59:05,189 [main] (AbstractBatcher.java) DEBUG - preparing statement
2006-06-14 06:59:05,229 [main] (NullableType.java) DEBUG - binding 'one' to parameter: 1
2006-06-14 06:59:05,229 [main] (AbstractBatcher.java) DEBUG - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2006-06-14 06:59:05,229 [main] (AbstractBatcher.java) DEBUG - closing statement
2006-06-14 06:59:05,239 [main] (JDBCExceptionReporter.java) DEBUG - could not execute query [select this_.parent_id as parent1_1_0_, this_.child_val1 as child2_1_0_, this_.child_val2 as child3_1_0_ from child this_ where p1_.value1=?]
java.sql.SQLException: ORA-00904: "P1_"."VALUE1": invalid identifier


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.