此問題令我發狂。我有一個雙向One2Many關係。當我加載entitymanager.find()
上的「One」時,相關對象列表爲空。JPA OneToMany關係未填充/填充/仍爲空
有什麼不對,關係沒有填充?
這裏是我的代碼...
「一」
的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;
「多」
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;
在我TESTDATA我有credit_id = 22,並在CreditApproval表我也credit_id一入一次AddedDebtorApproval進入= 22
當我加載creditApproval實體(「One」)entityManager.find(CreditApproval.class, bean.getCredit_id());
與many的關係爲null。
一些附註。
對象 - 關係-mapping.xml IST這樣的:更新
<?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的Project Explorer中,當我打開JPA實體時,CreditApproval顯示一個帶有「?」的小圖標。在與AddedDebtors的關係之前。另一個方向的圖標位於AddedDebtorsApproval實體內部,顯示了一個Many2One關係。
當您創建AddedDebtorApproval實體並將CreditApproval與其關聯時,是否還在addedDebtors集合中設置了AddedDebtorApproval併合並了CreditApproval?如果刷新CreditApproval然後訪問集合,會發生什麼情況? – Chris