JPA Hibernate希望延迟加载返回空集合 [英] JPA Hibernate want lazy load to return empty collection

查看:111
本文介绍了JPA Hibernate希望延迟加载返回空集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用JPA-Hibernate并希望集合为空,直到我调用相关的get()。我已经尝试了几天而没有任何成功。
我想要这个的原因是因为我使用Exterialize(或Serialize)并且在将序列化字符串发送到客户端时并不总是希望集合存在。

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;
    }

这里是回复模型:

@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);

如果我使用entityManager.find()或查询,它仍会加载回复列表。所以要清楚一点,我希望当我生成实体时,集合是空的。然后,如果我调用get(),我希望它填满。

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.

下图显示回复存储在列表中。它们中没有任何值,因为延迟加载意味着它们是尚未真正从数据库中获取的代理实体?如果我错了,请纠正。作为旁注,如果可以将列表存储为arraylist或其他一些非常好的标准列表实现。我知道JPA需要persistentBag / persistentSet表示法,因为它们可能具有标准实现不允许的重复值,并且可能还有其他一些原因。但是为了将模型序列化到客户端,最好不要使用这些库并且它们似乎也不适用于android,因为在使用android时SSID似乎在库中发生了变化。

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.

我真的希望这是可能的。 Thx提前!

I really hope this is possible. Thx in advance!

推荐答案

我偶然发现了这个博客: https://sites.google.com/a/pintailconsultingllc.com/java/hibernate-extra-lazy-collection-提取

i stumbled over this blog: https://sites.google.com/a/pintailconsultingllc.com/java/hibernate-extra-lazy-collection-fetching


  1. 我不确定在这种情况下LazyCollectionOption.EXTRA是否适合你,打开你的sql-logging查看hibernate实际执行了哪些查询

  2. 你注释了一个Collection getter所以这可能适用于你(来自引用的博客)如果类是org.hibernate .collection.PersistentBag,你正在使用行李语义,现在当你第一次触摸列表中的任何项目时,集合会急切加载所有项目。

  3. 我不会在可能在不同时间升级客户端和服务器的情况下使用ObjectOutputStream(android类版本与服务器版本)班级版)。如果序列化为xml或json,那么在这些问题上你会遇到很多麻烦。例如,使用xstream(不要使用默认的xstream-xml序列化,因为它包含整个类名,使用别名),您可以告诉序列化程序在序列化之前通过注释或明确忽略哪些字段。

这篇关于JPA Hibernate希望延迟加载返回空集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆