Results 1 to 5 of 5

Thread: Problem with lazy initial

  1. #1
    Join Date
    Feb 2012
    Location
    Russia, Siberia
    Posts
    8

    Default Problem with lazy initial

    Hello. My application build with Spring roo.

    First class:

    Code:
    @RooJavaBean
    @RooToString
    @RooEntity
    public class Detecter {
    
        @NotNull
        private String name;
    
        @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
        private List<Pictures> pics = new LinkedList<Pictures>();
    }
    Second class:

    Code:
    @RooJavaBean
    @RooToString
    @RooEntity
    public class Pictures {
    
        @NotNull
        private String code;
    }
    in the class Detecter i set parameter Lazy in field :: pics::

    I did it for not taking from database extra data. But in some case i need to take all related data.
    I do not how realize this choose. Give me some advice on this issue.

    P.S. Sorry for my bad English.

    Truly yours, Nafanya.

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    Hello

    I am confused with your case, first of all lets think about ORM only

    Seems your relation configuration is wrong, have you read the Hibernate documentation?

    1) Is better use a Set<Pictures> instead of List<Pictures>, because Set avoid duplication of Items within the collection
    2) The Pictures class should has a relation with Detecter to receive its PK like a FK
    3) Is better declare the class in singular, I mean only Picture
    4) What error you have received? Could you post your complete error stack trace?
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3
    Join Date
    Feb 2012
    Location
    Russia, Siberia
    Posts
    8

    Default

    Answer your questions:

    1. I use the LinkedList for two reasons:

    1.1 In HashSet each item added to the random position in set;
    1.2 LinkedList has more features than the HashSet.

    2. I do not understand what mean phrase "to receive its PK like a FK";(primary key like foreign key?)
    3. Ok. Thank you;
    4.

    Code:
    2012-04-06 13:06:35,125 [main] ERROR org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: ru.me.Detecter.pictures, no session or session was closed
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ru.me.Detecter.pictures, no session or session was closed
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
    	at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)
    	at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
    	at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
    	at MainClass.<init>(MainClass.java:38)
    	at MainClass.main(MainClass.java:46)
    Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ru.me.Detecter.pictures, no session or session was closed
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
    	at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)
    	at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
    	at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
    	at MainClass.<init>(MainClass.java:38)
    	at MainClass.main(MainClass.java:46)

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    You don't use a LinkedList as that is being replaced by a hibernate specific List implementation as soon as it becomes a managed object.

    Have you actually read the stacktrace? You retrieve an object,probably through a transactional service method, after that the session and transaction is gone.

    I suggest a forum search as questions regarding lazy loading have been answered numerous times before.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Feb 2012
    Location
    Russia, Siberia
    Posts
    8

    Default

    I agree with You. method to obtain the properties of entities (like detecter.getPictures()) called after the close of the session. I have read many articles on this issue on this and another forum. I understand I need to extend the session.
    but I did not understand how to do it.

    P.S. I develop a desktop application.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •