JPA OneToMany Relation未填充/填充/仍为null

时间:2013-05-17 09:24:36

标签: java jpa one-to-many

这个问题让我发疯。我有一个双向的One2Many关系。当我加载" One"超过entitymanager.find()相关对象列表为空。

有什么不对,关系没有填充?

这是我的代码......

" One"

爪哇

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

&#34;很多&#34;

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)
)

爪哇

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

在我的testdata中,我有一个AddedDebtorApproval条目,其中credit_id = 22,而在CreditApproval表中,我还有一个条目,其中credit_id = 22。

当我加载creditApproval实体(&#34; One&#34;)entityManager.find(CreditApproval.class, bean.getCredit_id());时,与many的关系为null。

更多旁注。

object-relational-mapping.xml就是这样:更新

<?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就是这样:

<?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发出通知,表明已经在映射文件中指定了那些xxxxxApproval类(这是真的)。​​

在Project Explorer的Eclipse中,当我打开JPA实体时,CreditApproval显示一个带有&#34;&#34;的小图标。在与AddedDebtors的关系面前。在AddedDebtorsApproval实体内的另一个方向的图标显示了Many2One关系。 enter image description here

2 个答案:

答案 0 :(得分:4)

您的映射文件显示它正在将CreditApproval的访问类型更改为PROPERTY,而集合的注释位于该字段上。这可能会导致它忽略注释,而是使用默认关系。修复访问以匹配您要使用的注释,以便它们不会发生冲突。

答案 1 :(得分:1)

为什么使用object-relational-mapping.xml文件?您正在通过注释和xml配置混合配置。您是否尝试删除此行persistence.xml文件?

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