Re: [ebean] EntityNotFoundException - How to handle broken relationships?

855 views
Skip to first unread message

Rob Bygrave

unread,
Dec 9, 2012, 3:07:38 PM12/9/12
to eb...@googlegroups.com
To clarify ... does that mean you can't tell the difference between the entity not there and a property being null?

Perhaps you could explain more fully the behavior and why it is useful behavior.  Is there Foreign Key / referential integrity in place or if the row deleted after the bean with the reference was initially loaded?

Thanks, Rob.


On 9 December 2012 23:30, Yves H. <yve...@gmail.com> wrote:
Hello,

I'm currently testing the Play! Framework which has EBean built-in. Now I'm facing the problem, that my entity has a reference to an entity which doesn't exist anymore. For example: I have a column "id_parent" with the value "2" but in the database there isn't a row with id "2". And that's why I'm getting this error: 

[EntityNotFoundException: Bean has been deleted - lazy loading failed]

But when I try to access a reference which doesn't exist anymore, I expect the result being null. Is there a way to accomplish this? In Hibernate there's a @NotFound annotation which does exactly that. 

Thanks for your help!

Yves H.

unread,
Dec 11, 2012, 1:46:15 AM12/11/12
to eb...@googlegroups.com
Thank you for your answer. I try to explain my problem a little bit more in detail. I have a third-party CMS for the back-end and now I'm want to build a front-end with EBean. The problem is, that the all references in the database doesn't have foreign keys. So it could happen, that table A references to table B, with an ID that doesn't exist anymore in table B. Since I can't just set such references to null, because the CMS needs those for its versioning system, I have to handle those broken references in my front-end application.

So if I try to access a row from table B via the reference in table A, I expect EBean returning null, instead of an EntityNotFoundException. That's also, for example, how the EntityFramework in .Net would handle this. In Hibernate there's a @NotFound annotation, which does the same. Now my question, is there anything similar in EBean?

Thanks

Sorra Dawnstar

unread,
May 8, 2016, 10:04:39 AM5/8/16
to Ebean ORM
Hi Rob,

Suppose there is a blogging application. Blog has a property @ManyToOne author pointing to User. If the user is soft-deleted, EntityNotFoundException causes the blog fails to render, and even the list of all blogs fails, too.
I want the not-found entity be ignored as null (like what Hibernate's @NotFound does) or assigned to a default object, instead of failing. (Escape from lazyLoadFailure)

To conclude, I want to treat that property like an Optional container, just like an empty-able Collection.

How do you think?

在 2012年12月10日星期一 UTC+8上午4:07:38,Rob Bygrave写道:

Rob Bygrave

unread,
May 8, 2016, 5:42:09 PM5/8/16
to ebean@googlegroups
Soft deleted rows are somewhat intended to be "deleted" and not "inactive" per say.  What that means is that by default the deleted = false filters out soft deleted rows and for the case of joins means that those joins are similarly filtered out.

The intention is that when you do NOT want that to be the case you actually use Query.setIncludeSoftDeletes( ).

As I understand this use case I am not convinced that having a @NotFound is a good idea and instead you go from one issue to a different issue.  If the Blog posts do not have @SoftDelete but they related to something that does then I would say your best option is to understand what you need and use Query.setIncludeSoftDeletes( ) and get exactly the query you need (understanding that an associated joined User/author my be soft deleted and that you can tell that by reading the deleted property and you also have full control over the query at that point).  You can manually add predicates to the query then as needed.

Can you describe your use case more specifically with some explanation as to why you can't use setIncludeSoftDeletes() to achieve what you need in this case?


Thanks, Rob.


--

---
You received this message because you are subscribed to the Google Groups "Ebean ORM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ebean+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sorra Dawnstar

unread,
May 9, 2016, 3:48:34 AM5/9/16
to Ebean ORM
This does not work for Model methods. And even if I query with setIncludeSoftDeletes(), I need to iterate over the results to hide the deleted data, which can be more complex for nested case:  blog.author.profile (if author has association to deleted Profile)

I think it's conceptually OK to tolerate a missing single-value association if intentional (you can treat it like a many-value association, which may be empty if elements are deleted).

I can think of three approaches to tolerate a missing single-value association:

1. Ebean built-in support
2. Declare the optional property as a custom wrapper (e.g. Lazy<User?> in Kotlin), instead of a @ManyToOne bean ref (in this way I can only use the default EbeanServer)
3. Don't use @SoftDelete. Snapshot and archive the bean which I intend to delete, then clear all its fields and mark ad-hoc deleted flag (this is a turnkey solution)

在 2016年5月9日星期一 UTC+8上午5:42:09,Rob Bygrave写道:

Rob Bygrave

unread,
May 9, 2016, 4:15:17 PM5/9/16
to ebean@googlegroups
This does not work for Model methods.

What does this mean?

Sorra Dawnstar

unread,
May 9, 2016, 10:47:40 PM5/9/16
to eb...@googlegroups.com
setIncludeSoftDeletes() is Query's method. Model convenient methods have no setIncludeSoftDeletes (I guess)

2016-05-10 4:15 GMT+08:00 Rob Bygrave <robin....@gmail.com>:
This does not work for Model methods.

What does this mean?

--

---
You received this message because you are subscribed to a topic in the Google Groups "Ebean ORM" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ebean/4lU_ZKcrX0k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ebean+un...@googlegroups.com.

Ben

unread,
May 19, 2021, 6:06:37 AM5/19/21
to Ebean ORM
Hi Rob,

I see some value in having a "NotFound" annotation or have this part of EBean be extensible.
Example: Let's say there is parent-child OneToOne relationship, the parentID being on the child, but the parents and children are external data that get ingested separately, e.g. a child might exist without a parent.
However, we would like to have the object graph remain navigable, rather than considering the child invalid.
Currently, workarounds are:
- use two entities: ChildWithoutParent and ChildWithParent
- always eagerly fetch this association, which will null it if it's not there
But it would be easier to manage if EBean did allow this scenario.

Thanks,
Ben
Reply all
Reply to author
Forward
0 new messages