LynAs
LynAs

Reputation: 6557

How to get the size of a list ( where list is a M2M map between class)

@Entity
public class EUser {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id; 
    @Column(nullable = false)
    private String userName;

    @ManyToMany
    private List<UserRole> roles;


        //getters and setters method here

}


@Entity
public class UserRole {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String roleName;


        //getters and setters method here
}

now using the service i am trying to see the size of roles list

EUser approveUser = (EUser) userService.getOne(2);
System.out.println(approveUser.getRoles().size());

but I am getting the following error

SEVERE: Servlet.service() for servlet [spring] in context with path [/Ebajar] threw exception [Request processing failed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.ebajar.model.EUser.roles, no session or session was closed] with root cause
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.ebajar.model.EUser.roles, no session or session was closed

How to solve this??

Upvotes: 0

Views: 479

Answers (2)

Rugal
Rugal

Reputation: 2717

let me explain it to you:

As spring will create a sessionFactory which related DB connection session, in debugging mode when you eager to fetch data from database, it can do it as the database session did not closed.

But when you do in lazily, pitifully session already closed and you can not get data from it. what's more, you will get no session or session was closed, I also had got this ERROR at the first time I got in touch with spring and hibernate, which puzzled me for a very long time.

I think the essential problem is that, you do not have session maintainer to keep your session alived while you are just debugging and have no struts or springmvc.

To solve this, I think There are three ways:
1. Do the debugging with the whole framework includes session maintainer, which is so slow and heavy!.
2. Add a session filter into web.xml:

<filter>
     <filter-name>session</filter-name>
    <filter-class>
        org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
    </filter-class>
</filter>
<filter-mapping>
    <filter-name> session </filter-name>
    <url-pattern> /* </url-pattern>
</filter-mapping>

which will maintain your session with database.
3. Or you could use Mocked testing method that imitate what session maintainer doing.

At the end, I will not suggest you with setting all type with eager, because this could eliminate the performance of your application. Even if it is not significant in testing stage, but to imitate real environment in testing stage is important.

Upvotes: 1

ravindra.kamble
ravindra.kamble

Reputation: 1023

Change your EUser Entity class to

@Entity
public class EUser {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id; 
    @Column(nullable = false)
    private String userName;

    @ManyToMany(fetch=FetchType.EAGER)
    private List<UserRole> roles;


        //getters and setters method here

}

Upvotes: 1

Related Questions