KFleischer
KFleischer

Reputation: 1020

JPA OneToMany Relation is not filled / populated / still null

This problem drives me crazy. I have a bidirectional One2Many relation. When I load the "One" over entitymanager.find() the list of related objects is null.

What is wrong, that the relationship is not populated?

Here is my Code...

The "One"

Java

@Entity
@Table(name = "CREDIT_APPROVAL")
public class CreditApproval extends FourEyesTask<CreditApproval> {

   @OneToMany(mappedBy = "credit", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
   private Set<AddedDebtorApproval> addedDebtors = null;

The "Many"

SQL

CREATE TABLE "LPM"."ADD_DEBTOR_APPROVAL" (
    "ID" DECIMAL(10 , 0) NOT NULL GENERATED ALWAYS AS IDENTITY ( START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 9999999999 NO CYCLE NO CACHE),
    "CREDIT_ID" DECIMAL(10 , 0) NOT NULL,
    "DEBTOR_ID" VARCHAR(14) NOT NULL,
    "NAME_LONG" VARCHAR(400)
)

Java

@Entity
@Table(name = "ADD_DEBTOR_APPROVAL")
public class AddedDebtorApproval {


    @ManyToOne(optional=false)
    @JoinColumn(name="credit_id", nullable=false, referencedColumnName = "credit_id")
    private CreditApproval credit;

    private String debtor_id;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private String name_long;

In my testdata I have one AddedDebtorApproval entry with credit_id = 22 and in the CreditApproval Table I have also one entry with credit_id = 22.

When I load the creditApproval Entity ("One") entityManager.find(CreditApproval.class, bean.getCredit_id()); the relation to the many is null.

Some more side notes.

the object-relational-mapping.xml ist this: updated

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd">
    <schema>LPM</schema>

    <entity class="com.ibm.lpm.history.jpa.CreditHistory" access="PROPERTY" />
    <entity class="com.ibm.lpm.history.jpa.DebtorHistory" access="PROPERTY" />
    <entity class="com.ibm.lpm.credit.jpa.CreditBasic" access="PROPERTY">
        <named-query name="findCreditBasicByTransactionNumber">
        <query>
        <![CDATA[SELECT c FROM CreditBasic c WHERE c.transactionNumber = :transactionNumber]]>
        </query>
        </named-query>
    </entity>
        <entity class="com.ibm.lpm.debtor.jpa.DebtorBasic" access="PROPERTY" />
    <entity class="com.ibm.lpm.rating.jpa.DebtorRating" access="PROPERTY" />
    <entity class="com.ibm.lpm.codes.jpa.PropCode" access="PROPERTY" />

</entity-mappings>

persistence.xml is this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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_1_0.xsd">
    <persistence-unit name="LPMJPA" transaction-type="JTA">
        <jta-data-source>jdbc/lpmDs</jta-data-source>
        <mapping-file>object-relational-mapping.xml</mapping-file>
        <class>com.ibm.lpm.history.jpa.HistoryCommon</class>
        <class>com.ibm.lpm.foureyes.taskqueue.jpa.FourEyesTask</class>
        <class>com.ibm.lpm.foureyes.taskqueue.jpa.DebtorApproval</class>
        <class>com.ibm.lpm.foureyes.taskqueue.jpa.CreditApproval</class>
        <class>com.ibm.lpm.foureyes.taskqueue.jpa.AddedDebtorApproval</class>
    </persistence-unit>
</persistence>

Eclipse gives a notification that those xxxxxApproval classes are already specified in the mapping-file (which is true).

In Eclipse in the Project Explorer, when I open the JPA Entities the CreditApproval shows a small Icon with a "?" in front of the relation to AddedDebtors. The Icon for the other direction, inside the AddedDebtorsApproval Entity, is showing a Many2One relation. enter image description here

Upvotes: 1

Views: 3287

Answers (2)

Chris
Chris

Reputation: 21145

Your mapping file shows it is changing the access type of CreditApproval to PROPERTY while the annotation for the collection is on the field. This likely causes it to ignore the annotation, using a default relation instead. Fix the access to match the annotations you want to use so they don't conflict.

Upvotes: 4

Julien Bodin
Julien Bodin

Reputation: 803

Why are you using a object-relational-mapping.xml file ? You are mixing a configuration by annotation a nd xml configuration. Have you tried to remove this line of you persistence.xml file ?

<mapping-file>object-relational-mapping.xml</mapping-file>

Upvotes: 1

Related Questions