简体   繁体   English

* 未知实体上的 @OneToOne 或 @ManyToOne

[英]@OneToOne or @ManyToOne on * an unknown entity

I have this error when I try to save a data to a table.当我尝试将数据保存到表时出现此错误。 I know there are a bunch of questions like mine, but none of them are really describing the problem I have.我知道有很多像我这样的问题,但没有一个是真正描述我遇到的问题。

I am using Annotations and mark my classes as Entities.我正在使用注释并将我的类标记为实体。 Here is the error I have.这是我遇到的错误。

@OneToOne or @ManyToOne on hibernate_test.entity.Employee.detail references an unknown entity.

And two Entities ( getters, setters and consturctors are omitted for more readable code ( they are created by IDEA anyway)还有两个实体(getter、setter 和 consturctors 被省略以获得更易读的代码(它们是由 IDEA 创建的)

First:第一的:

    @Entity
    @Table(name = "details")
    public class Detail {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private int id;
        @Column(name = "city")
        private String city;
        @Column(name = "phone_number")
        private String phoneNumber;
        @Column(name = "email")
        private String email;

And second第二个

@Entity
@Table(name="employees")
public class Employee {
    
        @Column(name = "id")
         @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;
        @Column(name = "name")
        private String name;
        @Column(name = "surname")
        private String surname;
        @Column(name = "department")
        private String department;
        @Column(name = "salary")
        private int salary;
    
        @OneToOne(cascade = CascadeType.ALL)
        @JoinColumn(name = "details_id")
        private Detail detail;

And my main method我的主要方法

SessionFactory sessionFactory = new Configuration() 
      .configure("hibernate.cfg.xml")
      .addAnnotatedClass(Employee.class) 
      .buildSessionFactory();
    
   try {
      Session session = sessionFactory.getCurrentSession();
      Employee employee = new Employee("Mark", "Bulkovsky", "PR", 5324);
            Detail detail = new Detail("Munich", "189432132", "John@gmail.com");
      employee.setDetail(detail);
      session.beginTransaction();
      session.save(employee);
      session.getTransaction().commit();
   } finally {
       sessionFactory.close();
   }

I am feeling desperate as I've been trying to solve this problem and none of the Stackoverflow answers helped me.当我一直在尝试解决这个问题时,我感到绝望,而 Stackoverflow 的答案都没有帮助我。 I wonder if the problem can be connected with incorrectly created tables.我想知道问题是否与错误创建的表有关。 So I have a screen of mine.所以我有我的屏幕。 I do appreciate your help a lot!我非常感谢您的帮助!

我的桌子

Try to correct your SessionFactory definition in this way:尝试以这种方式更正您的SessionFactory定义:

SessionFactory sessionFactory = new Configuration() 
   .configure("hibernate.cfg.xml")
   .addAnnotatedClass(Employee.class) 
   .addAnnotatedClass(Detail.class) 
   .buildSessionFactory();

The problem was in Main method.问题出在 Main 方法中。 You should always add annotated class.您应该始终添加带注释的 class。 .addAnnotatedClass(Detail.class) .addAnnotatedClass(Detail.class)

暂无
暂无

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

相关问题 @OneToOne 或 @ManyToOne 引用未知实体 - @OneToOne or @ManyToOne on references an unknown entity JPA OneToMany ManyToOne @OneToOne或@ManyToOne在引用未知实体时: - JPA OneToMany ManyToOne @OneToOne or @ManyToOne on references an unknown entity: org.hibernate.AnnotationException: @OneToOne 或 @ManyToOne<entity> 引用一个未知实体 - org.hibernate.AnnotationException: @OneToOne or @ManyToOne on <entity> references an unknown entity Spring 引导错误:@OneToOne 或 @ManyToOne 引用未知实体 - Spring Boot Error: @OneToOne or @ManyToOne references an unknown entity xxx 上的 @OneToOne 或 @ManyToOne 引用了一个未知实体:java.util.Set - @OneToOne or @ManyToOne on xxx references an unknown entity: java.util.Set @ManyToOne和@OneToOne在同一实体上 - @ManyToOne and @OneToOne on the same entity &#39;univ_orientation.dz.Entity.cursusGlobale.Student_idStudent&#39;上的@OneToOne或@ManyToOne引用了一个未知实体:int - @OneToOne or @ManyToOne on 'univ_orientation.dz.Entity.cursusGlobale.Student_idStudent' references an unknown entity: int 由以下原因引起:org.hibernate.AnnotationException:XX上的@OneToOne或@ManyToOne引用了未知实体:YY - Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on XX references an unknown entity: YY com.hibernate.domain.Bee.honey上的@OneToOne或@ManyToOne引用未知实体 - @OneToOne or @ManyToOne on com.hibernate.domain.Bee.honey references an unknown entity org.hibernate.AnnotationException:entities.Ques#tion.examId 上的 @OneToOne 或 @ManyToOne 引用了一个未知实体:long - org.hibernate.AnnotationException: @OneToOne or @ManyToOne on entities.Ques#tion.examId references an unknown entity: long
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM