由於我的內部連接,Hiberate正在創建重複的對象。查詢將父表與子表連接起來(父表與父子關係一對多)。Hibernate創建重複對象
數據:
Category (Parent)
ID|Name
1|A
2|B
Testcase (Child)
ID|CategoryID|Name|Run
1|1|A|500
2|1|B|500
3|1|C|500
4|2|D|600
從加入的結果,其中運行= 500
CategoryID|TestcaseID|TestCase Name
1|1|A
1|2|B
1|3|C
從此,我希望Hibernate來創建一個類的對象包含3個測試用例對象的列表。相反,它創建了3個類別對象,每個對象都帶有3個測試用例的正確列表
類別[ID = 1,name = FOO,testCases = [TestCase [ID = 1,name = A,runId = 500],TestCase [ 2,name = B,runId = 500],TestCase [ID = 3,name = C,runId = 500]]]
類別[ID = 1,name = FOO,testCases = [TestCase [名稱= C,runId = 500]]]
類別[ID = 1,名稱= A,runId = 500],TestCase [ID = 2,名稱= B,runId = 500] name = FOO,testCases = [TestCase [ID = 1,name = A,runId = 500],TestCase [ID = 2,name = B,runId = 500],TestCase [ID = 3,name = C,runId = 500 ]]]
型號:
@Entity
@Table(name = "CATEGORY")
public class Category implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "CATEGORYID")
private int ID;
@Column(name = "CATEGORYNAME")
private String name;
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "CATEGORYID")
@Filter(name = "TEST_RUN_ID_FILTER")
private Collection<TestCase> testCases;
}
@Entity
@Table(name = "TESTCASE_NEW")
@FilterDef(name = "TEST_RUN_ID_FILTER", defaultCondition = "TESTRUNID in (:IDS)", parameters = { @ParamDef(name = "IDS", type = "int") })
public class TestCase implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "TESTCASEID")
private int ID;
@Column(name = "TESTCASENAME")
private String name;
@Column(name = "STATUS")
private String status;
@Column(name = "TESTRUNID")
private int testRunId;
}
DAO:
public List<Category> getAllCategoriesForTestRuns(List<Integer> testRunIDs)
{
Session session = getSession();
session.enableFilter("TEST_RUN_ID_FILTER")
.setParameterList("IDS", testRunIDs);
Query query = session.createQuery("select c from Category c inner join c.testCases tc");
List<Category> result = query.list();
return result;
}
我可以,如果我恰克的HQL查詢來選擇不同的,以獲得正確的結果,但我想知道是否有更正確的方式。從我的谷歌搜索中,我嘗試添加@Fetch(FetchMode.SELECT)到類別中的testCase列表,但沒有任何效果。
謝謝!
非常有幫助謝謝。當你將它看作結果集時,它確實有意義!我一直將每個結果對象添加到一個集合中,以濾除總是感覺錯誤的重複。 – JLove 2014-02-05 16:46:06