JPA (Hibernate) : Tabelle zweimal joinen

DiaEcho

Mitglied
Hallo,

ich versuche hier eine Tabelle zweimal zu joinen, aber leider taucht im Ergebnis nur der letzte join auf.

Hier ein Beispiel:

Ich habe zwei Tabellen:
text und translation.

text referenziert zweimal translation, einmal durch 'foo' und einmal durch 'bar'.

So sieht mein Versuch aus:

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

	@Id
	private Long tid;
  
	private String text;
	
}

Java:
@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;
	
}

Mit dem Erfolg das foo immer null bleibt. Der Hibernate Debug-Log zeigt, dass das Feld 'foo' ignoriert wird und die Tabelle translation nur einmal gejoined wird.

Und hier einmal als SQL was ich eigentlich erwartet haette:

[sql]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[/sql]

Vielen Dank.
 
Bei kurzem drauf schauen fällt mir auf, dass deine JoinColumns gleich heißen

@JoinColumn(name = "tid")
 
stimmt mein Fehler 😉

Aber ist das was Du da machst dann nicht eine ManyToMany Beziehung? Welche Du dann als Collection in der Klasse Text mappen würdest?

etwa so:

Java:
	@ManyToMany
	@JoinTable(name = "TRANSLATION_JOIN_TABLE",
			inverseJoinColumns = { @JoinColumn(name = "TRANSLATION_ID") },
			joinColumns = { @JoinColumn(name = "TEXT_ID") })
	public List<Translation> getTranslations() {
		return translations;
	}

Achtung ...nicht getestet, sondern nur schnell hingeschrieben
 
Zuletzt bearbeitet von einem Moderator:
Aber ist das was Du da machst dann nicht eine ManyToMany Beziehung? Welche Du dann als Collection in der Klasse Text mappen würdest?

Danke, aber das ist hier keine Many-To-Many Beziehung.

Ein Eintrag in Text sieht etwa so aus:

SQL:
select 200 as oid, -- Ist PK
  100 as foo, -- Referenz auf Translation
  101 as bar, -- Referenz auf Translation
  'a value' as moep -- Beliebiger Wert


Ich habe hier also in der Tat 2 Referenzen auf 'Translation'.

Update:

Wenn ich die Annotation von

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

auf

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

umstelle funktioniert es uebrigens, nur das ich dann keine outer joins mehr habe.

Update:

... nur das ich dann keine outer joins mehr habe.

Stimmt nicht, so funktionierts !
 
Zuletzt bearbeitet:

Zurück
Oben