{"action":"created","issue":{"url":"https://api.github.com/repos/ebean-orm/ebean/issues/1382","repository_url":"https://api.github.com/repos/ebean-orm/ebean","labels_url":"https://api.github.com/repos/ebean-orm/ebean/issues/1382/labels{/name}","comments_url":"https://api.github.com/repos/ebean-orm/ebean/issues/1382/comments","events_url":"https://api.github.com/repos/ebean-orm/ebean/issues/1382/events","html_url":"https://github.com/ebean-orm/ebean/issues/1382","id":325380863,"number":1382,"title":"Optional OneToOne with PrimaryKeyJoinColumn fail with EntityNotFoundException","user":{"login":"rPraml","id":5530228,"avatar_url":"https://avatars3.githubusercontent.com/u/5530228?v=4","gravatar_id":"","url":"https://api.github.com/users/rPraml","html_url":"https://github.com/rPraml","followers_url":"https://api.github.com/users/rPraml/followers","following_url":"https://api.github.com/users/rPraml/following{/other_user}","gists_url":"https://api.github.com/users/rPraml/gists{/gist_id}","starred_url":"https://api.github.com/users/rPraml/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rPraml/subscriptions","organizations_url":"https://api.github.com/users/rPraml/orgs","repos_url":"https://api.github.com/users/rPraml/repos","events_url":"https://api.github.com/users/rPraml/events{/privacy}","received_events_url":"https://api.github.com/users/rPraml/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-05-22T16:54:13Z","updated_at":"2018-05-23T11:15:37Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"## Expected behavior\r\n\r\nFetching a OneToOne with optional \"extra\" should set the extra to null if it does not exist\r\n\r\n## Actual behavior\r\n```\r\njavax.persistence.EntityNotFoundException: Lazy loading failed on type:org.tests.model.onetoone.OtoUPrimeExtra id:e4fd0a89-bb1a-4fcc-ae1c-6b9db37081b7 - Bean has been deleted.\r\n\tat io.ebean.bean.EntityBeanIntercept.loadBeanInternal(EntityBeanIntercept.java:846)\r\n\tat io.ebean.bean.EntityBeanIntercept.loadBean(EntityBeanIntercept.java:815)\r\n\tat io.ebean.bean.EntityBeanIntercept.preGetter(EntityBeanIntercept.java:915)\r\n\tat org.tests.model.onetoone.OtoUPrimeExtra._ebean_get_extra(OtoUPrimeExtra.java:6)\r\n\tat org.tests.model.onetoone.OtoUPrimeExtra.getExtra(OtoUPrimeExtra.java:37)\r\n```\t\r\n\r\n\r\n### Steps to reproduce\r\n\r\n```java\r\n  @Test\r\n  public void insertWithoutExtra() {\r\n    String desc = \"\" + System.currentTimeMillis();\r\n    OtoUPrime p1 = new OtoUPrime(\"u\" + desc);\r\n    Ebean.save(p1);\r\n\r\n    Query<OtoUPrime> query = Ebean.find(OtoUPrime.class).setId(p1.getPid());\r\n\r\n    OtoUPrime found = query.findOne();\r\n\r\n    if (found.getExtra() != null) {\r\n      found.getExtra().getExtra(); // fails here, because getExtra should be null\r\n    }\r\n    assertThat(found.getExtra()).isNull();\r\n  }\r\n```\r\n\r\n### Suggestions / ideas how to fix\r\nEither perform a left join when the OtoUPrime bean is fetched (would affect performance) - or on the first `getExtra()` call, check if the bean is a reference bean. This could be done in the preGetter like this:\r\n```java\r\n  public void preGetter(int propertyIndex) {\r\n    preGetterCallback(propertyIndex);\r\n    if (state == STATE_NEW || disableLazyLoad) {\r\n      return;\r\n    }\r\n    if (!isLoadedProperty(propertyIndex)) {\r\n      loadBean(propertyIndex);\r\n    }\r\n    // Quick & Ditry hack to fix that particular problem (may have side effects!)\r\n    Object obj = getOwner()._ebean_getField(propertyIndex);\r\n    if (obj instanceof EntityBean ) { // TODO Check if it is Oto Optional\r\n      if (((EntityBean)obj)._ebean_getIntercept().state == STATE_REFERENCE) {\r\n        try {\r\n          (((EntityBean)obj)._ebean_getIntercept()).loadBean(1);\r\n        } catch (EntityNotFoundException enf) {\r\n          getOwner()._ebean_setField(propertyIndex, null);\r\n        }\r\n      }\r\n    }\r\n    if (nodeUsageCollector != null) {\r\n      nodeUsageCollector.addUsed(getProperty(propertyIndex));\r\n    }\r\n  }\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/ebean-orm/ebean/issues/comments/391310476","html_url":"https://github.com/ebean-orm/ebean/issues/1382#issuecomment-391310476","issue_url":"https://api.github.com/repos/ebean-orm/ebean/issues/1382","id":391310476,"user":{"login":"rbygrave","id":208973,"avatar_url":"https://avatars0.githubusercontent.com/u/208973?v=4","gravatar_id":"","url":"https://api.github.com/users/rbygrave","html_url":"https://github.com/rbygrave","followers_url":"https://api.github.com/users/rbygrave/followers","following_url":"https://api.github.com/users/rbygrave/following{/other_user}","gists_url":"https://api.github.com/users/rbygrave/gists{/gist_id}","starred_url":"https://api.github.com/users/rbygrave/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rbygrave/subscriptions","organizations_url":"https://api.github.com/users/rbygrave/orgs","repos_url":"https://api.github.com/users/rbygrave/repos","events_url":"https://api.github.com/users/rbygrave/events{/privacy}","received_events_url":"https://api.github.com/users/rbygrave/received_events","type":"User","site_admin":false},"created_at":"2018-05-23T11:15:37Z","updated_at":"2018-05-23T11:15:37Z","author_association":"MEMBER","body":"> Maybe I really catch the \"EntityNotFoundException\" in my application\r\n\r\nI think so. I believe it is the nature of `PrimaryKeyJoinColumn` ... there will not be a perfect workaround in Ebean (which doesn't know the context of what is trying to be achieved).  That is, I don't believe we want to _always_ do the left join ... and I believe any 'fix' after that is going to be a bit of a hack.\r\n\r\nThat is, I believe developers need to be aware if they are going to lazy load with `PrimaryKeyJoinColumn` and deal with the possible EntityNotFoundException - I think that will be better than Ebean trying to hide/deal with this issue in some way."}}
{"actor":{"display_login":"rbygrave"}}