我遇到了这个错误:
sesion.org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: entities.Elaborado.componentes[entities.Producto]
这是父类(super 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;
}
子类是:
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;
}
这是我必须解决的拼贴练习。问题是我有一些限制。如果我将 @Entity 添加到父类(super class)中,它会要求提供我没有且无法创建的表 Producto 。 我也无法将继承类型更改为 SINGLE_TABLE,因为老师给了我 2 个不同的子类表和 0 个父类(super class)表。
很抱歉,类和属性的名称是西类牙语。如果您需要我翻译它们,请告诉我。
最佳答案
来自规范:
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
每个具体类需要一个表,因此抽象类 Producto
不需要表。
关于java - Hibernate 使用 @OneToMany 或 @ManyToMany 定位未映射的类,其中集合指向没有表的父类(super class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28388552/