facebook

@namedquery using fields from one-to-many child

  1. MyEclipse IDE
  2.  > 
  3. Spring Development
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #320823 Reply

    urklnme
    Member

    Is it possible in Hibernate/scaffolding/JPA/Spring MVC to use the child fields in a one-to-many/many-to-one relationship in the where clause of a named query?

    i have tried many variations of:

    select parent ParentClass as parent, child ChildCollection as child
    where child.dateOfBirth = ?1

    and also

    select parent ParentClass as parent, child parent.Child as child
    where child.dateOfBirth = ?1

    I want to retrieve from the database (oracle) all the parent records (and their associated children records) with children who were born on the date specified.

    note: the foreign key (parent_id) that appears (parent_id) in the database table for the child does not appear in the scaffolded domain bean of the child, so I can not even write my own queries.

    Solutions?

    #320858 Reply

    cconway
    Member

    Have you tried the JOIN keyword? I think it would go something like this:

    SELECT parent FROM ParentClass parent JOIN parent.children child WHERE child.dateOfBirth = ?1

    where “children” is the relationship name.

    #320862 Reply

    urklnme
    Member

    That is awesome! It definitely makes things easier.

    I have read that there is no guarantee of unique results when querying on a one-to-many, so I used a linkedhashset for the results of the query.

    In the linkedhashset for the results, I am seeing the same parent id twice, once with a child that has a matching date of birth and once with a child that does not match date of birth)

    Is there a way to prevent this or a workaround that will not be a performance hit.

    #320866 Reply

    cconway
    Member

    Ok, I think that makes sense from a JPQL perspective. I’d try adding a DISTINCT keyword as shown in the example under section 10.2.2.2 in this document.

    http://download.oracle.com/docs/cd/E16764_01/apirefs.1111/e13046/ejb3_langref.html#ejb3_langref_schemanaming

    “Distinct” is described in section 10.2.7 of that same reference document.

    HTH

    #320990 Reply

    urklnme
    Member

    Thank you very much. Thanks for helping out a newbie.

    It really has made a difference for me.

    #320992 Reply

    cconway
    Member

    I’m happy I could help!

Viewing 6 posts - 1 through 6 (of 6 total)
Reply To: @namedquery using fields from one-to-many child

You must be logged in to post in the forum log in