我可以在课堂上使用相同的mapedBy名称吗?

时间:2016-11-28 09:42:32

标签: hibernate mapping java-ee-7

我收到此错误但我不确定是否因为mappedBy的名称与属性相同

错误

Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Collection, at table: a_escalaPregunta, for columns: [org.hibernate.mapping.Column(valoracioItems)]

Class EscalaPregunta

@Entity
@Table(name = "a_escalaPregunta")
public class EscalaPregunta extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(length = 255)
    private String pregunta;

    @OneToMany(mappedBy = "pregunta", cascade = CascadeType.ALL)
    private Collection<EscalaResposta> respostes;

    @OneToMany(mappedBy = "pregunta", cascade = CascadeType.PERSIST)
    private Collection<ValoracioItem> valoracioItems;

    @Id
    public Long getId(){
       return id;
    }

Class EscalaResposta

@Entity
@Table(name = "a_escalaResposta")
public class EscalaResposta extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "idEscalaPregunta", referencedColumnName = "id", nullable = false)
    private EscalaPregunta pregunta;

ValoracioItem类

@Entity
@Table(name = "t_valoracioitem")
public class ValoracioItem extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(cascade = CascadeType.PERSIST, optional = false)
    @JoinColumn(name = "idEscalaPregunta", nullable = false)
    private EscalaPregunta pregunta;

2 个答案:

答案 0 :(得分:0)

您错过了ValoracioItem实体中的以下属性

referencedColumnName = "id" 

它应该像这样

    @ManyToOne(cascade = CascadeType.PERSIST, optional = false)
    @JoinColumn(name = "idEscalaPregunta", referencedColumnName = "id", nullable = false)
    private EscalaPregunta pregunta;

答案 1 :(得分:0)

问题是注释@Id在属性和getter方法中被重复。