2010-04-01 18 views
0

我需要使用eclipselink以有序方式讀取複雜模型。順序是mandantory,因爲它是一個巨大的數據庫,我想在jface tableview中輸出一小部分數據庫。嘗試在加載/查詢線程中重新排序需要太長的時間,並且在LabelProvider中對其進行排序會阻塞UI線程太多時間,所以我想如果Eclipselink可以這樣使用,數據庫會對它進行排序,那可能會給我我需要的表現。不幸的是,對象模型不能:-(Eclipselink以有序方式讀取複雜對象模型

該模型被改變爲這樣的:

@SuppressWarnings("serial") 
@Entity 
public class Thing implements Serializable { 
    @Id 
    @GeneratedValue(strategy = GenerationType.TABLE) 
    private int id; 
    private String name; 
    @OneToMany(cascade=CascadeType.ALL) 
    @PrivateOwned 
    private List<Property> properties = new ArrayList<Property>(); 
    ... 
    // getter and setter following here 
} 
public class Property implements Serializable { 
    @Id 
    @GeneratedValue(strategy = GenerationType.TABLE) 
    private int id; 

    @OneToOne 
    private Item item; 

    private String value; 
    ... 
    // getter and setter following here 
} 
public class Item implements Serializable { 
    @Id 
    @GeneratedValue(strategy = GenerationType.TABLE) 
    private int id; 
    private String name; 
    .... 
    // getter and setter following here  
} 
// Code end 

在表中查看y軸或多或少與查詢

Query q = em.createQuery("SELECT m FROM Thing m ORDER BY m.name ASC"); 
創建

使用Thing對象中的「name」屬性作爲標籤

在表格視圖中,x軸或多或少地通過查詢創建

Query q = em.createQuery("SELECT m FROM Item m ORDER BY m.name ASC"); 

使用Item對象的「name」屬性作爲標籤。

每個單元具有值

Things.getProperties().get[x].getValue() 

不幸的是列表中的「屬性」不是有序的,這樣的細胞和值x軸列編號的組合(x)是不一定正確。因此,我需要以和我訂購X軸標籤相同的方式來排列列表「屬性」。

而這正是我不知道它是如何完成的。因此,查詢Thing對象應返回「Item」對象的列表「屬性」「Order by Name ASC」。我的想法就像查詢兩個JOIN一樣。與財產和項目,但不知何故,我無法得到它的工作。

謝謝你的幫助和你的想法來解決這個謎。

回答

1

實際上,可能是這個問題的答案的其他問題可以幫助你: Defining the order of a list

我想你可能要爲每一件事情用另一查詢得到的屬性列表命令由item.name 。

喜歡的東西:

SELECT p FROM Property p WHERE p.thing = ?1 ORDER BY p.item.name 
0

嘗試使用@OrderBy JPA註釋。像

@OrderBy('name ASC') 
@OneToMany(cascade=CascadeType.ALL) 
private List<Property> properties = new ArrayList<Property>(); 
+0

謝謝! 不幸的是我得到這個錯誤: 「內部異常:異常[EclipseLink-7217](Eclipse持久性服務 - 2.0.1.v20100213-r6600):org.eclipse.persistence.exceptions.ValidationException 異常說明:按值排序[名稱],在實體[class Thing]的元素[properties]中指定,是無效的。目標實體[class Property]中不存在具有該名稱的屬性或字段。 看來eclipselink不知道Item.name中的名稱字段呢。 – Raven 2010-04-01 13:01:10

+0

但是我將@OrderBy添加到Item.name,這沒有幫助。 Eclipselink仍然以無序方式讀取列表。:-( – Raven 2010-04-01 13:07:39

+0

對不起,誤解了這個問題,EclipseLink支持@OrderBy,但對你的情況沒有幫助。 – lexicore 2010-04-01 13:08:20