问题描述
我有一个旧数据库表,为简单起见,看起来像:
表格地址{
varchar line1
varchar line2
varchar line3
varchar(1)deliveryline
}
对 deliveryline 有一个检查约束,保证它具有值'1,'2'或'3'。
这看起来很适合在hibernate中进行枚举。我有一个像这样的代表地址表的实体:
public class Address {
String line1;
String line2;
String line3;
DeliveryLine deliveryLine;
$ b我通常使用 @Enumerated(EnumType.STRING) 在映射枚举时,但是这种策略在这里不起作用。例如:
public enum DeliveryLine {
1,2,3
}
这不能编译,因为数据库(1,2,3)不是有效的Java标识符。
有没有一种直接的方法可以在hibernate中强制映射?
GenericEnumUserType 看看 GenericEnumUserType .htmlrel =nofollow noreferrer> hibernate.org (在灵活的解决方案下)
如果您使用Hibernate 4,使用修改后的版本 p>
I have a legacy database table that, for simplicity sake looks like:
table address{ varchar line1 varchar line2 varchar line3 varchar(1) deliveryline }There is a check constraint on deliveryline guaranteeing it has the values '1,'2', or '3'.
This seems like a good candidate for enumeration in hibernate. I have an entity that looks like this representing the Address table:
public class Address{ String line1; String line2; String line3; DeliveryLine deliveryLine; }I normally use @Enumerated(EnumType.STRING) on when mapping enums, but that strategy does not work here. For example:
public enum DeliveryLine { 1,2,3} This does not compile since the valid values in the database (1,2,3) are not valid Java Identifiers.
Is there a straightforward way to coerce this mapping in hibernate?
解决方案Look at GenericEnumUserType described at hibernate.org (Under "Flexible solution")
If you're using Hibernate 4 you'll have to use a modified version as discussed here
这篇关于Hibernate枚举映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!