-->
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.  [ 3 posts ] 
Author Message
 Post subject: query parameter mapping with persistence
PostPosted: Wed Jun 25, 2008 4:55 pm 
Newbie

Joined: Wed Jun 25, 2008 3:34 pm
Posts: 2
I have 3 classes plus id classes (weak example):

Group (table group...columns: group_id)

Person (table person...columns: group_id, person_id)
PersonId (personId, groupId)

Address (table address...columns: group_id, person_id, address_id)
AddressId (personId, groupId)

there are one to many Person for the Group class, and one to many Address for the Person class.

Group
Code:
    [color=blue]private Set<Person> persons= new HashSet<Person>( 0 );
    ...
    @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "group")
    public Set<Person> getPersons()
    {
        return this.persons;
    }[/color]


Person
Code:
    [color=blue]
    Group group;

    private Set<Address> addresses= new HashSet<Address>( 0 );
    ...

    @ManyToOne(cascade = {}, fetch = FetchType.LAZY)
    @JoinColumn(name = "GROUP_ID", ...)
    public Group getGroup()
    {
        return this.group;
    }
    ...
    @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "person")
    public Set<Address> getAddress()
    {
        return this.address;
    }[/color]


PersonId
Code:
    [color=blue]Integer groupId;
    Integer personId;

    @Column(name = "GROUP_ID", ...)
    public Integer getGroupId()
    {
        return this.groupId;
    }

    @Column(name = "PERSON_ID", ...)
    public Integer getPersonId()
    {
        return this.personId;
    }[/color]



Address
Code:
    [color=blue]
    Person person;
    ...

    @ManyToOne(cascade = {}, fetch = FetchType.LAZY)
    @JoinColumns( {
                   @JoinColumn(name = "GROUP_ID",...),
                   @JoinColumn(name = "PERSON_ID",...) })
    public TPal getPerson()
    {
        return this.person;
    }[/color]


AddressId
Code:
    [color=blue]Integer groupId;
    Integer personId;
    Integer addressId;

    @Column(name = "GROUP_ID", ...)
    public Integer getGroupId()
    {
        return this.groupId;
    }

    @Column(name = "PERSON_ID", ...)
    public Integer getPersonId()
    {
        return this.personId;
    }

    @Column(name = "ADDRESS_ID", ...)
    public Integer getAddressId()
    {
        return this.addressId;
    }[/color]



I didn't show the obvious like @EmbeddedId, and some of the @AttributeOverride entries...

I have retrieved the object Group from the db.

Here is the following code to get down to the Address object(s)

Code:
   [color=blue] Group group = ....

    Set<Person> peeps = group.getPersons();

    //-- fetch data from db is done here
    Iterator<Person> pi = peeps.iterator();

    while( pi.hasNext() )
    {
        Person person = pi.next();

        Set<Address> addresses = person.getAddresses();

        //-- Query returns nothing since the mapping key->params is
        //-- totally wrong
        Iterator<Address> ai = addresses.iterator();
        ...
    }[/color]


so...basically....this is my query log output

where
address_.GROUP_ID=?
and address_.PERSON_ID=?

DEBUG NullableType - binding '11111' to parameter: 1

DEBUG NullableType - binding '99999' to parameter: 2



11111 is the PERSON_ID
and
99999 is the GROUP_ID

the key values in the peeps Set is in the order of person/group values. Debugging into hibernate code, I cannot see that it is doing any kind of mapping. It is simply doing a for loop on the key sets and adding that to the parameter list. It doesn't really appear to be mapping the key to the column at all.......


HELP!


Top
 Profile  
 
 Post subject: Re: query parameter mapping with persistence
PostPosted: Thu Jun 26, 2008 8:11 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
SeanP wrote:
Address
Code:
    [color=blue]
    Person person;
    ...

    @ManyToOne(cascade = {}, fetch = FetchType.LAZY)
    @JoinColumns( {
                   @JoinColumn(name = "GROUP_ID",...),
                   @JoinColumn(name = "PERSON_ID",...) })
    public TPal getPerson()
    {
        return this.person;
    }[/color]



Do you specify referencedColumnName as part of your ... in JoinColumn. If not the order IS important and I would try switching the two JoinColumn annotations. Or you just specify referencedColumnName as well.

Hope this helps,
--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 26, 2008 10:31 am 
Newbie

Joined: Wed Jun 25, 2008 3:34 pm
Posts: 2
Thank you very much..it was that simple :)

I had to open a ticket with MyEclipse because their rev eng isn't setting a referencedColumnName entry - which is why I was at such a loss.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.