Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate ObjectNotFoundException Inconsistency
    primarykey
    data
    text
    <p>I have the following one-to-one (optional) relationship between two entities:</p> <pre><code>@Entity @Table(name = "HLTH_RQRMT") public class Requirement { ... @OneToOne(mappedBy = "healthRequirement", cascade = CascadeType.ALL) private HealthRequirementSystemIdentifier requirementSystemId; ... public HealthRequirementSystemIdentifier getRequirementSystemId() { return requirementSystemId; } } @Entity @Table(name = "HLTH_RQRMT_SYS_IDNTFR") public class HealthRequirementSystemIdentifier { @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "Hlth_Rqrmt_Id", nullable = false) private Requirement healthRequirement; } </code></pre> <p>After the requirement is loaded from the database, the identifier is checked:</p> <pre><code>//Load will always return a result (or exception) - works correctly. Requirement requirement = (Requirement) getSession().load(Requirement.class, id); //This is where the exception is thrown if (requirement.getRequirementSystemId() == null) </code></pre> <p>When there is an identifier, the code works fine in all environments. When there is no identifier, it works fine in unit tests and locally (confirmed by testing against the production database). When there is no identifier in production, the following error is thrown:</p> <pre><code>org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [Requirement#250583] at org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:419) at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:154) at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:143) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:174) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190) at Requirement_$$_javassist_66.getRequirementSystemId(Requirement_$$_javassist_66.java) at mypackage.myclass.processResponse(MyServiceImpl.java:244) </code></pre> <p>I can trigger the above error in unit tests by added the property <code>optional=false</code> to the mapping annotation.</p> <p>What could cause the results to be different between unit tests and production? The default value of the <code>optional</code> property is true, which is why it works correctly in unit tests. If I add the property <code>optional=true</code>, it has no impact on the unit tests, but is it likely to fix the issue in production?</p> <p>I am aware of the annotation <code>@NotFound(action=NotFoundAction.IGNORE)</code>, but I'd prefer to understand the cause of the problem before ignoring it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload