简体   繁体   English

引用的属性不是 (One|Many)ToOne: 在 mappedBy 中

[英]Referenced property not a (One|Many)ToOne: in mappedBy of

I'm pretty new in hibernate.我是 hibernate 的新手。 I have to class: User (parent) and Area (child) that are related, User has the FK with Area.我必须 class:相关的用户(父)和区域(子),用户具有带区域的 FK。 I'm doing the hibernate relations but I'm getting error:我正在处理 hibernate 关系,但出现错误:

 2020-05-31 12:39:53.442 WARN --- [ restartedMain] ationConfigEmbeddedWebApplicationContext: Exception encountered

during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed;在上下文初始化期间 - 取消刷新尝试:org.springframework.beans.factory.BeanCreationException:创建在 class 路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class] 中定义的名称为“entityManagerFactory”的 bean 时出错:调用初始化方法失败; nested exception is org.hibernate.AnnotationException: Referenced property not a (One|Many)ToOne: com.telefonica.npro.model.Area.id_area in mappedBy of com.telefonica.npro.model.User.area nested exception is org.hibernate.AnnotationException: Referenced property not a (One|Many)ToOne: com.telefonica.npro.model.Area.id_area in mappedBy of com.telefonica.npro.model.User.area

This is USER:这是用户:

    @Entity
@Table(name = "NPRO_USUARIOS")
public class User implements Serializable {

    private static final long serialVersionUID = -1330075515340995797L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @NotNull
    @Column
    private int id_usuario;

    @Column
    @NotNull
    private String nombre_usuario;


    @OneToOne(mappedBy = "id_sociedad", cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH })
    private Sociedad sociedad;


    @OneToOne(mappedBy = "id_area", cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH })
    private Area area;

    @Column
    @NotNull
    private String matricula_usuario;


    @Column
    @NotNull
    private String email_usuario;

    @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH })
    @JoinColumn(name="id_perfil")
    private Perfil perfil;

}

This is Area:这是区域:

@Entity
@Table(name = "NPRO_MAESTRO_AREAS")
public class Area implements Serializable {

    private static final long serialVersionUID = -1330075515340995797L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @NotNull
    @Column
    private int id_area;

    @NotNull
    @Column
    private String nombre_area;

    @Column
    private LocalDateTime fecha_modif;
    @Column
    private String observaciones;
    @Column
    private int usuario_modif;
    @Column
    @NotNull
    private String activo;

    @ManyToOne
    @JoinColumn(name="id_sociedad")
    private Sociedad sociedad;

    @OneToOne
    @JoinColumn(name="id_usuario")
    private User user;
}

I have no idea what is wrong.. Thanks in advance!我不知道出了什么问题..提前谢谢!

Antonio安东尼奥

mappedBy should be the field in the related entity that can be navigated back to the current entity. mappedBy应该是相关实体中可以导航回当前实体的字段。 So for User , it should be the user field in Area :所以对于User ,它应该是Area中的user字段:

@Entity
@Table(name = "NPRO_USUARIOS")
public class User implements Serializable {


     @OneToOne(mappedBy = "user", cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH })
     private Area area;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM