Java 将枚举映射到带有休眠注释的表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/735732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mapping enum to a table with hibernate annotation
提问by Thierry-Dimitri Roy
I have a table DEAL and a table DEAL_TYPE. I would like to map this code:
我有一个表 DEAL 和一个表 DEAL_TYPE。我想映射此代码:
public class Deal {
DealType type;
}
public enum DealType {
BASE("Base"), EXTRA("Extra");
}
The problem is that the data already exist in the database. And I'm having a hard time mapping the classes to the database.
问题是数据已经存在于数据库中。而且我很难将类映射到数据库。
The database looks something like that:
数据库看起来像这样:
TABLE DEAL {
Long id;
Long typeId;
}
TABLE DEAL_TYPE {
Long id;
String text;
}
I know I could use a simple @OneToMany relationship from deal to deal type, but I would prefer to use an enum. Is this possible?
我知道我可以使用简单的 @OneToMany 关系从交易到交易类型,但我更喜欢使用枚举。这可能吗?
I almost got it working by using a EnumType.ORDINAL type. But unfortunately, my IDs in my deal type table are not sequential, and do not start at 1.
我几乎通过使用 EnumType.ORDINAL 类型让它工作。但不幸的是,我在交易类型表中的 ID 不是连续的,也不是从 1 开始的。
Any suggestions?
有什么建议?
采纳答案by Brandon Yarbrough
Hibernate is kind of terrible at Enums. It's a strange failing of an otherwise pretty good ORM. The "easiest" way to get around it is to declare your Enum a custom hibernate type. Fortunately, Hibernate wrote an example implementation which you can crib verbatim into your app:
Enums 中的 Hibernate 有点糟糕。这是一个原本相当不错的 ORM 的奇怪失败。解决它的“最简单”方法是将 Enum 声明为自定义休眠类型。幸运的是,Hibernate 编写了一个示例实现,您可以将其逐字写入您的应用程序:
http://www.hibernate.org/265.html
http://www.hibernate.org/265.html
They even include instructions on how to use it. This is the pattern I use whenever I end up with the need to persist enums.
它们甚至包括有关如何使用它的说明。每当我最终需要保留枚举时,这就是我使用的模式。
回答by Jeff Mc
Although far far from ideal, my solution to this problem was to use EnumStringType and a denormalized updatable view.
尽管远非理想,但我对这个问题的解决方案是使用 EnumStringType 和非规范化的可更新视图。
回答by raisercostin
I've created a similar class like the one suggested by hibernate only that is configurable and there is no need to create a new type only for this persistence.
我已经创建了一个类似的类,就像 hibernate 建议的那个类一样,它只是可配置的,不需要仅为这种持久性创建一个新类型。
Can be used like
可以像
@Type(type = "ro.raisercostin.hibernate.EnumUserType", parameters = @Parameter(name = "type", value = "DealType"))
DealType dealType;
I added an implementation of ParameterizedType to support the passed parameter.
我添加了 ParameterizedType 的实现来支持传递的参数。
public class EnumUserType implements UserType, ParameterizedType {
private static final int[] SQL_TYPES = { Types.VARCHAR };
private Class clazz = null;
public EnumUserType() {
}
@Override
public void setParameterValues(Properties parameters) {
String className = (String) parameters.get("type");
try {
this.clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Couldn't get the class for name [" + className + "].", e);
}
}
public int[] sqlTypes() {
return SQL_TYPES;
}
public Class returnedClass() {
return clazz;
}
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException,
SQLException {
String name = resultSet.getString(names[0]);
Object result = null;
if (!resultSet.wasNull()) {
result = Enum.valueOf(clazz, name);
}
return result;
}
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException,
SQLException {
if (null == value) {
preparedStatement.setNull(index, Types.VARCHAR);
} else {
preparedStatement.setString(index, ((Enum) value).name());
}
}
public Object deepCopy(Object value) throws HibernateException {
return value;
}
public boolean isMutable() {
return false;
}
public Object assemble(Serializable cached, Object owner) throws HibernateException {
return cached;
}
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value;
}
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return original;
}
public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y) {
return true;
}
if ((null == x) || (null == y)) {
return false;
}
return x.equals(y);
}
}
回答by don_jones
You could annotate the enum with @Entity
and use a custoumn tuplizer to create the instances of the enum with Enum.valueOf
您可以注释枚举@Entity
并使用 custoumn tuplizer 创建枚举的实例Enum.valueOf
The enum declaration then looks like:
枚举声明如下所示:
@Entity
@Table(name = "node_interface_type")
@Tuplizer(impl = EnumTuplizer.class)
public enum Type {
WIRED, WIRELESS, WIRELESS_SENSOR_NODE;
@Id
public String name = toString();
}
And the Tuplizer is:
Tuplizer 是:
public class EnumTuplizer extends PojoEntityTuplizer {
public EnumTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity) {
super(entityMetamodel, mappedEntity);
}
@Override
protected Instantiator buildInstantiator(final PersistentClass persistentClass) {
return new Instantiator() {
@Override
public Object instantiate(Serializable id) {
try {
return Enum.valueOf(
(Class) persistentClass.getClass().getClassLoader().loadClass(persistentClass.getClassName()),
(String) id
);
} catch (ClassNotFoundException e) {
throw new AssertionError(e);
}
}
@Override
public Object instantiate() {
throw new UnsupportedOperationException();
}
@Override
public boolean isInstance(Object object) {
throw new UnsupportedOperationException();
}
};
}
}
回答by xmedeko
If you want to use the entity just read-only, then you can use @Formula
and @Enumerated
. Try something like:
如果您只想使用只读实体,则可以使用@Formula
and @Enumerated
。尝试类似:
@Entity
public class Deal {
@Formula("(select text from DEAL_TYPE dt where dt.id = typeId)")
@Enumerated(EnumType.STRING)
DealType type;
}