Hibernate 复杂类型保存
我需要保留一个其属性之一是枚举的对象。 整个结构必须保存在一个表中。 枚举定义了可能的性别
public enum Gender { 男、女 }
主类是
public Person { 字符串名称; 性别 性别; ?
如果这个 Person 类必须保存在单个表中,那么 hbm 映射应该如何
i need to persist an object whose one of the properties is an enum. This whole structure have to be saved in a single table.
The enum defines the possible genders
public enum Gender
{
MALE,FEMALE
}
The main class is
public Person
{
String name;
Gender gender;
}
If this Person class has to be persisted in a single table, how should the hbm mapping be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有提到您使用的是 Java 还是 .Net,但我将为您提供我为 .Net 找到的解决方案,因此您可以根据需要将其转换为 Java。 我发现一篇文章建议使用通用类型,它允许您将枚举声明为表中的文本字段,以便您可以看到表中值的名称。
点击我!
我稍微改变了我的限制列的长度。
这在很多方面对我来说都非常有效。 如果您更改枚举中元素的顺序,它仍然有效,并且还意味着您可以查看该值在数据库中的含义,从而更轻松地编写临时查询! :)
You haven't mentioned if you are using Java or .Net but I will give you the solution that I found for .Net and so you can translate it to Java if need be. I found a post which suggests the use of a generic type which allows you to declare the enum as a field in the table as text so you can see the name of the value in the table.
Click Me!
I changed mine slightly to restrict the length of the column.
This has worked great for me in a number of ways. If you change the order of the elements in the enum, it still works and it also means you can see what the value means in the db making writing ad-hoc queries easier! :)