-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Unable to find entity with id ...
PostPosted: Mon Sep 24, 2007 3:24 pm 
Newbie

Joined: Mon Sep 24, 2007 2:55 pm
Posts: 13
I'm using Hibernate version 3.2.4.SP1_CP01-brew within JBoss-4.2.2.GA (build from source).
The datamodel is given for many things by a legacy application.
Numeric foreign key fields are not empty (NULL) but filled with a 0 (zero) which cannot be changed.
Accessing this data gives exceptions like the following:
Code:
Caused by: javax.persistence.EntityNotFoundException: Unable to find biz.mbisoftware.fn.ejb.entity.MbiAdres with id [0]
        at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:107)
        at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:79)
        at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:68)
        at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
        at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:166)
        at biz.mbisoftware.fn.ejb.entity.MbiAdres_$$_javassist_1997.getAdressNr(MbiAdres_$$_javassist_1997.java)


The relation field in MbiAdres (uni-directional):
Code:
   /** Relation with MbiAdres. */
   @ManyToOne(fetch=FetchType.LAZY)
   @JoinColumns({
      @JoinColumn(name="d_adress_nr",referencedColumnName="adress_nr")
   })
   private MbiAdres mbiAdresDAdressNr;


Is there a way to configure hibernate to ignore the EntityNotFound and simply set the field to null?
Or better, a way to advise Hibernate to treat a zero within a FK field like NULL as this is not a legal FK/PK value?


Top
 Profile  
 
 Post subject: Re: Unable to find entity with id ...
PostPosted: Fri Jan 03, 2014 5:15 am 
Newbie

Joined: Thu May 20, 2004 4:51 am
Posts: 4
Code:
@NotFound(action=NotFoundAction.IGNORE)

When this annotation is added to a LAZY fetched @ManyToOne, Hibernate will issue two SQL queries: one for the main object, and one for the @ManyToOne. So you loose the LAZY behavior, see http://opensource.atlassian.com/projects/hibernate/browse/HHH-2753

But you can have @ManyToOne with LAZY loading AND without throwing EntityNotFoundException with the following code:

Code:
public MyManyToOneEntity getMyManyToOneEntityOrNull() {
  try {
    // force fetching the entity (will raise an EntityNotFoundException if not found)
    getMyManyToOneEntity().toString(); // or whatever method which force a fetch

    return getMyManyToOneEntity();
  } catch (EntityNotFoundException e) {
    return null;
  }
}




This way, Hibernate will issue one query for the main object, and one other query for the MyManyToOneEntity but only when the getMyManyToOneEntityOrNull() method is called


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.