实际上我在persistence.xml
中有2个持久性单元,其中性别实体在phase1-pu
持久性单元中配置,而Person在phase2-pu
中配置。在Person实体中,我想添加@ManyToOne to Gender Entity
。
当我启动应用程序时出现以下错误:
[错误]无法执行目标de.juplo:hibernate-maven-plugin:2.0.0:create(h2-create-pris-p2)on project lookup-rest:执行h2-create-pris-p2 of goal de.juplo:hibernate-maven-plugin:2.0.0:创建失败:my.mimos.entity.Person.gender上的@OneToOne或@ManyToOne引用了一个未知实体:my.mimos.entity.Gender-> [帮助1] [错误]
[错误]要查看错误的完整堆栈跟踪,请使用-e开关重新运行Maven [ERROR]使用-X开关重新运行Maven以启用完整的调试日志记录。 [错误] [错误]有关错误和可能解决方案的更多信息,请阅读以下文章: [错误] [帮助1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
以下是我的设置
<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>
@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;
}
@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;
}
答案 0 :(得分:1)
持久性单元有自己的实体管理器,它们应明确分开,通常用于不同的数据库或模式。
您不能跨持久性单元映射实体,您可以定义要在两个持久性单元中存在的同一实体类。
答案 1 :(得分:0)
必须为一个数据库或模式仅为一个创建持久性单元。如果您的应用程序使用两个不同的数据库,则使用两个持久性单元。每个持久性创建自己的实体管理器。在你的代码中它看起来不是两个数据库。仅使用一个持久性单元。
的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
@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
@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;
}