JPA Hibernate want lazy load to return empty collection

前端 未结 1 1953
一个人的身影
一个人的身影 2021-01-24 03:57

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 reaso

1条回答
  •  梦毁少年i
    2021-01-24 04:24

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

    1. i'm not sure if the LazyCollectionOption.EXTRA is the right one for you in this scenario, turn on your sql-logging to see what queries are really executed by hibernate
    2. you annotated a Collection getter so this may be applicable for you (from referenced blog) If the class is org.hibernate.collection.PersistentBag, you're using bag semantics and now the collection will eager load all the items the first time you touch any item in the List.
    3. i wouldn't use ObjectOutputStream in scenarios where it is possible that client and server are upgraded at different times (android class version vs. server class version). you will safe yourself a lot of trouble with these kinds of problems if you serialize to xml or json. For example with xstream (do not use default xstream-xml serialization though because it contains the whole class-name, use aliases) you can tell the serializer which fields to ignore via annotation or explicity before serializing.

    0 讨论(0)
提交回复
热议问题