-->
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.  [ 1 post ] 
Author Message
 Post subject: [solved] trying to join one table two times
PostPosted: Mon Dec 12, 2011 11:16 am 
Newbie

Joined: Mon Dec 12, 2011 10:25 am
Posts: 1
Hi there,

i am trying to join one table two times, but for some reason there is only the second one in the result.

Here is an example:

I have two tables:
text and translation.

text contains two fields referencing translation ('foo' and 'bar').

Here is my attempt:

Code:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Translation {

   @Id
   private Long tid;
 
  private String text;
   
}


Code:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Text {

   @Id
   @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequence")
  @SequenceGenerator(name="sequence", sequenceName="seq_oid")
   protected Long oid;
   
   @ManyToOne(optional = true)
   @JoinTable(name = "Translation",
      joinColumns = {
         @JoinColumn(name = "tid")
      },
      inverseJoinColumns = {
         @JoinColumn(name="foo")
      }
   )
   protected Translation foo;
 
   @ManyToOne(optional = true)
   @JoinTable(name = "Translation",
      joinColumns = {
         @JoinColumn(name = "tid")
      },
      inverseJoinColumns = {
         @JoinColumn(name="bar")
      }
   )
   protected Translation bar;
   
   protected String moep;
   
}


But what I expected to get is something like this:

Code:
select text.oid, translation_0.text, translation_1.text, text.moep
from (
  select 200 as oid,
     100 as foo,
     101 as bar,
     'a value' as moep
  union
  select 201 as oid,
     102 as foo,
     103 as bar,
     'another value' as moep
  ) text
  left outer join (
  select 100 as tid,
    'first value' as text
  union
  select 101 as tid,
    'second value' as text
  union
  select 102 as tid,
    'third value' as text
  union
  select 103 as tid,
    'fourth value' as text
  ) translation_0 on translation_0.tid = text.foo
  left outer join (
  select 100 as tid,
    'first value' as text
  union
  select 101 as tid,
    'second value' as text
  union
  select 102 as tid,
    'third value' as text
  union
  select 103 as tid,
    'fourth value' as text
  ) translation_1 on translation_1.tid = text.bar


Any would be greatly appreciated.

Update:

By replacing the annotation

Code:
   @ManyToOne(optional = true)
   @JoinTable(name = "Translation",
      joinColumns = {
         @JoinColumn(name = "tid")
      },
      inverseJoinColumns = {
         @JoinColumn(name="foo")
      }
   )


by this

Code:
    @ManyToOne(optional = true)
    @JoinColumn(name = "foo")


everything seems to be fine.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.