这个问题让我发疯。我有一个双向 One2Many 关系。当我在相关对象列表上加载“一个”时entitymanager.find()
为空。
有什么问题,关系没有填充?
这是我的代码...
唯一的那个”
爪哇
@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;
“许多”
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;
在我的测试数据中,我有一个 Credit_id = 22 的 AdditionalDebtorApproval 条目,在 CreditApproval 表中,我还有一个 credit_id = 22 的条目。
当我加载 creditApproval 实体(“一个”)entityManager.find(CreditApproval.class, bean.getCredit_id());
时,与许多的关系为空。
还有一些旁注。
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 类已经在映射文件中指定(这是真的)。
在项目资源管理器的 Eclipse 中,当我打开 JPA 实体时,CreditApproval 显示一个带有“?”的小图标。在与 AdditionalDebtors 的关系之前。另一个方向的图标,在 AdditionalDebtorsApproval 实体内,显示了 Many2One 关系。