实体equals(),hashCode()和toString()。如何正确实施它们? [英] Entities equals(), hashCode() and toString(). How to correctly implement them?

查看:123
本文介绍了实体equals(),hashCode()和toString()。如何正确实施它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行 equals() hashCode() toString()

I'm implementing equals(), hashCode() and toString() of my entities using all the available fields in the bean.

当我尝试比较前端时,我在前端获得了一些惰性init异常平等或当我打印obj状态。这是因为实体中的一些列表可以被延迟初始化。

I'm getting some Lazy init Exception on the frontend when I try to compare the equality or when I print the obj state. That's because some list in the entity can be lazy initialized.

我想知道什么是实现 equals()的正确方法 toString()在一个实体对象上。

I'm wondering what's the correct way to for implementing equals() and toString() on an entity object.

推荐答案

equals() hashCode()应该使用业务关键字 - 即一组唯一标识对象的属性,但不是其自动生成的ID。

equals() and hashCode() should be implemented using a business key - i.e. a set of properties that uniquely identify the object, but are not its auto-generated ID.

位于 toString()中,您可以放置​​任何有趣的信息 - 例如所有字段。

in toString() you can put whatever information is interesting - for example all fields.

使用您的IDE(Eclipse中,NetBeans中的IntelliJ)生成所有这些给你。

Use your IDE (Eclipse, NetBeans, IntelliJ) to generate all these for you.

为了避免 LazyInitializationException中 equals()还是在你的视图(jsp)中,你都可以使用 OpenSes sionInView

In order to avoid LazyInitializationException, no matter whether in equals() or in your view (jsp), you can use OpenSessionInView.

这篇关于实体equals(),hashCode()和toString()。如何正确实施它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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