I am using JPA-Hibernate at the moment and want collections to be empty until i call the associated get(). I have been trying for several days now without any success. The reason i want this is because i use Exterialize (or Serialize) and do not always want the collections to be there when sending the serialized string over to the client.
@Entity
public class Thread implements Externalizable {
static final long serialVersionUID = 9L;
@OneToMany(mappedBy = "parentThread", fetch = FetchType.LAZY)
@LazyCollection(LazyCollectionOption.EXTRA)
public Collection<Reply> getReplies() {
return replies;
}
And here is the Reply model:
@Entity
public class Reply implements Externalizable {
static final long serialVersionUID = 8L;
@ManyToOne
@JoinColumn(name = "threadId", referencedColumnName = "id", nullable = false)
public Thread getParentThread() {
return parentThread;
}
This code is what i use to serialize the model:
public static final String serialize(Serializable object) throws IOException, ClassNotFoundException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( object );
oos.close();
BASE64Encoder base64Encoder = new BASE64Encoder();
return new String( base64Encoder.encode(baos.toByteArray()));
}
This is what i use to find the thread objects
models.Thread thread = em.find(models.Thread.class, threadId);
If i use entityManager.find() or a query it still loads the list of replies. So to be extra clear, i would like the collection to be empty when i generate the Entity. Then if i call get() i would like it to fill up.
The following image shows that the replies are stored in the list. They do not have any values in them whitch i presume is because lazy loading means that they are proxy entities that have not really been fetched from the database? Please correct be if i am wrong. As a side note, if it is possible to store the list as an arraylist or some other standard list implementation that would be great. I understand that JPA wants the persistentBag/persistentSet notation since they can have duplicate values which standard implementation do not allow and probably some other reasons aswell. But for serializing the model to the client it would be great not to need the libraries and they also do not seem to work with android because the SSID seem to change in the libraries when using android.
I really hope this is possible. Thx in advance!