如何优雅地处理Target Unreachable

发布于 2024-11-06 11:06:07 字数 412 浏览 0 评论 0原文

我有两个关于常见的目标无法到达异常的问题。 处理它的最佳做法是什么,例如您有: 国家有城市,城市有街道。 - 你是否放入 Country 的构造函数 new City() 和 City 的构造函数 new Street() (这样你就可以以某种方式将它们放在一个集中的地方,但总是制作你可能不需要的对象) 或者您在代码中需要它们的各个位置初始化对象? (遍布您的代码) - 如果用户没有输入 Street 的任何内容,以防止在数据库中插入空白行 你把街道放回零。将其恢复为 null 的最佳位置在哪里? (假设你有 Cascade.ALL 或扩展上下文,否则如果你知道它是空的,你就不会保存它)

PS:为什么 JSF 不只是实例化它需要的东西,而 Hibernate 不持久化所有持久字段为空的实体? 为了性能还是为什么?再说一遍,数据库中只有 PK 和 FK 的空行是不是很糟糕?

I have 2 questions regarding the so common Target Unreachable Exception.
What's the best practice to handle it, for example you have:
Country has City, City has Street.
- do you put in Country's constructor new City(), and in City's constructor new Street()
(so that you somehow have them into a centralized place, but always make objects which you might not need)
OR you initialize the objects in various places in your code where you need them ? (spread all over your code)
- and if the user doesn't type anything say for Street, in order to prevent the insertion of a blank row in the DB
you put the street back to null. Where's the best place to put it back to null ?
(say you have Cascade.ALL or Extended Context, otherwise you'd just not save it if you knew it's empty)

PS: why isn't JSF just instantiating what it needs, and Hibernate not persisting entities that have all persistent fields empty ?
For performance or why ? Yet again, is it bad to have empty rows in db, just with PK and FKs ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜嗑 2024-11-13 11:06:07

我认为这取决于应用程序中实体之间的关系。在某些情况下,我确实会在构造函数中加载另一个对象的相关实例,但仅限于只有一个实体而没有另一个实体的情况。

一种替代方法是在 getter 中惰性地创建对象:

public class Country {

    private City city;

    public City getCity() {
        if (this.city == null) {
            this.city = new City();
        }

        return this.city;
    }

}

就 PS 问题而言,JSF 不会为您实例化对象——我不确定这是否可取……但是如果您使用惰性 getter 方法,那么您可以有效地使用惰性 getter 方法。得到同样的东西。如果 Hibernate 会持久化一个已实例化的实体,因为它会持久化可持久对象模型的当前状态,如果它没有持久化该实体,它就不会按预期工作。

我通常不担心一些空行,因为我选择使用 Hibernate,因为我知道 ORM 会带来一些小的性能成本。对我来说,享受坚持的抽象仍然是非常值得的。

I think it depends on the relationship between the entities in your app. In some cases I do load a related instance of another object in the constructor but only in the case where you wouldn't have one entity without the other.

One alternative is to create the objects lazily in the getter:

public class Country {

    private City city;

    public City getCity() {
        if (this.city == null) {
            this.city = new City();
        }

        return this.city;
    }

}

As far as the PS questions JSF doesn't instantiate objects for you -- I'm not sure that would be desirable ... but if you use the lazy getter approach you effectively get the same thing. Hibernate persists an entity if it has been instantiated since it persists the current state of the persistable object model and if it didn't persist that entity it wouldn't be working as expected.

I typically don't worry about a few null rows as I choose to use Hibernate knowing that an ORM comes with some small cost in performance. To me it is still well worth it to enjoy the abstraction of persistence.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文