在处理遗留数据库时,Hibernate 会因丢失行而卡住

发布于 2024-11-03 18:29:44 字数 423 浏览 1 评论 0原文

我正在尝试在旧数据库(仍然有旧 PHP 客户端)上实现休眠,并且遇到了一些问题,因为编写原始应用程序的人不知道他们在做什么。

数据库设置为所有列都不可为空,因此如果没有记录,它们的外键默认为 0。此外,它们的表上没有正确的外键,因此有一些表的 ID 无效。我无法选择更改架构或将相应的列设为空。

这是我从休眠中得到的错误:

Caused by: org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.tv.platform.domain.Program#0]

我想要的是一种优雅的方式来处理这些垃圾,如果行无效或不存在,则该字段将为空,但我没有找到如何解决的问题在文档中处理这个问题。

有什么建议吗?

I am trying to implement hibernate on a legacy database (that still has a legacy PHP client), and am running into some problems because the people who wrote the original app had no idea what they were doing.

The database is set up so that none of the columns are nullable, so they default foreign keys to 0 if there is no record for them. Additionally, they don't have proper foreign keys on the tables so there are a few with invalid IDs. I do not have an option to change the schema or null the appropriate columns.

This is the error I get from hibernate:

Caused by: org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.tv.platform.domain.Program#0]

What I would like is a graceful way to deal with this muck, where the field will just null if the row is invalid or doesn't exist, but I am not having any luck finding how to handle this in the documenation.

Any tips?

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

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

发布评论

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

评论(4

云裳 2024-11-10 18:29:50

注释:
@NotFound( action = NotFoundAction.IGNORE )

正是我想要的。我在这里找到了它:

Hibernate多对一外键默认0

The annotation:
@NotFound( action = NotFoundAction.IGNORE )

Does exactly what I was looking for. I found it through here:

Hibernate Many-To-One Foreign Key Default 0

拥抱我好吗 2024-11-10 18:29:50

我认为 Hibernate 不太适合解决此类问题。 Hibernate 期望表中的记录相互关联,如果只是有时强制执行外键关系,那么 Hibernate 就无法正常工作。我无法想象更改或配置 Hibernate 会很容易
以这种方式行事——知道如何处理破裂的外键关系。

您可以从像 MyBatis SQLMaps 这样的框架中获得更多好处,您可以在其中提供 SQL 语句将数据加载到程序外部的文件中,但框架提供了将 SELECT 语句链接在一起以加载完整对象图的选项。这样您就可以用逻辑来补充 SQL 语句以过滤掉 0 值。

I don't think Hibernate is a good fit for this type of problem. Hibernate expects the records in your tables to relate to each other, and really can't work if the foreign-key relationships are only sometimes enforced. I can't imagine it would be easy to change or configure Hibernate
to behave this way - to know how to deal with broken foreign-key relationships.

You may get more mileage out of a framework like MyBatis SQLMaps, in which you provide the SQL statements to load your data in files external to your program, but the framework provides options for chaining SELECT statements together to load full object graphs. This way you could supplement the SQL statements with logic to filter out the 0 values.

忆梦 2024-11-10 18:29:50

我认为 拦截器 可能会成功。

I think an interceptor might do the trick.

仲春光 2024-11-10 18:29:50

我最近针对具有类似问题的遗留数据库编写了一个网络应用程序。如果您不想使用多个数据访问框架,您仍然可以使用 hibernate。我所做的是使用“createSqlQuery”为棘手部分编写本机 SQL 查询。在某些情况下,我必须将完整的子查询或计算值映射到实体属性,但它确实有效。对于更常规的数据库部分,我仍然可以使用常规 Hibernate 映射和条件/HQL 查询。

另一件事:也许你应该避免使用“不知道他们在做什么”或“搞砸”之类的术语。我知道人们很容易认为前辈留给你的任何代码都是低劣的,但要意识到这些人当时可能已经尽力了。另外,您自己的代码也并非完美无缺,因此要保持一定的谦虚。为什么?其一,如果你没有一直对他们认为知识渊博的前开发人员喋喋不休,那么你周围的人可能会与你相处得更好一些。只是一个提示。

I recently programmed a webapp against a legacy database with similar issues. You can still use hibernate, if you don't want to use multiple data access frameworks. What I did is write out native SQL queries for the tricky parts using 'createSqlQuery'. In some cases I had to map complete subqueries or computed values to an entity property, but it works. For parts of the database that were more regular I could still use regular Hibernate mappings and criteria/HQL queries.

Another thing: maybe you should refrain from using terms like 'no idea what they were doing' or 'muck' a bit. I know it is tempting to view any code left to you by predecessors as inferior, but realize that these people probably did the best they could at the time. Also, your own code is not flawless either, so some humility is in place. Why? For one, people around you might get along with you a bit better if you are not foaming around the mouth all the time about the previous developer(s) who they thought were very knowledgeable. Just a tip.

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