[英]@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.