Hibernate 1:n Beziehung mit Vererbung

  • Themenstarter Themenstarter Gast2
  • Beginndatum Beginndatum
G

Gast2

Gast
Hallo zusammen,

ich habe ein Problem mit Hibernate und zwar habe ich eine Klassenhierarchie(SINGLE_TABLE) zu der ich eine bidirektionale Beziehung aufbauen möchte. Es sind quasi 2 Beziehung zu den jeweiligen Unterklassen (siehe Code).

Aber ich bekomme folgende Exception.
Code:
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: jpa.Child.personList in jpa.PersonList.childs

Hier mein Beispiel (Die Klassenhierarchie funktioniert soweit)
Oberklasse
Java:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "diskriminator", discriminatorType = DiscriminatorType.INTEGER)
public class Person {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	protected Long id;
	private String name;

	@ManyToOne
	protected PersonList personList; --> hier ist das Problem

	public void setId(Long id) {
		this.id = id;
	}

	public Long getId() {
		return id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setPersonList(PersonList personList) {
		this.personList = personList;
	}

	public PersonList getPersonList() {
		return personList;
	}
}

2 Unterklassen davon
Java:
package jpa;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue("1")
public class Parent extends Person {

	private boolean married;

	public void setMarried(boolean married) {
		this.married = married;
	}

	public boolean isMarried() {
		return married;
	}
}


package jpa;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue("0")
public class Child extends Person {

	private int age;

	public void setAge(int age) {
		this.age = age;
	}

	public int getAge() {
		return age;
	}
}

Und hier das Beziehungsobjekt dazu...

Java:
package jpa;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class PersonList {
	@Id
	@GeneratedValue
	private Long id;
	@OneToMany(mappedBy = "personList")
	private Set<Child> childs = new HashSet<Child>();
	@OneToMany(mappedBy = "personList")
	private Set<Parent> parents = new HashSet<Parent>();

	public void setId(Long id) {
		this.id = id;
	}

	public Long getId() {
		return id;
	}

	public void setChilds(Set<Child> childs) {
		this.childs = childs;
	}

	public Set<Child> getChilds() {
		return childs;
	}

	public void setParents(Set<Parent> parents) {
		this.parents = parents;
	}

	public Set<Parent> getParents() {
		return parents;
	}
}

Ich will quasi 2 Beziehungen zu den Unterklassen und NICHT zu der Oberklasse Person. Geht sowas mit Hibernate?
Ich hab das Problem erst seit ich eine bidirektionale Beziehung eingebaut hab("mappedBy" und in der Personen Klasse die Membervariable personList mit @ManyToOne)

Kompletter Stacktrace
Java:
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: jpa.Child.personList in jpa.PersonList.childs
	at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:685)
	at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:645)
	at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:65)
	at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1716)
	at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1423)
	at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1375)
	at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1519)
	at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193)
	at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1100)
	at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:282)
	at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:366)
	... 4 more

Weiß jemand wie man sowas elegant lösen kann?
 
Hmm, ich würd da jetzt auch nur anfangen, zu spielen. Spontan fällt mir das target-Attribut der OneToMany-Annotation ein. Ausprobiert habe ich es aber nicht...

Ach ja, und im Englischen ist die Mehrzahl von child children und nicht childs 😉
 
Zuletzt bearbeitet von einem Moderator:
Hmm, ich würd da jetzt auch nur anfangen, zu spielen. Spontan fällt mir das target-Attribut der OneToMany-Annotation ein. Ausprobiert habe ich es aber nicht...

Hab ich auch schon ausprobiert, welche target Klasse würdest den angeben

Ach ja, und im Englischen ist die Mehrzahl von child children und nicht childs 😉

Das hat Eclipse generiert 😉, für das Bsp. war mir das egal 😛
 

Zurück
Oben