0

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. In Person entity I want to add @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]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging. [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

Following is my setup

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

@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

@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;
}

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.