引起:org.hibernate.AnnotationException:@OneToOne或XX上的@ManyToOne引用了一个未知实体:YY

时间:2018-02-03 13:35:06

标签: java hibernate

这里我的目标实体User.class存在于一个单独的jar文件中。

package com.aa.model;
    import javax.persistence.Entity;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToOne;

@Entity
@Table(name = "ABC")
public class Abc implements Serializable{
---    
@OneToOne(targetEntity=com.bb.model.User.class)
    @JoinColumn(name  = "CREATED_BY")
    private User createdBy;
}
---
}

目标类:

package com.bb.model;
    import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="USER")
public class User implements Serializable {
---
}

我收到以下错误:

Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.aa.model.Abc.createdBy references an unknown entity: com.bb.model.User
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:109)
    at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1598)

1 个答案:

答案 0 :(得分:0)

在Abc班,你写了

@OneToOne(targetEntity=com.bb.User.class)

而不是

@OneToOne(targetEntity=com.bb.model.User.class)

您忘记了com.bb.model包的导入

import com.bb.model.User;