简体   繁体   English

多个持久性单元获取未知实体异常

[英]Multiple persistence-units getting unknown entity exception

Actually I have 2 persistence units in persistence.xml where Gender entity is configured in phase1-pu persistence unit and Person is configured in phase2-pu . 实际上,我在persistence.xml中有2个持久性单元,其中Gender实体是在phase1-pu持久性单元中配置的,Person是在phase2-pu配置的。 In Person entity I want to add @ManyToOne to Gender Entity . 在个人实体中,我想将@ManyToOne to Gender Entity添加@ManyToOne to Gender Entity

When I start the application getting following error: 当我启动应用程序时,出现以下错误:

[ERROR] Failed to execute goal de.juplo:hibernate-maven-plugin:2.0.0:create (h2-create-pris-p2) on project lookup-rest: Execution h2-create-pris-p2 of goal de.juplo:hibernate-maven-plugin:2.0.0:create failed: @OneToOne or @ManyToOne on my.mimos.entity.Person.gender references an unknown entity: my.mimos.entity.Gender-> [Help 1][ERROR] [错误]无法在项目查找其余部分上执行目标de.juplo:hibernate-maven-plugin:2.0.0:create(h2-create-pris-p2):执行目标de.juplo的h2-create-pris-p2 :hibernate-maven-plugin:2.0.0:创建失败:my.mimos.entity.Person.gender上的@OneToOne或@ManyToOne引用了未知实体:my.mimos.entity.Gender-> [帮助1] [错误]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [错误]要查看错误的完整堆栈跟踪,请使用-e开关重新运行Maven。
[ERROR] Re-run Maven using the -X switch to enable full debug logging. [错误]使用-X开关重新运行Maven以启用完整的调试日志记录。 [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException [错误] [错误]有关错误和可能的解决方案的更多信息,请阅读以下文章:[错误] [帮助1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

Following is my setup 以下是我的设置

persistence.xml persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
<persistence-unit name="phase1-pu">
    <class>my.lookup.entity.Gender</class>
</persistence-unit>

<persistence-unit name="pahse2-pu">
    <class>my.lookup.entity.Person</class>
</persistence-unit>

Gender.java Gender.java

@Entity
@Table(name = "REF_GENDER")
public class Gender extends AbstractEntity<Byte> {

    @Id
    @Access(AccessType.PROPERTY)
    private Byte id;

    @NotBlank
    @Size(max = 100)
    @Column(nullable = false, length = 100)
    private String name;

    @NotBlank
    @Size(max = 10)
    @Column(nullable = false, length = 10)
    private String code;
}

Person.java 人.java

@Entity
@Table(name = "REF_PERSON")
public class Person extends AbstractEntity<Byte> {
    @Id
    @Access(AccessType.PROPERTY)
    private Byte id;

    @NotBlank
    @Size(max = 100)
    @Column(nullable = false, length = 100)
    private String name;

    @NotBlank
    @Size(max = 10)
    @Column(nullable = false, length = 10)
    private String code;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "gender_id")
    private Gender gender;
}

Persistence units have their own entity manager, they are meant to be distinctly separate, usually for different databases or schemas. 持久性单元有自己的实体管理器,它们通常是针对不同的数据库或架构而明确地分开的。

You cannot map entities across persistence units, You can define the same entity class to be present in two persistence units. 您不能跨持久性单元映射实体。可以定义存在于两个持久性单元中的相同实体类。

Persistence units you have to create for one database or schema for only one.If in your application uses two different database, then use two persistence units.Each persistence creates their own entity manager. 您必须为一个数据库或模式仅创建一个持久性单元。如果在您的应用程序中使用两个不同的数据库,则使用两个持久性单元。每个持久性都会创建自己的实体管理器。 In your code its does not looks two database. 在您的代码中它看起来不像两个数据库。 use one persistence unit only. 仅使用一个持久性单元。

persistence.xml persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="phase1-pu">
<class>my.lookup.entity.Gender</class>
<class>my.lookup.entity.Person</class>

</persistence-unit>   
</persistence>

Gender.java Gender.java

@Entity
@Table(name = "REF_GENDER")
public class Gender extends AbstractEntity<Byte> {

@Id
@Access(AccessType.PROPERTY)
private Byte id;

@NotBlank
@Size(max = 100)
@Column(nullable = false, length = 100)
private String name;

@NotBlank
@Size(max = 10)
@Column(nullable = false, length = 10)
private String code;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "gender", targetEntity = Person.class)
private List<Person> persons = new ArrayList();
}

Person.java 人.java

@Entity
@Table(name = "REF_PERSON")
public class Person extends AbstractEntity<Byte> {
@Id
@Access(AccessType.PROPERTY)
private Byte id;

@NotBlank
@Size(max = 100)
@Column(nullable = false, length = 100)
private String name;

@NotBlank
@Size(max = 10)
@Column(nullable = false, length = 10)
private String code;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "gender_id")
private Gender gender;
}

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

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