2012-10-18 103 views
2

我如何在eclipselink中做一個簡單的連接查詢?我想收到ArrayList; 例如,我有兩個實體:Eclipse鏈接簡單連接

public class A implements Serializable { 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Basic(optional = false) 
@Column(name = "id") 
private Long id; 
@Column(name = "value") 
private String value; 

@OneToMany(mappedBy = "aid") 
private Collection<B> bCollection; 

}

public class B implements Serializable { 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Basic(optional = false) 
@Column(name = "id") 
private Long id; 

@JoinColumn(name = "a_id", referencedColumnName = "id") 
@ManyToOne 
private A aid; 

}

我想是這樣執行的查詢:

Select * From A a Join B b ON a.id = b.a_id Where a.value = '1'; 

我這樣做:

EntityManager em = createEntityManager(); 
Query q = em.createQuery("Select a From A a Where a.value = 1"); 
q.setHint("eclipselink.join-fetch", "a.bCollection"); 
Object result = q.getResultList(); 

而且我收到一個對象,它有一個鏈接到B對象(bCollection),它在A對象上有一個鏈接(aid),並且是無限期的。這是正常的嗎?

回答

1

是的。在bidirectional mapping父指孩子和孩子指父母,其正常。但是如果你無法無限地訪問圓形圖,那你就很好。