使用 Hibernate 在 J2EE 应用程序中存储全局数据的最佳实践
我正在寻找使用 Hibernate 存储 Java EE 应用程序全局数据的最佳解决方案。它将由键值对组成。示例:
projectStarted = "10-11-11"
developerNumber = 3
teamLeader = "John"
如您所见,所有这些条目都有不同的类型。
现在我看到两个选项:
1)创建 GlobalData 实体。它的每个字段将表示为表中唯一的列,并将包含唯一的设置。这样我就不会遇到类型转换的问题,但我想避免它,以防存在大量设置。
2) 创建设置实体。每个它都包含两个字段:键(主键)和值,并将在表中表示为唯一记录。这是更好的解决方案,但在我看来,我会得到很多类型转换,因为设置可以是任何类型。
所以基本上,我正在寻找实现第二种解决方案的方法,而不会因为不同类型而遇到很多麻烦。有人可以帮助我吗?
谢谢。
编辑 1。
是的,谢谢克里斯蒂安。刚刚有类似的想法。
如果我有设置实体,它会像:
@Entity
@Table(name = "settings")
public class Setting {
@Column
private String key;
@Column
private String value;
@Column
private String converterClassFullName; //example by.lugovsky.MyConverter
//Getters, setters
}
和 GlobalData 类。
public class GlobalData {
private Date projectStarted;
private int developerNumber;
private String teamLeader;
Set<Setting> settings;
//Getters and setters for all, except settings.
}
所以基本上我的想法是在持久/更新/等之前转换设置实体。我可以在我的 DAO 中执行此操作,但我想知道是否也可以使用 @Entity 注释来注释 GlobalData 类而不创建新表。这样我可以将 OneToMany 注释设置为设置的集合并在内部 @PrePersist 等方法中执行转换。
Hibernate 允许我这样做吗?
再次感谢
I'm looking for the best solution to store Java EE application's global data using Hibernate. It will consist of key value pairs. Example:
projectStarted = "10-11-11"
developerNumber = 3
teamLeader = "John"
As you see, all of this entries have different types.
For now I see two options:
1) Create GlobalData entity. Each field of it will be represented as unique column in the table and will contain unique setting. This way I have no problems with type casting, but I would like to avoid it in case where there will be big amount of settings.
2) Create Setting entity. Each of it will contain two fields: key(Primary key) and value and will be represented as unique record in the table. This is preferable solution, but It's seems to me that I will get a lot of type casting, because settings can be any type.
So basically, I'm looking for the way to implement second solution without getting a lot of troubles from different types. Can anybody help me?
Thanks.
Edit 1.
Yeah, thanks Christian. Just got similar idea.
What if I will have Settings entity, which will be like:
@Entity
@Table(name = "settings")
public class Setting {
@Column
private String key;
@Column
private String value;
@Column
private String converterClassFullName; //example by.lugovsky.MyConverter
//Getters, setters
}
And GlobalData class.
public class GlobalData {
private Date projectStarted;
private int developerNumber;
private String teamLeader;
Set<Setting> settings;
//Getters and setters for all, except settings.
}
So basically my idea is to convert Setting entity before persisting/updating/ etc. I can do this in my DAO, but I was wondering, if I could annotate GlobalData class with @Entity annotation as well without creating new table. This way I can set OneToMany annotation to Setting's set and Perform conversions in the internal @PrePersist etc. methods.
Will Hibernate allow me to do this?
Thanks again
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将转换器类存储到数据库中,并让它在使用该值之前通过属性的给定转换器运行。 JSF 提供 Converter API:
如果您有一个模式
,名称为:字符串
值:字符串
转换器:类,
那么您可以执行以下操作:
为了更酷,您还可以在类中定义一个不会被持久化的字段,您可以使用它将转换后的值保存在对象内。
You could store a Converter-Class into the db and the let it run through the given converter for a property before using the value. JSF offers Converter API:
If you have a schema with
name: String
value: String
converter: Class
then you could do something like this:
For even more coolness you could also define a field in the class which will not be persisted which you could use to hold the converted value within the object.