2010-06-23 69 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