2011-11-02 12 views
2

了NetBeans主/詳細表格樣本一些背景顯示可變的PostgreSQL的陣列使用JPA 1.0

我有一個名爲遊戲表具有多個屬性和一個叫體裁遊戲數據庫。 Genres屬性在PostgreSQL中被定義爲一個整數[]。爲了簡單起見,我沒有使用任何外鍵約束,但基本上這個數組中的每個整數都是對流派表中的id屬性的外鍵約束。第一次使用NetBeans主/細節示例表單和Java持久性,並且除了1件事之外它迄今爲止工作得很好。當程序嘗試顯示具有一維整數數組的列時,出現此錯誤。在這個例子中,值是{1,11}。

Exception Description: The object [{1,11}], of class [class org.postgresql.jdbc3.Jdbc3Array], from mapping [oracle.toplink.essentials.mappings.DirectToFieldMapping[genres-->final.public.games.genres]] with descriptor [RelationalDescriptor(finalproject.Games --> [DatabaseTable(final.public.games)])], could not be converted to [class [B]. 
Exception [TOPLINK-3002] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.ConversionException 

我的研究

從我已經能夠閱讀,它看起來像PostgreSQL的陣列需要做的,然後才能顯示在這個模板編輯一些特別的東西。默認情況下,示例表單使用TopLink Essentials(JPA 1.0)作爲其持久性庫,但我也可以使用Hibernate(JPA 1.0)。

以下是需要以某種方式更改的代碼。從Games.java文件:

@Entity 
@Table(name = "games", catalog = "final", schema = "public") 
@NamedQueries({ 
// omitting named queries 
@NamedQuery(name = "Games.findByGenres", query = "SELECT g FROM Games g WHERE g.genres = :genres") 
}) 

public class Games implements Serializable { 
    @Transient 
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this); 
    private static final long serialVersionUID = 1L; 

    // omitting other attributes 

    @Column(name = "genres") 
    private Serializable genres; 

    // omitting constructors and other getters/setters 

    public Serializable getGenres() { 
     return genres; 
    } 

    public void setGenres(Serializable genres) { 
     Serializable oldGenres = this.genres; 
     this.genres = genres; 
     changeSupport.firePropertyChange("genres", oldGenres, genres); 
    } 
} // end class Games 

這裏也有一些可能有我只是不理解解決方案的網站: https://forum.hibernate.org/viewtopic.php?t=946973

http://blog.xebia.com/2009/11/09/understanding-and-writing-hibernate-user-types/

//省略因超鏈接用戶限制

嘗試的解決方案

如果將流派的類型更改爲字符串,我可以獲取要顯示的數據,但它是不可變的,我無法對其進行編輯。這是我改變了這樣做:

@Column(name = "genres") 
    private String genres; 

    public String getGenres() { 
     return genres; 
    } 

    public void setGenres(String genres) { 
     String oldGenres = this.genres; 
     this.genres = genres; 
     changeSupport.firePropertyChange("genres", oldGenres, genres); 
    } 

我還試圖創建與Hibernate(JPA 1.0)使用用戶類型的文件,但不知道發生了什麼不對勁的地方。

我也試圖使用@OneToMany和其他標籤,但這些不起作用可能是因爲我沒有正確使用它們。

我正在尋找的

必須有一個簡單的方法來得到這個數據來顯示,並使其可編輯的,但因爲我是完全新的持久性,我不知道該怎麼做。

回答

0

將您的問題付諸表決。不幸的是,JPA目前不支持PostgreSQL數組。根本的問題是陣列在很多其他數據庫中並不經常使用,因此對它們的過分依賴是PostgreSQL特有的。因此,您可以期望一般的跨數據持久性API通常不會很好地支持它們。 JPA也不例外,目前不支持PostgreSQL數組。

我一直在研究在Java中編寫自己的支持數組的Java持久性API,但它還沒有發生,只會在寫PostgreSQL時纔會發生,並且基於一個非常不同於JPA和朋友的原則。