Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Is there a method for checking if an object (Entity or attribute) is loaded?

persistenceUnitUtil.isLoaded(entity, attributeName) can be used to determine if a particular attribute has been loaded.  Is there something about using this method in combination with the value of the field that does not suit your needs?

-Tom

On 09/07/2013 5:19 PM, Andreas Joseph Krogh wrote:
På tirsdag 09. juli 2013 kl. 10:37:47, skrev Andreas Joseph Krogh <andreak@xxxxxxxxxxxx>:
På mandag 08. juli 2013 kl. 15:21:13, skrev Tom Ware <tom.ware@xxxxxxxxxx>:
Hi Andreas,

  The best suggestion I can think of is to write a similar method.  You can leverage the behavior already provided by persistenceUnitUtil.isLoaded(value).  In addition to that, you can do your own check for null.  The additional code you will have to add is EclispeLink specific.  Essentially what you need to do is check whether EclispeLink thinks the class of the object is one that is an Entity. You can do that using our org.eclipse.persistence.jpa.JpaHelper utility.  It provides a set of method that allow you to get at the EclipseLink internals from a JPA class.  One thing you could do is this:

  Session session = JpaHelper.getDatabaseSession(entityManagerFactory);
  if (session.getClassDescriptor(object.getClass()) != null){
      // this indicates this is an Entity/MappedSuperclass//Embeddable
 
Ok, that's usefull, I'll try it. Thanks.
 
After some digging, will the following do?
objectToCheckIfIsInitialized match {
        case ic: IndirectContainer => ic.isInstantiated
        case vhi: ValueHolderInterface => vhi.isInstantiated
        case _ => true
}
The object may be of any type, String, Enum, Integer, Collection, "Entity". After what I figure those 2 interfaces cover *ToOne and *ToMany associations, right?
 
One more thing;
I think I have control over non-JPA objects and *ToMany associations as *ToMany associations always have a value (the IndirectContainer IIUC) which I may test if is instantiated.
 
But, when there's a @ManyToOne mapping then the field-value is null whether it's actually null (not set in DB) or just not initialized yet (because it's not accessed). I need to tell the difference between the two but it seems impossible to check if a @ManyToOne-attribute exists for an instance of an Entity without actually loading it, is there a way? I want to prevent is loading *ToOne associations by the validation-framework if they are not initialized (to skip validation of the actual value of the "other end" itself, if it's annotated by @AssertValid), but want to be able to tell if the value is null (set or not), for giving correct validation-error if @NotNull is set, without loading it, is this possible? Hibernate inserts a proxy for the value if the value exists but is lazy, with a reference to the foreign key so it can tell if there's an object "on the other side" without loading it, hence making it possible to tell if it's null or not without loading the object.
 
I hope I made claer what I'm trying to do...
 
Thanks.
 
--
Andreas Joseph Krogh <andreak@xxxxxxxxxxxx>      mob: +47 909 56 963
Senior Software Developer / CTO - OfficeNet AS - http://www.officenet.no
Public key: http://home.officenet.no/~andreak/public_key.asc
 


_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top