我在此错误消息中发现了很多关于SO的问题,但是不幸的是(不幸的是,对于以前有此问题的人),他们都缺少了他们打算映射的列。我有两个类ResetPin和User,并且想要在ResetPin和User.email之间建立OneToOne映射。

但是,每当我运行项目时,都会收到以下错误消息:

Caused by: org.hibernate.MappingException: Unable to find column with logical name: email in org.hibernate.mapping.Table(users) and its related supertables and secondary tables
    at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:582)
    at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:258)
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:116)
    at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1598)
    at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1521)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1422)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852)


以下是我的课程:

ResetPin类:

public class ResetPin implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "email", referencedColumnName = "email")
    private User user;

    @NotNull
    @Size(min = 5, max = 50)
    @Column(name = "token")
    private String token;

    @Column(name = "active")
    private Boolean active;

    //Getters and setters
    .
    .
    .
    .
}


用户类别:

public class User implements Serializable {

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

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

    @NotNull
    @Size(min = 5, max = 50)
    @Column(name = "email", unique = true)
    private String email;

    @NotNull
    @Size(min = 5, max = 50)
    @Column(name = "password")
    private String password;

    //Getters and setters
    .
    .
    .
    .

}

最佳答案

referencedColumnName是指表示另一个表(在您的情况下为User)中的关联的列的名称,并且应映射到主键

  @JoinColumn(name = "email", referencedColumnName = "id")


默认情况下,它是引用主键的,除非您有复合键,然后您需要使用它,所以在您的情况下,它就足够像这样

@JoinColumn(name = "email")

09-03 20:00