Hibernate 使用 @OneToMany 或 @ManyToMany 以未映射的 class 为目标,其中集合指向没有 table 的超级 class

Hibernate Use of @OneToMany or @ManyToMany targeting an unmapped class where the collection is pointing to a super class that doesn't have a table

我有这个错误:

sesion.org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: entities.Elaborado.componentes[entities.Producto]

这是超级class:

package entities;

import javax.persistence.*;

@MappedSuperclass
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Producto {

    @Id
    protected Integer numero;
    protected String descripcion;

    public Integer getNumero() {
        return numero;
    }
    public void setNumero(Integer numero) {
        this.numero = numero;
    }
    public String getDescripcion() {
        return descripcion;
    }
    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

而子class是:

package entities;


import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;

@Entity

@Table(name="elaborados")

public class Elaborado extends Producto {

    private float precioVenta;
    private int porcentajeGanancia;

@ManyToOne
private Unidad unidad;

@OneToMany
@JoinTable(
        name="compuestoDe",
        joinColumns = @JoinColumn( name="codProductoE"),
        inverseJoinColumns = @JoinColumn( name="codProductoSM")
        )
private List<Producto>componentes;

public float getPrecioVenta() {
    return precioVenta;
}
public void setPrecioVenta(float precioVenta) {
    this.precioVenta = precioVenta;
}
public int getPorcentajeGanancia() {
    return porcentajeGanancia;
}
public void setPorcentajeGanancia(int porcentajeGanancia) {
    this.porcentajeGanancia = porcentajeGanancia;
}
public Unidad getUnidad() {
    return unidad;
}
public void setUnidad(Unidad unidad) {
    this.unidad = unidad;
}
public List<Producto> getComponentes() {
    return componentes;
}
public void setComponentes(ArrayList<Producto> componentes) {
    this.componentes = componentes;
}

这是我必须为 Collage 解决的练习。问题是我有一些限制。如果我将 @Entity 添加到 super class ,它会要求 table Producto,我没有也无法创建。 我也不能将继承类型更改为 SINGLE_TABLE,因为老师给了我 2 个不同的 table 用于 subclasses 和 0 用于 superclass。

很抱歉 classes 和属性的名称是西班牙语。如果您需要我翻译它们,请告诉我。

来自规范:

A mapped superclass, unlike an entity, is not queryable and must not be passed as an argument to EntityManager or Query operations.

但是查询 Producto 是这段代码的作用:

@OneToMany
@JoinTable(
        name="compuestoDe",
        joinColumns = @JoinColumn( name="codProductoE"),
        inverseJoinColumns = @JoinColumn( name="codProductoSM")
        )
private List<Producto>componentes;

您必须将 @MappedSuperclass 更改为 @Entity,并保留 @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) 以匹配您拥有的两个 table。

InheritanceType.TABLE_PER_CLASS 每个 concrete class 需要一个 table,所以 [=16] 不需要 table =].