2010-06-23 71 views
2

使用Hibernate存儲應用程序設置的最佳/最漂亮/最靈活的方式是什麼?使用Hibernate持久保存應用程序設置

單行表是要走的路嗎,還是有更好的辦法?將額外設置存儲在同一地點/表格中,但在應用程序域之外的功能會很好。

我已經嘗試了一個鍵/值表,例如:

Key  | Value 
------------------------- 
width | 20px 
height | 40px 
showall | true 
etc. 

但這似乎並不適合Hibernate的非常好。

任何想法?

+0

是否真的需要將這些存儲在SQL數據庫中?你是否會加入這些數據與其他數據,從其他應用程序查詢它等?或者它只是存儲它最方便的地方? – 2010-06-23 19:51:17

+0

在大多數情況下,我會說不。但在這種情況下,我正在處理兩個不同的應用程序(在不同的機器上),這兩個應用程序都在同一個數據庫上工作。 – 2010-06-23 20:45:39

回答

1

其實我已經實現了這個在C#中的鍵/值對與NHibernate。該表與您僅用一個鍵/值配對所描述的相同。當我想獲取/設置一個值時,我的持久層接受「鍵」(作爲一個枚舉)並返回一個ApplicationSetting對象。這個對象實際上只是鍵/值對的包裝。

簡單的例子(C#):

class ApplicationSetting 
{ 
    private string theApplicationSettingString = string.Empty; 

    /// <summary> 
    /// The name of the setting. 
    /// </summary> 
    public virtual ApplicationSettingKey SettingName 
    { 
    //I use enumerations here so that I'm guaranteed to get a key I 
    //already know about and not a random string. 
    get 
    { 
     return (ApplicationSettingKey)Enum.Parse(
      typeof(ApplicationSettingKey), theApplicationSettingString); 
    } 
    set 
    { 
     theApplicationSettingString = 
      Enum.GetName(typeof(ApplicationSettingKey), value); 
    } 
    } 

    /// <summary> 
    /// The value of the setting. 
    /// </summary> 
    public virtual string SettingValue 
    { 
    get; 
    set; 
    } 
} 



/// <summary> 
/// Enumeration for all application settings. 
/// </summary> 
public enum ApplicationSettingKey 
{ 
    WIDTH, 
    HEIGHT, 
    SHOWALL 
} 


/// <summary> 
/// Returns the ApplicationSetting from the database that corresponds to 
/// the passed in key/name. 
/// </summary> 
/// <param name="aKey">The key/name of the Application setting to 
/// retrieve.</param> 
/// <returns>The ApplicationSetting Definition that with the corresponding 
/// application setting key/name.</returns> 
public ApplicationSetting GetApplicationSettingByKey(ApplicationSettingKey aKey) 
{ 
    const string propertyName = "theApplicationSettingString"; 
    string key = Enum.GetName(typeof(ApplicationSettingKey), aKey); 
    DetachedCriteria criteria = DetachedCriteria.For<ApplicationSetting>(); 
    criteria.Add(Restrictions.Eq(propertyName, key)); 
    return FindFirst(criteria); 
} 
+0

我最終使用了這種方法,但在做出決定之前,我建議任何人讀這篇文章以評估其他所有提議的解決方案以及任何其他現有技術。 – 2010-06-26 12:21:18

+0

@Kristoffer我完全同意別人應該看看所有的解決方案。這個解決方案是爲我的項目選擇的,因爲它適合於項目的其他部分。 – brainimus 2010-06-26 21:47:20

+0

將任何類型保存爲字符串是容易出錯的方法。使用ORM的能力來正確映射類型會更好。所以我建議爲每個設置鍵創建適當類型的單獨列。顯然你應該防止創建多個設置行。 – Sneg 2016-07-20 09:11:18

2

我建議你基於值的使用方式(類似於你將如何組織任何OO模型)組織你的領域模型,然後堅持這些實體。對於您的示例,您將擁有一個Window對象,其中包含width,heightshowall屬性,然後將其作爲單個行/對象持久保存到數據庫中。

你可以有以下的(假設情況)是這樣的:

id | layoutType | width | height | images 
----------------------------------------------- 
0 | widescreen | 1000 | 500 | true 
1 | normal  | 600 | 500 | true 
2 | mobile  | 320 | 480 | false 
2

要嚴格回答你的問題,你可以地圖休眠鍵/值表:

@Entity 
public class Settings { 
    @Id 
    private String key; 
    private String value; 

    // getters, setters, etc 
} 

但你是否真的需要將其存儲在數據庫中或者不存在,我想我會選擇Commons Configuration而不是休眠(或者 只是 Properties對象,如果您不需要在數據庫中持續設置 @ring提供的Preferences API)。

+0

相反,'基於java.util.Preferences'的API – 2010-06-23 20:19:44

+0

@ring好點,回答更新,謝謝。 – 2010-06-23 20:29:09