1

Can I have a relationship as below:

@Entity Table1{

    @ManyToOne
    @JoinColumn(name = "Column1", 
                referencedColumnName = "t2id", 
                insertable = false, 
                updatable = false)
    private Table2 table2_col;

    @ManyToOne
    @JoinColumn(name = "Column1", 
                referencedColumnName = "t3id", 
                insertable = false, 
                updatable = false)
    private Table3 table3_col;
}
Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58

1 Answers1

1

Yes, the mapping looks valid. The column Column1 in both cases belong to different tables (Table2.column1 and Table3.column1). So I don't see any collision here. It is not the case as the tittle says "one column references to two other columns".

In this case you have two many-to-one relations: Table1<--->Table2 and Table1<--->Table3. So the column1 in both tables (2 and 3) is a Foreign Key to the Table1. So you have 2 different foreign keys.

Alex Vayda
  • 6,154
  • 5
  • 34
  • 50
  • Thanks, but the Column1 is the column of Table1 and it references t2id from Table2 and t2id from Table3, so it is still same as a single column referencing two other columns. Is that a valid scenario? Because I don't get desired output. – user2055100 Feb 10 '13 at 04:23
  • Ah, you are right. Sorry, I misread your code. Can you show the Table2 and Table3 mapping? What exactly are you trying to implement? May be you problem can be solved using different mapping? In terms of database you cannot have a foreign key referencing more than one column. (See http://stackoverflow.com/questions/7844460/foreign-key-to-multiple-tables). But there are workarounds. – Alex Vayda Feb 10 '13 at 11:26
  • Btw, it's not a good idea to think tables when working with ORM. Instead it's better to think objects. – Alex Vayda Feb 10 '13 at 11:29