映射FunctionalJava选项< Type>与休眠

编程入门 行业动态 更新时间:2024-10-25 08:19:26
本文介绍了映射FunctionalJava选项< Type>与休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个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 ) 您可以直接映射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选项&lt; Type&gt;与休眠

本文发布于:2023-11-09 06:08:55,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项   amp   FunctionalJava   gt   Type

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!