-->
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: One-to-many with a composite key
PostPosted: Tue Oct 27, 2009 7:38 am 
Newbie

Joined: Mon May 01, 2006 6:14 am
Posts: 6
I've a class A with a one-to-many relationship with a class B
Each class has a composite key (id1,id2)
In the database, I've a TableA, a TableB and I want to use a join table TableAB instead of declaring a "composite foreign key" in TableB

What could be my mapping file for that ?

Thanks


Top
 Profile  
 
 Post subject: Re: One-to-many with a composite key
PostPosted: Tue Oct 27, 2009 9:47 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Try this

Code:
@Entity
@Table (name="TableA")
@SecondaryTable(name = "join_table_name", pkJoinColumns = {
        @PrimaryKeyJoinColumn(name = "ref_id1", referencedColumnName = "id1"),
        @PrimaryKeyJoinColumn(name = "ref_id2", referencedColumnName = "id2") })
public class ClassA {
   
    @EmbeddedId
    private CompositeId id;
   
    @OneToMany
    @JoinTable(name="join_table_name",joinColumns = @JoinColumn(name="refer_b_id"),
            inverseJoinColumns = {@JoinColumn(name="ref_id1"),@JoinColumn(name="ref_id2")})
    private ClassB b;
   
   
}

@Embeddable
class CompositeId implements Serializable{
   
   
    private static final long serialVersionUID = -2655072024737775697L;

    @Column(name="id1")
    private int id1;
   
    @Column(name="id2")
    private int id2;
   
    //Need to define default constructor
    public CompositeId(){}

    //MOST IMPORTANT :Implement this method properly
    public boolean equals(Object obj) {

        return super.equals(obj);
    }

    //MOST IMPORTANT :Implement this method properly
    public int hashCode() {

        return super.hashCode();
    }
   
}

@Entity
@Table(name="TableB")
class ClassB{
   //
   
}


Top
 Profile  
 
 Post subject: Re: One-to-many with a composite key
PostPosted: Tue Oct 27, 2009 3:43 pm 
Newbie

Joined: Mon May 01, 2006 6:14 am
Posts: 6
thanks, I will try it


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.