-->
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.  [ 4 posts ] 
Author Message
 Post subject: How to join tow tables having each two fileds in common?
PostPosted: Fri Feb 06, 2009 2:38 am 
Newbie

Joined: Fri Feb 06, 2009 2:16 am
Posts: 8
Hibernate version: 3.0.5
JBOSS Server: 4.3.0.GA
Database: Oracle10G

I am sorry to ask this question because it seems to me as some basics in SQL. Nonetheless I see nothing quite close to it in the books and web sites I am searching in.

so easy in SQL, I thought it expected it to be easy with hibernate ate well.

I have one table linked with many others. First, the DB schema is old and most of the table lacks primary keys (But embedded Id fixed that, thanks!)

Now, all I want is to make two join between table.

I basically have two table A and B. In each table, I have two common fields such as A.field1 = B.field1 and A.filed2 = B.filed2.

this a one-to-many unidirectional relation from A to B : each record from A has many related records in B.

I created an entity EA :

Code:
@Entity
@Table(name = "A)

public class EA implements Serializable {

    @Id
    @Column(name = "ID", nullable = false)
    private Integer id;

    @Column(name = "FILED1")
    private String field1;

    @Column(name = "FILED2")
    private Integer field2;

    /*  + Getters and Setters */
   
}


and another EB :

Code:
@Entity
@Table(name = "B)

public class EB implements Serializable {

    @Id
    @Column(name = "ID", nullable = false)
    private Integer id;

    @Column(name = "FILED1")
    private String field1;

    @Column(name = "FILED2")
    private Integer field2;

    /*  + Getters and Setters */
   
}


So how do I make my mapping so when I load an entity EA, I can have a list of all related EB? (meaning all EB respecting the joins A.field1 = B.field1 and A.filed2 = B.filed2)?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2009 3:16 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Hi,
Please fine the mapping in code (as you told this is unidirectional association from EA to EB).

Code:
@Entity
@Table(name = "A)

public class EA implements Serializable {

    @Id
    @Column(name = "ID", nullable = false)
    private Integer id;

    @Column(name = "FILED1")
    private String field1;

    @Column(name = "FILED2")
    private Integer field2;

    /*  + Getters and Setters */

    @OneToMany
    @JoinColumns({ @JoinColumn(name = "FILED1", referencedColumnName="FILED1"),
                  @JoinColumn(name = "FILED2", referencedColumnName="FILED2")})
    private Set<EB> bList = new HashSet<EB>();
}


@Entity
@Table(name = "B)

public class EB implements Serializable {

    @Id
    @Column(name = "ID", nullable = false)
    private Integer id;

    @Column(name = "FILED1")
    private String field1;

    @Column(name = "FILED2")
    private Integer field2;

    /*  + Getters and Setters */
   
}



--------------------------------------------------------------------------
Please don't forget to rate, if this help you.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2009 3:44 am 
Newbie

Joined: Fri Feb 06, 2009 2:16 am
Posts: 8
Thanks for this answer. It sounds familiar with some tries I made but I shall try it again!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2009 4:36 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
If this helps you, please rate the post.
alfredoc wrote:
Thanks for this answer. It sounds familiar with some tries I made but I shall try it again!


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