映射FunctionalJava选项< Type>与休眠 [英] Mapping a FunctionalJava Option<Type> with Hibernate
问题描述
我有一个hibernate-mapped Java对象, JKL
,它充满了一堆正常的hibernate-mappable字段(比如字符串和整数)。
我为它添加了一个新的嵌入字段(它位于同一个表中 - 不是映射), asdf
,这是一个 fj.data.Option< ASDF>
。我已经提出了一个选项来说明这个字段实际上可能不包含任何内容(而不是每次访问时都必须处理 null
)。
如何在我的 JKL.hbm.xml
文件中设置映射?我希望hibernate自动将数据库中的 null
转换为 fj的
当它检索对象。它还应该将 none
.data.Option< ASDF> ASDF
的非空实例转换为某些
的 fj.data 。选项< ASDF>
。还有什么我不得不做的吗?谢谢。
我会建议在访问器中引入FunctionalJava的 Option
(getter和setter),同时让Hibernate处理一个简单的java字段,该字段允许为 null
。
例如,对于可选的整数
字段:
// SQL
CREATE TABLE`JKL`(
`JKL_ID` INTEGER PRIMARY KEY,
`MY_FIELD` INTEGER DEFAULT NULL
)
$ b $ 您可以直接映射Hibernate专用字段:
// Java
@Column(nullable = true)
私人整数myField;
然后,您可以在访问器中引入 Option
border:
// Java
public fj.data.Option< Integer> getMyField(){
return fj.data.Option.fromNull(myField);
}
public void setMyField(fj.data.Option< Integer> value){
myField = value.toNull();
}
这是否符合您的需求?
I have a hibernate-mapped Java object, JKL
, which is full of a bunch of normal hibernate-mappable fields (like strings and integers).
I'm added a new embedded field to it (which lives in the same table -- not a mapping), asdf
, which is a fj.data.Option<ASDF>
. I've made it an option to make it clear that this field may not actually contain anything (as opposed to having to handle null
every time I access it).
How do I set up the mapping in my JKL.hbm.xml
file? I'd like hibernate to automatically convert a null
in the database to a none
of fj.data.Option<ASDF>
when it retrieves the object. It should also convert a non-null instance of ASDF
to a some
of fj.data.Option<ASDF>
. Is there any other trickery that I have to do? Thank you.
I would suggest introducing FunctionalJava's Option
in the accessors (getter and setter), while leaving Hibernate to handle a simple java field which is allowed to be null
.
For example, for an optional Integer
field:
// SQL
CREATE TABLE `JKL` (
`JKL_ID` INTEGER PRIMARY KEY,
`MY_FIELD` INTEGER DEFAULT NULL
)
You can map a Hibernate private field directly:
// Java
@Column(nullable = true)
private Integer myField;
You could then introduce Option
at the accessor boundary:
// Java
public fj.data.Option<Integer> getMyField() {
return fj.data.Option.fromNull(myField);
}
public void setMyField(fj.data.Option<Integer> value) {
myField = value.toNull();
}
Does that work for your needs?
这篇关于映射FunctionalJava选项< Type>与休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!