可能拥有不可变的JPA实体? [英] Possible to have immutable JPA entities?

查看:100
本文介绍了可能拥有不可变的JPA实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的hibernate项目中,实体使用java bean模式进行编码。我们的代码中有很多地方有人忘记了设置mutator,并且由于NOT NULL字段导致异常。



是否有人使用构建器构造它们的实体或使它们不可变?



我试图找到一个有效的模式,不是java bean模式的风格。



谢谢

解决方案

如果你让你的bean不可变,那么你必须使用字段级别的访问权限,并且这会带来一系列问题,详细讨论这里。我们采取的方法是让建造者/工厂为我们执行/验证必要性等规则。


In our hibernate project, the entities are coded using the java beans pattern. There's quite a few spots in our code where someone has forgotten a to set a mutator and we get an exception due to a NOT NULL field.

Is anyone using a builder to construct their entities or making them immutable?

I'm trying to find an effective pattern that is not in the style of the java beans pattern.

Thanks

解决方案

If you make your beans immutable then you have to use field level access and this comes with its own set of problems as discussed thoroughly here. The approach we took is to have a Builder/Factory enforcing/validating the requiredness etc rules for us.

这篇关于可能拥有不可变的JPA实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆