2

I have two Entities Person and PersonImage:

@Entity
public class Person {

....

    @OneToMany(mappedBy = "person", cascade = { CascadeType.PERSIST,
            CascadeType.REMOVE }, orphanRemoval = true, fetch = FetchType.LAZY)
    private List<PersonImage> images;

....
}

@Entity
public class PersonImage {

....

    @Lob
    private byte[] content;

    @ManyToOne
    private Person person;
....

}

The problem is that when I insert a Person (without any image attached to it), before Insert the images list is null. But after insert the list is an empty IndirectList instance. I can see while debugging that it is changed from null to {[]}.

Why is this so? I would like it to be either a null or an Uninitialized IndirectList (which should look like {IndirectList: not instantiated} while debugging). How can I achieve this?

Jehan Zeb
  • 155
  • 12
  • 2
    For a JPA implementation to retain knowledge of whether a collection is empty or null it would have to have a column in the table to store that info in. I doubt that EclipseLink supports that, so presumably just instantiates an empty list – Neil Stockton Aug 28 '15 at 08:53
  • 1
    @NeilStockton AFAIK a JPA implementation should assume any lazy collection to be uninitialized and upon access it would do a query to the referenced table. So I'd assume the list to be empty initially but also to be marked as uninitialized as well - or am I mistaken? – Thomas Aug 28 '15 at 09:14
  • 3
    You're referring to how Hibernate would do it, and is incorrect to assume that as a rule. Other implementations use bytecode enhancement by default and hence don't have uninitialised proxies. How EclipseLink does it by default I've no idea. – Neil Stockton Aug 28 '15 at 09:32
  • @NeilStockton hmm, I'm mostly working with Hibernate, but I didn't actually assume that it works the same way in EclipseLink. Just from a logical point of view, a JPA implementation that doesn't know whether a lazy collection has any elements or not should have _some_ means to detect that (I'm pretty sure this is part of the JPA spec) - and in fact EclipseLink seems to have something similar since the debugger shows different string represenations. – Thomas Sep 03 '15 at 09:06
  • knowing whether it has some elements or not is not the issue. Ok, so its got no elements. That doesn't say is it null or is it an empty list ... back to my first comment. – Neil Stockton Sep 03 '15 at 10:02

1 Answers1

0

Guess that you see org.eclipse.persistence.indirection.IndirectList instance which is used internally to represent this kind of 1:N relations. It holds Vector delegate instance internally and both IndirectList and Vector are instantiated when persist is called. It's default EclipseLink behaviour.

Semantics of null value and emty list in 1:N relationship is the same: There are no PersonImage instances asociated with Person. Is there some serious reason for you to distinguish null from an empty list? If so, you have to keep it in a separate attribute.

Tomas Kraus
  • 466
  • 2
  • 6