-->
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: Damned if you do, damned if you don't
PostPosted: Mon Jan 05, 2009 4:51 pm 
Newbie

Joined: Fri Jan 02, 2009 8:21 pm
Posts: 9
I'm teetering on the edge of "to hell with hibernate, I'm using jdbc."

What I'm trying to do is very simple: I have a one-to-many relationship. I need to use eager fetching. The trouble is that the ManyToOne column needs to be part of the primary key for the class in question. I've tried the following approaches:

1) Composite PK class - Result: infinite loop. Apparently a known hibernate bug.

2) Using IdClass to specify a composite primary key including the foreign key. This required adding a second non-updatable property for the foreign key - Result: Index out of bounds exception. Note that this approach was suggested as a workaround for the bug in approach 1.

3) Using IdClass to specify a composite primary key which includes the ManyToOne referenced entity.
a) with annotation for the ManyToOne relationship in the main class: result - "Could not determine type" for the column.
b) with the annotation for the ManyToOne relationship moved to the pk class referenced by IdClass: result - infnite loop again.

Is there ANY way to make this work?


EDIT: I apologize for the bitchy tone of the above - I was just frustrated.


Last edited by AtticusFynch on Tue Jan 06, 2009 2:19 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2009 4:22 am 
Newbie

Joined: Tue Jan 06, 2009 4:16 am
Posts: 3
I have a situation where a class has a primary key consisting of two foreign key references.

Heres the code for the class:
Code:
@Entity
@IdClass(MembershipPK.class)
public class Membership {
    protected Membership() {
        // Needed for JPA
    }
   
    public Membership(User user, Group group) {
        this.group = group;
        this.user = user;
    }
   
    @Id
    @ManyToOne
    Group group;
    @Id
    @ManyToOne
    User user;
    public Group getGroup() {
        return group;
    }
    public User getUser() {
        return user;
    }
   
   
}


And here is the code for the primary key:

Code:
@Embeddable
public class MembershipPK implements Serializable {
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(referencedColumnName="id", nullable=false, insertable=false, updatable=false)
    public Group group;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(referencedColumnName="id", nullable=false, insertable=false, updatable=false)
    public User user;
   
    public MembershipPK() {}
   
    public MembershipPK(User user, Group group) {
        this.user = user;
        this.group = group;
    }

    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof MembershipPK)) {
            return false;
        }
        MembershipPK other = (MembershipPK) obj;
        return group.getId() == other.group.getId() && user.getId() == other.user.getId();
    }

    @Override
    public int hashCode() {
        return  group.getId() * 3 + user.getId();
    }
   
   
}


This seems to be more or less what you want to do, so maybe you can use a similar approach.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2009 2:18 pm 
Newbie

Joined: Fri Jan 02, 2009 8:21 pm
Posts: 9
Thanks for the response.

Does that mapping work for cascades? I tried something along those lines, but setting updatable and insertable to false on the relationship declarations broke cascading inserts.

-Sean


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2009 5:08 pm 
Newbie

Joined: Tue Jan 06, 2009 4:16 am
Posts: 3
I haven't tried cascades, but i think the cascade declarations would have to be on the entity object, not the PK.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2009 5:21 pm 
Newbie

Joined: Fri Jan 02, 2009 8:21 pm
Posts: 9
ahj wrote:
I haven't tried cascades, but i think the cascade declarations would have to be on the entity object, not the PK.


I think it goes wherever the ManyToOne/OneToMany declarations go. So if you move those to the PK, the cascade declaration would live there.

I suppose you could use the standalone @Cascade annotation instead, if you want to separate them.

In any event, the infinite loop was happening on lookup, not persist, so it shouldn't matter.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2009 6:23 pm 
Newbie

Joined: Tue Jan 06, 2009 4:16 am
Posts: 3
I have the @ManyToOne declaration on both the PK and the entity. They map to the same columns. insertable and updateable are false on the PK, but not on the entity.


Top
 Profile  
 
 Post subject: Critieria query fails for composite primary key associations
PostPosted: Fri Jan 09, 2009 6:09 am 
Newbie

Joined: Thu Oct 25, 2007 9:39 pm
Posts: 7
I wrote a Crirteria query to fetch records and it throws SQLServerException: The multi-part identifier "user1_name" could not be bound.

The query is:

-------------------------------------------
Criteria criteria = getSession().createCriteria(Membership.class)
.createAlias("user", "user")
.add(Restrictions.eq("user.name", "kannan"));

criteria.list();

-------------------------------------------

I looked at the sql query and it had:

select this.user_id, this.group_id from Membership
inner join
User user where ...
inner join
Group where ....

and
user1.name = ?

clearly, user1 is not bound. it should have been user. can someone help me solve this problem?

thanks,
kannan


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.