具有子类和泛型的双向多对一

时间:2017-01-16 12:55:06

标签: java hibernate jpa

我正在努力实现两个类层次结构之间的双向多对一映射。

我有以下内容:

超类Queue,包含子类AQueueBQueueCQueue。 超类Element,包含子类AElementBElementCElement

AQueue有一个AElement列表,BQueue列有BElement,依此类推,AElement有一个AQueue和等等。

我尝试过这样的事情:

@Entity
@Inheritance(strategy InheritanceType.SINGLE_TABLE)
public abstract class Queue<T extends Element> {
    @OneToMany(mappedBy="queue")
    private List<T> elements = new ArrayList<>();
    //...
}

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Element<T extends Element> {
    @ManyToOne
    @JoinColumn("queue_id")
    private Queue<T> queue;
}

只有Hibernate抱怨mappedBy reference an unknown target entity property

有没有办法使用泛型在超类中映射这样的关系,还是我必须选择每个维持一个关系的子类对?

1 个答案:

答案 0 :(得分:1)

这是一个适合我的Spring Boot测试(跳过样板)(Hibernate 5.0):

import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.transaction.Transactional;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.*;

@SpringBootTest
@RunWith(SpringRunner.class)
public class JpaTest {

    @Autowired
    private EntityManager em;

    @Test
    @Transactional
    public void test() {
        QueueA queueA = new QueueA(1L);
        ElementA elementA = new ElementA(1L, queueA);
        queueA.getElements().add(elementA);
        em.persist(queueA);
        em.persist(elementA);

        QueueB queueB = new QueueB(2L);
        ElementB elementB = new ElementB(2L, queueB);
        queueB.getElements().add(elementB);
        em.persist(queueB);
        em.persist(elementB);

        List queues = em.createQuery("SELECT q FROM Queue q").getResultList();

        assertThat(queues).containsOnly(queueA, queueB);

        List elements = em.createQuery("SELECT e FROM Element e").getResultList();

        assertThat(elements).containsOnly(elementA, elementB);
    }
}

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
abstract class Queue<T extends Element<T>> {

    @Id
    private Long id;

    @OneToMany(mappedBy = "queue", targetEntity = Element.class)
    private List<T> elements = new ArrayList<>();

    public Queue(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

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

    public List<T> getElements() {
        return elements;
    }

    public void setElements(List<T> elements) {
        this.elements = elements;
    }
}

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
abstract class Element<T extends Element<T>> {

    @Id
    private Long id;

    @ManyToOne(targetEntity = Queue.class)
    private Queue<T> queue;

    public Element(Long id, Queue<T> queue) {
        this.id = id;
        this.queue = queue;
    }

    public Long getId() {
        return id;
    }

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

    public Queue<T> getQueue() {
        return queue;
    }

    public void setQueue(Queue<T> queue) {
        this.queue = queue;
    }
}

@Entity
class QueueA extends Queue<ElementA> {

    public QueueA(Long id) {
        super(id);
    }
}

@Entity
class ElementA extends Element<ElementA> {

    public ElementA(Long id, Queue<ElementA> queue) {
        super(id, queue);
    }
}

@Entity
class QueueB extends Queue<ElementB> {

    public QueueB(Long id) {
        super(id);
    }
}

@Entity
class ElementB extends Element<ElementB> {

    public ElementB(Long id, Queue<ElementB> queue) {
        super(id, queue);
    }
}

要解决的重要事项是:

  • 使所有泛型正确并正确引用彼此
  • targetEntity关系中设置@OneToMany/@ManyToOne。否则,Hibernate无法仅从通用字段确定应该是实际引用的类。在这里,通过指定targetEntity = Element.class / targetEntity = Queue.class我们说我们希望Hibernate将它映射到整个实体类层次结构。