Hibernate 映射超类关系和覆盖 [英] Hibernate Mapped Superclass relationships and overriding

查看:28
本文介绍了Hibernate 映射超类关系和覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象的MappedSuperClass,Participant,由三种'Participant'扩展而来.然后每个人都使用自己的项目"类型,也是一个抽象的 MappedSuperClass.但是,我希望基类了解项目,以便我可以编写通用代码与参与者进行交互.如何使用 Hibernate 注释来指定它?以及如何在 ExtendedParticipant 和 ExtendedProject 类中覆盖它?

I have an abstract MappedSuperClass, Participant, which is extended by three kinds of 'Participant'. Each one then uses its own kind of 'Project', also an abstract MappedSuperClass. However, I want the base class to know about Projects so I can write generic code to interact with Participants. How do I specify this using Hibernate annotations? and how will I override it in the ExtendedParticipant and ExtendedProject classes?

每个参与者类型和每个项目类型都有自己的数据库表,其中包含我无法更改的现有数据和 ID(在表中不是唯一的).

Each Participant type, and each Project type, have their own database tables with existing data and ids (not unique across tables) that I cannot change.

下面的代码给了我 IDE 错误多对一属性不应该是'映射超类'".

The following code gives me the IDE error "Many to one attribute should not be 'Mapped Superclass'".

@MappedSuperclass
public abstract class Participant implements Persistable {

    ...

    @ManyToOne
    @JoinColumn(name = "project_id")
    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    ...
}

和 Project 类大同小异,有同样的问题:

and the Project class is much the same with the same problem:

@MappedSuperclass
public abstract class Project implements Persistable {

    ...

    @OneToMany
    public List<Participant> getParticipants() {
        return participants;
    }

    public void setProject(List<Participant> participants) {
        this.participants = participants;
    }

    ...
}

推荐答案

映射的超类不是实体,它不能是关联的一部分.因此,将您的类映射为实体,并在它们上方"引入一个映射的超类,或者使用 TABLE_PER_CLASS 策略.

A mapped superclass is not an Entity, it can't be part of an association. So map your classes as entities and either introduce a mapped superclass "above" them or use a TABLE_PER_CLASS strategy.

这篇关于Hibernate 映射超类关系和覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆