hibernate问题中的@SecondaryTable注释

时间:2016-03-10 22:45:24

标签: java spring hibernate jpa

我正在使用Spring框架4.2.5.RELEASE和Hibernate 5的项目中工作,我试图用两个表映射一个实体,我有这样的事情:

@Entity
@Table(name = "programa")   
@SecondaryTable(name="jornada", pkJoinColumns={
    @PrimaryKeyJoinColumn(name="jor_codigo",   referencedColumnName="jor_codigo") })
public class Programa {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "pro_codigo")
    private int id;

    @Column(name = "pro_calendario")
    private Character calendario;

    @Column(name = "pro_registro_snies")
    private String codigoSnies;

    @Column(name = "jor_nombre", table = "jornada")
    private String jornada;

    @Column(name = "pro_propio")
    private boolean propio;

    @ManyToOne
    @JoinColumn(name = "sed_codigo")
    private Sede sede;

    @Column(name = "pro_titulo_otorgado")
    private String tituloOtorgado;

    @OneToOne
    @JoinColumn(name = "uaa_codigo")
    private Uaa uaa;
    //Setters and getters
}

我正在使用junit进行测试,我收到此错误:

Unable to find column with logical name: jor_codigo in org.hibernate.mapping.Table(programa) and its related supertables and secondary tables

希望你能帮助我,谢谢。

修改

表格的列是:  programa专栏:

  1. pro_codigo - >主键
  2. pro_nombre
  3. jor_codigo - >外键
  4. jornada专栏:

    1. jor_codigo - >主键
    2. jor_nombre

2 个答案:

答案 0 :(得分:0)

 @PrimaryKeyJoinColumn(name="jor_codigo",   referencedColumnName="jor_codigo")

您的referencedColumnName应为pro_codigo。以便jor_codigojornada可以参考它。

答案 1 :(得分:0)

@PrimaryKeyJoinColumn(name="jor_codigo", referencedColumnName="jor_codigo") })

在此处指明如何将辅助实体映射到主要实体。在这里,您希望通过两个实体来实现这一目标。 id列。

name="main_entity_id_column_name", referencedColumnName="secondary_entity_id_column_name") 因此,在您的情况下,referencedColumnName应为pro_codigo