OneToOne引用未知的实体错误

OneToOne引用未知的实体错误

本文介绍了Hibernate @OneToOne引用未知的实体错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到导致错误的根本原因,我的@OneToOne映射不起作用,并返回引用未知实体异常。

首先,我确保两个类都使用@Entity标签正确注释。另外值得注意的是我的Product.java类(你会在下面看到)工作的很好,而且当我尝试将外键附加到引用Product.java类的Project.java类时,只会遇到问题。



Project.java和Product.java的Java代码

  @Entity 
@Table(name =project)
@SequenceGenerator(name =seq_project,sequenceName =BI **********.SEQ_PROJECT,allocationSize = 1,initialValue = 1)
public class Project {

// Fields
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator =seq_project)
@Column(name =PROJECT_ID)
private int id;
@Column(name =PROJECT_CODE)
private String projectCode;
@Column(name =PRODUCT_ID)
private int productId;
@Column(name =DESCRIPTION)
私有字符串描述;

@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
// @ PrimaryKeyJoinColumn
@JoinColumn(name =PRODUCT.PRODUCT_ID)
私人产品;


@Entity
@Table(name =PRODUCT)
@SequenceGenerator(name =seq_product,sequenceName =BI ****** ******。SEQ_PRODUCT,allocationSize = 1,initialValue = 1)
public class Product {

//字段
@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE,generator =seq_product)
@Column(name =PRODUCT_ID)
private int productId;
@Column(name =PRODUCT_NAME)
private String productName;
@Column(name =DESCRIPTION)
私有字符串描述;
@OneToOne(mappedBy =Project)
@JoinColumn(name =PROJET_FK1)
私人项目项目;

以下是我收到的错误代码:

 线程main中的异常org.hibernate.AnnotationException:com.manh.entries.Project.product中的@OneToOne或@ManyToOne引用未知实体:com.manh.entries .Product 

感谢您的任何帮助和建议。

解决方案

这是一个配置问题。 Hibernate无法识别类 Product 作为实体。


I'm having a hard time finding the root cause of an error where my @OneToOne mapping isn't working and is returning a "references an unknown entity" exception.

To start with I ensured that both classes are properly annotated with the @Entity tag. Also it is worth noting my Product.java class (which you will see below) worked fine and only encounters a problem when I try to attach a foreign key to my Project.java class referencing the Product.java class.

Java Code for Project.java and Product.java

@Entity
@Table (name="project")
@SequenceGenerator(name="seq_project",sequenceName="BI**********.SEQ_PROJECT", allocationSize=1, initialValue=1)
public class Project {

    //Fields
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_project")
    @Column(name="PROJECT_ID")
    private int id;
    @Column(name="PROJECT_CODE")
    private String projectCode;
    @Column(name="PRODUCT_ID")
    private int productId;
    @Column(name="DESCRIPTION")
    private String description;

    @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    //@PrimaryKeyJoinColumn
    @JoinColumn(name="PRODUCT.PRODUCT_ID")
    private Product product;


@Entity
@Table (name="PRODUCT")
@SequenceGenerator(name="seq_product",sequenceName="BI************.SEQ_PRODUCT", allocationSize=1, initialValue=1)
public class Product {

    //Fields
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_product")
    @Column(name="PRODUCT_ID")
    private int productId;
    @Column(name="PRODUCT_NAME")
    private String productName;
    @Column(name="DESCRIPTION")
    private String description;
    @OneToOne(mappedBy="Project")
    @JoinColumn(name="PROJET_FK1")
    private Project project;

Here is the error code I'm getting:

Exception in thread "main" org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.manh.entries.Project.product references an unknown entity: com.manh.entries.Product

Thanks for any help and advice.

解决方案

It's a configuration problem. Hibernate doesn't recognize class Product as a entity.

这篇关于Hibernate @OneToOne引用未知的实体错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 07:43